step through string characters

M

mp

is this the only (or best) way to step through the characters in a string?
for (int CharPos = 0; CharPos < InputString.Length; CharPos++)
{
Debug.Print(InputString.Substring(CharPos,1))
}

thanks
mark
 
M

mp

Peter Duniho said:
is this the only (or best) way to step through the characters in a
string?
for (int CharPos = 0; CharPos< InputString.Length; CharPos++)
{
Debug.Print(InputString.Substring(CharPos,1))
}

No. In fact, it's an awful way.

At the very least, this would be better:

for (int i = 0; i < inputString.Length; i++)
{
Debug.Print(inputString);
}

If you don't need the string index, a more readable approach would be:

foreach (char ch in inputString)
{
Debug.Print(ch);
}

Pete


ah! i didn't realize String was so flexible!
in your first example it looks "like an array"
in the second "like a collection" (supporting foreach)
that's cool!
the only reason I need the index is, if i find a certain character, i need
to recheck the preceeding character

so in your example1 I could look at (inputString[i - 1])
if (inputString == "\""){if(inputString[i-1]== "\\"){...}else {...};

or in example2, i could store a PreceedingCharacter variable each time
char lastchar;
foreach (char ch in inputString)
{if (ch == "\"")
{if(lastchar != null)
{if(lastchar == "\\")
{...}
}
}
lastchar = ch}
or something like that...

would there be reason to prefer one or the other?
thanks
mark
 
J

Jeff Johnson

ah! i didn't realize String was so flexible!
in your first example it looks "like an array"
in the second "like a collection" (supporting foreach)

In .NET, it isn't really a "collection" that provides foreach but rather the
fact that a type implements IEnumerable provides foreach. And since
System.Array implements IEnumerable, all arrays do.
 
M

mp

Jeff Johnson said:
In .NET, it isn't really a "collection" that provides foreach but rather
the fact that a type implements IEnumerable provides foreach. And since
System.Array implements IEnumerable, all arrays do.

Jeff,
thanks for the terminology correction...
I tend to get sloppy with my language and just mean looks like (to my old
vb6 brain)
so a string is an object, but the object is implemented as an array of
characters?
mark
 
J

Jeff Johnson

thanks for the terminology correction...
I tend to get sloppy with my language and just mean looks like (to my old
vb6 brain)

Yeah, and since I knew you were coming from VB I wanted to make sure that
you understood the difference in .NET. (Did/do you post in
m.p.vb.general.discussion?)
so a string is an object, but the object is implemented as an array of
characters?

Yes, the backing store for a string is a character array. String implements
IEnumerable and then basically delegates a foreach request to the underlying
array. (If String didn't implement IEnumerable itself then you wouldn't be
able to do foreach, since the array is a private member and therefore can't
be accessed directly. It also uses the C# concept of an "indexer" to allow
it to behave like an array with []-style access.)
 
M

mp

Jeff Johnson said:
Yeah, and since I knew you were coming from VB I wanted to make sure that
you understood the difference in .NET. (Did/do you post in
m.p.vb.general.discussion?)

yes i did and sometimes still do, and it's sad to see how quiet it's become
if i want to continue learning AutoCad programming i'm forced to move to
dotnet
so i'm over here more these days
so a string is an object, but the object is implemented as an array of
characters?

Yes, the backing store for a string is a character array. String
implements IEnumerable and then basically delegates a foreach request to
the underlying array. (If String didn't implement IEnumerable itself then
you wouldn't be able to do foreach, since the array is a private member
and therefore can't be accessed directly. It also uses the C# concept of
an "indexer" to allow it to behave like an array with []-style access.)
thanks
mark
 
J

Jeff Johnson

Interesting trivia (to me, anyway): while this almost never comes up, the
"foreach" statement is the one place I'm aware of in C# where "duck
typing" is used.

Specifically, the collection expression does not actually need to
implement IEnumerable. It just needs to have a parameterless
GetEnumerator() method. And the return type of that method does not
actually need to be an IEnumerator type, but rather just needs to have the
necessary Current property and MoveNext() method.

In practice, a type that has an appropriate GetEnumerator() returning an
appropriate type with implementations of Current and MoveNext() is pretty
much always going to actually be declared as implementing IEnumerable.
But the C# specification doesn't require that.

Interesting to me, too.
 
M

mp

Peter Duniho said:
That all depends on what else you're doing. If the only purpose of the
loop is to search for the string @"\\" (note the @.without it, that string
has only a single backslash),

yes, i think that's what i'm trying to do...i just want to compare a single
character to \
but to do that i need the two \\
as you point out below "\" would not work, i actually need "\\" to represent
\
and "\"" to represent "
in addition to the fact that i discovered i need ch.ToString() not just ch


then I'd use the Contains() or
IndexOf() method (depending on whether you just need to know it's there,
or if you actually need to know the position).

If you have other processing going on within the loop as well, then either
alternative you've suggested should be fine. In fact, in the indexed
version you could even look at the next character rather than the
preceding one (but of course you'd have to check the index against the
string length in that approach).

Note that the code you posted won't actually compile successfully. The
char type is a value type and so is non-nullable (but you can use a
different initial value, such as '\0'.the only important thing is that it
isn't initialized to '\\'), and a string "\" without the @ is not a
properly terminated string. But the general idea is fine.

Pete

right, that was just bad pseudocode...
i discovered i needed:
if (ch.ToString == "\"")

....or perhaps, since i only am looking at two possible characters(in this
example)
perhaps i should initialize a character variable ch1 to " , and another
character ch2 to \
then i could use ch.CompareTo ch1 or lastChar.CompareTo ch2
i'm thinking maybe the .ToString method is "expensive" - not that it matters
in my case, but just to learn the "right" way...

thanks again
mark
 
M

mp

Peter Duniho said:
For strings, you use double-quotes, for chars, you use single quotes. You
can compare a char variable to a single character literal like so:

if (ch == '\\')

But all that is ignoring that if all you want to know where, if any, those
characters are in a string, you should just use the built-in method
IndexOf().

Pete

i think indexOf only finds the first occurrance...i may have more than one
in each line
also
i'm using the character check to see if i'm "inside a string<or quote>" or
not
so i'm tracking the occurrances of start quote <">
and end quote <">
which can occur on different lines (input to this function)
and setting a boolean variable _AmInQuote to track the state

ultimately this is for the same function that's looking for the ; character
but i have to track if ; occurs inside a string(quote) or outside
thats' why a simple IndexOf(";") won't tell me the whole story.

at least thats what i'm thinking....???
thanks
mark
 
M

mp

Peter Duniho said:
[...]
if (ch == '\\')

or if(ch.Equals ('\\'))

any preference?

I find the former more readable.

Also, the C# compiler always generates a method call for the latter, while
it's able to optimize the former. In theory, the JIT compiler could pick
up where the C# compiler leaves off, and optimize the Equals() method
call, but it in fact does not.

So, the bottom line is that the former is always going to perform better,
and at least some people (like me :) ) prefer the way it looks too.

Pete

excellent info, thanks
mark
 
M

mp

Peter Duniho said:
[...]
ultimately this is for the same function that's looking for the ;
character
but i have to track if ; occurs inside a string(quote) or outside
thats' why a simple IndexOf(";") won't tell me the whole story.

That's fine. But if so, then your reply of "yes, i think that's what i'm
trying to do" to my question when I suggested Contains() or IndexOf() in
the first place was inaccurate.

sorry for the misunderstanding....
the text i was replying to was:
If the only purpose of the
loop is to search for the string @"\\" (note the @.without it, that
string
has only a single backslash),

yes, i think that's what i'm trying to do... [...]

what i meant was, i'm not searching for the string @"\\" (double backslash)
I'm searching for the string "\\" (single backslash)
sorry for not making that more clear.

i appreciate all the time and info you have provided to help me with all
these beginner questions

mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top