Testing for the (possible) occurance of a substring.

M

Michael McCarthy

I want to test for the ~possible~ occurance of a string within another
string... IndexOfAny gives wildly odd results, possibly because it's
expecting the string to be there... This is probably exacerbated when
you compare two strings of different lengths... I simply want to test if
a stream of data has an occurance of a string in it, if so react, if not
skip that string and wait for the next one.

This seems achingly simple but it seems to be esacaping me.
Any info is appreciated, thanks.

~m.
 
J

Jon Skeet [C# MVP]

Michael McCarthy said:
I want to test for the ~possible~ occurance of a string within another
string... IndexOfAny gives wildly odd results, possibly because it's
expecting the string to be there...

IndexOfAny doesn't deal with strings at all - it deals with arrays of
characters. What "wildly odd results" are you talking about, exactly?

Anyway, to find one string within another, just use IndexOf.
 
M

Michael McCarthy

Adam said:
What about using plain IndexOf(); ? returns -1 if no substring is found.

Works great! Thanks... I did look at that, but I wasn't sure if I was
even in the right ballpark. It even has an overload that accepts a
string rather than a char.

Don't know why IndexOfAny gives results even when it shouldn't but I'll
have to experiment more when I have time on my hand. Thanks much again.

Regards,
M.
 
J

Jon Skeet [C# MVP]

Michael McCarthy said:
Don't know why IndexOfAny gives results even when it shouldn't but I'll
have to experiment more when I have time on my hand. Thanks much again.

If you have an example, I'm sure we could explain it. My guess is that
you think the char array represents a *sequence* of characters to
search for, when actually it represents a *set* of characters to look
for - if *any* of them are found, it will return an answer.
 
M

Michael McCarthy

Jon said:
IndexOfAny doesn't deal with strings at all - it deals with arrays of
characters. What "wildly odd results" are you talking about, exactly?

Anyway, to find one string within another, just use IndexOf.

I converted to a CharArray previously, but I still got results that were
unexpected to me, an interger value over 0 when the string didn't exist
in the string I was testing, but since I tried IndexOf, I went back and
tried IndexOfAny again, and it appears to be working, so I must have
done something incorrectly.

~m.
 
J

Jon Skeet [C# MVP]

Michael McCarthy said:
I converted to a CharArray previously, but I still got results that were
unexpected to me, an interger value over 0 when the string didn't exist
in the string I was testing, but since I tried IndexOf, I went back and
tried IndexOfAny again, and it appears to be working, so I must have
done something incorrectly.

Well, if you tried something like

"Word".IndexOfAny ("Foo".ToCharArray());

then that would indeed return that it had found a character, because
'o' is part of both "Word" and "Foo". As I said, IndexOfAny doesn't
look for whole strings, it looks for individual characters.
 
M

Michael McCarthy

Jon said:
Well, if you tried something like

"Word".IndexOfAny ("Foo".ToCharArray());

then that would indeed return that it had found a character, because
'o' is part of both "Word" and "Foo". As I said, IndexOfAny doesn't
look for whole strings, it looks for individual characters.

Makes sense now, thanks for clearning that up.

~m.
 

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