For statement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a string.
When I step through it in the debugger, it highlights the first statement
(int at=0),
then highlights the second statement, then jumps to the next line of code.
It doesn't appear to increment n at all, nor does it appear to ever evaluate
the 3rd statement of the for loop. The string I debugged with has 3 newlines
at various locations.

Thanks,
Gary
 
Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a string.
When I step through it in the debugger, it highlights the first statement
(int at=0),
then highlights the second statement, then jumps to the next line of code.

The for statement has the following form:
for ([initializers]; [expression]; [iterators];) {
// statements
}

The initializers are a comma separated list of statements or expressions to
initialize counters.
The expression is an expression that can be evaluated as a boolean value. If
the expression evaluates to true the statements inside the for are executed
and then the iterators are evaluated.
The iterators are statements to increase or decrement the counters.

In your code the expression at==-1 always evaluates to false since at is
initialized to 0.

To count the number of occurances of a new line you can use this code:
int startPos=0;
int foundPos=0;
int count=0;
do {
foundPos=msg.IndexOf('\n',startPos);
if (foundPos>-1) {
startPos=foundPos+1;
count++;
}
} while(foundPos > -1 && startPos < msg.Length);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Can anyone tell me why this statement doesn't work?
^^

I believe you want != there.
This won't work unless n is initialized to -1 because it will do a iteration
for at==-1 resulting in n being of by one. Of course, you could subtract 1
from n after the loop. Still, it would be faster than my example.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Mattias Sjögren said:
^^

I believe you want != there.

Even with at != 1, it does not work. If first char is \n, the loop won't see
it because IndexOf is called with an index of 1 the first time.
And also, the count is incremented once more than necessary.

The following should work:

int n = 0;
for (int at = -1; (at = msg.IndexOf('\n', at + 1)) != -1; )
n++;

but I would prefer:

int n = 0;
int at = -1;
while ((at = msg.IndexOf('\n', at + 1)) != -1)
n++;

Unfortunately, the while syntax does not let me scope the at variable, but
this looks cleaner.
 
Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexOf("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a
string. When I step through it in the debugger, it highlights
the first statement (int at=0),
then highlights the second statement, then jumps to the next
line of code. It doesn't appear to increment n at all, nor does
it appear to ever evaluate the 3rd statement of the for loop.
The string I debugged with has 3 newlines at various locations.

Gary,

In addition to the other posts, here is a non-deterministic way to
count the number of newlines in a string:

using System.Text.RegularExpressions;
...
string input = "one\ntwo\nthree\n";
string regex = "\n";
int numberOfMatches = Regex.Matches(input, regex, RegexOptions.Singleline).Count;
 
In addition to the other posts, here is a non-deterministic way
to count the number of newlines in a string:

Duh! I really should proofread before I post. I meant non-
procedural, not non-deterministic.


Chris.
 
Back
Top