Regex.Split help
-
I am counting words in string, that i get from text file. Using this code i get always 1 more word in counting. What could be wrong?
int stevec = 0;
string s="avc sde s a s"
string[] words = Regex.Split(s, @"[\S]+");
foreach (string word in words)
{
stevec++;
}After deb stevec has value 6. But there are only 5 words.
-
I am counting words in string, that i get from text file. Using this code i get always 1 more word in counting. What could be wrong?
int stevec = 0;
string s="avc sde s a s"
string[] words = Regex.Split(s, @"[\S]+");
foreach (string word in words)
{
stevec++;
}After deb stevec has value 6. But there are only 5 words.
I tested your regex.. Making the S lowercase did the trick.
Regex.Split(s, @"[\s]+");
:bob: Kristian Sixhoej "You can always become better." - Tiger Woods
-
I am counting words in string, that i get from text file. Using this code i get always 1 more word in counting. What could be wrong?
int stevec = 0;
string s="avc sde s a s"
string[] words = Regex.Split(s, @"[\S]+");
foreach (string word in words)
{
stevec++;
}After deb stevec has value 6. But there are only 5 words.
gee. Did you consider looking what is inside the words array? to fix a faulty program, you need to open your eyes and look around. Nobody is going to do that for you. If you get all the facts, and still can't figure it out, then post a real question, clear and well documented. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
gee. Did you consider looking what is inside the words array? to fix a faulty program, you need to open your eyes and look around. Nobody is going to do that for you. If you get all the facts, and still can't figure it out, then post a real question, clear and well documented. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
Sorry.. while i was debuggin i couldn't see words with breakpoints, even in console they weren't visible. And with greater S using only Regex.Match the counting was correct, thats why i asked here. Anyway, thanks for help.
there are many debugging tools, the simplest one is Console.WriteLine; it works always, never had a problem with it. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
I am counting words in string, that i get from text file. Using this code i get always 1 more word in counting. What could be wrong?
int stevec = 0;
string s="avc sde s a s"
string[] words = Regex.Split(s, @"[\S]+");
foreach (string word in words)
{
stevec++;
}After deb stevec has value 6. But there are only 5 words.
@"[\S]+" should be replaced by @"[\s]+". "s" should be the lowercase.
April Comm100 - Leading Live Chat Software Provider