Split divides a string up into pieces:
using System; class MainClass//from w w w.j av a 2 s .c o m { public static void Main(string[] args) { string[] words = "this is a test".Split(); foreach (string word in words) Console.Write (word + "|"); } }
By default, Split uses whitespace characters as delimiters.
Split method is overloaded to accept a params array of char or string delimiters.
Split optionally accepts a StringSplitOptions enum, which has an option to remove empty entries.
Join method does the reverse of Split. It requires a delimiter and string array:
using System; class MainClass/* w ww . ja v a 2 s .co m*/ { public static void Main(string[] args) { string[] words = "this is a test".Split(); string together = string.Join (" ", words); Console.WriteLine(together); } }
Concat method is similar to Join but accepts only a params string array and applies no separator.
Concat is equivalent to the + operator.
using System; class MainClass/* w w w. j av a 2s.co m*/ { public static void Main(string[] args) { string sentence = string.Concat ("a", " b", " c", " d"); Console.WriteLine(sentence); string sameSentence = "a" + " b" + " c" + " d"; Console.WriteLine(sameSentence); } }