How to Split a String by a Multi-Character Delimiter in C#

string.split - by multiple character delimiter

To show both string.Split and Regex usage:

string input = "abc][rfd][5][,][.";
string[] parts1 = input.Split(new string[] { "][" }, StringSplitOptions.None);
string[] parts2 = Regex.Split(input, @"\]\[");

Split Multiple character in C#

I believe you want to split on space, ,,. and -, then try:

string[] splitArray = s1.Split(',','-','.',' ');

string.Split returns an array of string element, not a single string element.

string.split - by multiple character delimiter },{

You can use a regular expression:

var sample = "abc},{rfd},{5},{,},{.";
var result = Regex.Split(sample, Regex.Escape("},{"));
foreach (var item in result)
Console.WriteLine(item);

Split by multiple characters

string[] list = b.Split(new string[] { "ab" }, StringSplitOptions.None);

How do I split a string by a multi-character delimiter in C#?

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

Example from the docs:

string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};
string[] result;

// ...
result = source.Split(stringSeparators, StringSplitOptions.None);

foreach (string s in result)
{
Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}

How to split a string by multiple chars?

You can split by string, not by character:

var result = ip.Split(new string[] {" | "}, StringSplitOptions.RemoveEmptyEntries);

splitting a string based on multiple char delimiters

Use string.Split(char [])

string strings = "4,6,8\n9,4";
string [] split = strings .Split(new Char [] {',' , '\n' });

EDIT

Try following if you get any unnecessary empty items. String.Split Method (String[], StringSplitOptions)

string [] split = strings .Split(new Char [] {',' , '\n' }, 
StringSplitOptions.RemoveEmptyEntries);

EDIT2

This works for your updated question. Add all the necessary split characters to the char [].

string [] split = strings.Split(new Char[] { ',', '\\', '\n' },
StringSplitOptions.RemoveEmptyEntries);

Split a string on multiple delimiters and keep them in the output

Try this,

private char[] alphabets = {'A','B','C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};    

var input = "AB123456789C123412341234B123";
var result = input.SplitAndKeep(alphabets).ToList();

Sample Image

public static class Extensions
{
public static IEnumerable<string> SplitAndKeep(this string s, char[] delims)
{
int start = 0, index;
while ((index = s.IndexOfAny(delims, start)) != -1)
{
if (index - start > 0)
yield return s.Substring(start, index - start);
yield return s.Substring(index, 1);
start = index + 1;
}
if (start < s.Length)
{
yield return s.Substring(start);
}
}
}

Split string - By multiple character like {1} {2} {3}

OK, I want to play too! Your input string has a space both before and after the numbers in brackets. I put one space after the numbers. Then there should be one space left between the words when the Join is performed.

 string s = "Hello {1} World {2} example {3} today";
string[] splitters = { "{1} ", "{2} ", "{3} " };
string[] newS = s.Split(splitters, StringSplitOptions.None);
string Final = String.Join("",newS);
Debug.Print(Final);

To use Debug.Print add a using statement System.Diagnostics

Split a string base on multiple delimiters specified by user

Add the user input into the List delimiters.

 string data = "Car|cBlue,Mazda~Model|m3";
List<string> delimiters = new List<string>();
delimiters.Add("|c");//Change this to user input
delimiters.Add("|m");//change this to user input

string[] parts = data.Split(delimiters.ToArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string item in parts)
{
Console.WriteLine(item);
}


Related Topics



Leave a reply



Submit