Skip to content

Instantly share code, notes, and snippets.

@itn3000
Last active May 29, 2023 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itn3000/0b59f284b38eb9347ed18fe3027ef863 to your computer and use it in GitHub Desktop.
Save itn3000/0b59f284b38eb9347ed18fe3027ef863 to your computer and use it in GitHub Desktop.
split string test
using System.Text;
// space(U+0020),tab(U+0009),fullwidth space(U+3000),linefeed(U+000a)
var str = "a b\tc d\n";
foreach(var x in str.Split(' '))
{
Console.WriteLine($"a: '{x}'");
}
foreach(var x in str.Split(null))
{
Console.WriteLine($"b: '{x}'");
}
foreach(var x in str.Split(""))
{
Console.WriteLine($"c: '{x}'");
}
foreach(var x in str.Split(new char[0]))
{
Console.WriteLine($"d: '{x}'");
}
foreach(var x in str.Split(default(string[]), StringSplitOptions.None))
{
Console.WriteLine($"e: '{x}'");
}
foreach(var x in str.Split(new string[0], StringSplitOptions.None))
{
Console.WriteLine($"f: '{x}'");
}
foreach(var x in str.Split(new string[]{ "" }, StringSplitOptions.None))
{
Console.WriteLine($"g: '{x}'");
}
a: 'a'
a: 'b c d
'
b: 'a'
b: 'b'
b: 'c'
b: 'd'
b: ''
c: 'a b c d
'
d: 'a'
d: 'b'
d: 'c'
d: 'd'
d: ''
e: 'a'
e: 'b'
e: 'c'
e: 'd'
e: ''
f: 'a'
f: 'b'
f: 'c'
f: 'd'
f: ''
g: 'a b c d
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment