I have this string in my code:
string test = @"aaaaa aaaaa aaaaa bbbb ttttt tttttt
33333 44444 777777 77777 88888 8888 8888 88888 88888
wwwww wwwww wwwwww wwwww wwwww wwwwww";
And here is my code spliting string by new line character and tab.
foreach (var line in test.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
{
foreach (var lineItem in line.Split('\t'))
{
}
}
In first interation of first loop line variable is "aaaaa aaaaa aaaaa bbbb ttttt tttttt" and this is correct, but in seconds loop first interation variable lineItem is the same, it doesn't split this string with tab separator. Why it's happening?
Are you sure your string has tab characters ? try using Split()
it will split by tab and white-space:
foreach (var lineItem in line.Split())
{
}
The reason it does not work is because the tabs
are not tabs in your string test
- see this:
The first line is what i generated myself from MS Excel. Use regex to correctly represent any whitespace char (even for Unicode escape sequences)
Regex regex = new Regex(@"\s");
string[] bits = regex.Split(line);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.