String split
In this chapter you will learn:
- How to split a string
- Use the Split() method to split strings
- Split with space
- Split with a list of dividers
- Split by char array
- Split a string delimited by another string and return 2 non-empty elements
- Split string with \\
Split string
using System;/*from j ava 2 s .co m*/
class Sample
{
public static void Main()
{
string s = "this is a test from java2s.com";
string[] arr = s.Split();
foreach (string ss in arr)
{
Console.WriteLine(ss);
}
}
}
The output:
Use the Split() method to split strings
Split
method splits a string by
specified characters and returns an array of strings.
By default Split
method uses space as the separator.
using System;/* j a v a 2 s .com*/
class MainClass
{
public static void Main()
{
string[] myStrings = {"To", "be", "or", "not", "to", "be"};
string myString9 = String.Join(".", myStrings);
myStrings = myString9.Split('.');
foreach (string mySplitString in myStrings)
{
Console.WriteLine("mySplitString = " + mySplitString);
}
}
}
The code above generates the following result.
mySplitString = To//from ja v a2 s.com
mySplitString = be
mySplitString = or
mySplitString = not
mySplitString = to
mySplitString = be
Split with space
using System;/*from ja va2 s . c o m*/
using System.Text;
class SplitStringApp
{
static void Main(string[] args)
{
string s = "Once Upon A Time In America from java2s.com";
char[] seps = new char[]{' '};
foreach (string ss in s.Split(seps))
Console.WriteLine(ss);
}
}
Split with a list of dividers
To split with more than one delimiters use the overloaded version:
using System;/*from j a v a 2s. c om*/
class Sample
{
public static void Main()
{
string s = "this is a test from java2s.com";
string[] arr = s.Split(new char[] { ' ', 't' });
foreach (string ss in arr)
{
Console.WriteLine(ss);
}
}
}
The output:
Split by char array
using System;// ja v a 2 s . c om
public class Main
{
static void Main(string[] args)
{
string MyString = "The quick brown fox ran around! java2s.com";
string[] MyStringSplit = new string[6];
MyStringSplit = MyString.Split(new char[] { ' ' }, 6);
Console.WriteLine(MyStringSplit[1] + MyStringSplit[3]);
}
}
Split a string delimited by another string and return 2 non-empty elements
Split also accepts a StringSplitOptions
enum.
We can use StringSplitOptions
to remove empty entries.
using System;//j a v a 2s.c o m
class Sample
{
public static void Main()
{
string s2 = "[divider]" +
"A[divider][divider]" +
"B[divider][divider][divider]" +
"C[divider][divider]";
string[] stringSeparators = new string[] {"[divider]"};
string[] result;
result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
Show(result);
}
public static void Show(string[] entries)
{
foreach (string entry in entries)
{
Console.WriteLine(entry);
}
}
}
Split string with \\
using System;/*j a v a 2 s . co m*/
class Class1
{
[STAThread]
static void Main(string[] args)
{
char delim = '\\';
string filePath = "C:\\Windows\\Temp";
string [] directories = null;
directories = filePath.Split( delim );
foreach (string directory in directories)
{
Console.WriteLine("{0}", directory);
}
}
}
Next chapter...
What you will learn in the next chapter:
- Search with IndexOf
- String from the middle
- String by case
- Search a character from the last with LastIndexOf
- Index of any characters
- Search a character in a string
Home » C# Tutorial » String