473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Testing for the (possible) occurance of a substring.

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.
Nov 17 '05 #1
4 2861
What about using plain IndexOf(); ? returns -1 if no substring is found.

"Michael McCarthy" <no********@nomail.com> wrote in message
news:uX**************@TK2MSFTNGP15.phx.gbl...
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.

Nov 17 '05 #2
Michael McCarthy <no********@nomail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Adam Barker wrote:
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.
Nov 17 '05 #4
Jon Skeet [C# MVP] wrote:
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.
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Harman | last post by:
Hi, I'm trying to do a MySQL Query using Mysql 3.23.58 something like that below SELECT name FROM customers WHERE name LIKE "Fred" ORDER BY difference(name,"Fred"); The difference piece...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
7
by: news.hku.hk | last post by:
how to count the occurance of a character in a string ? e.g. #include <iostream> #include <string> using namespace std; int main (){ string text = "abc, def, ghi, jkl, mno"; // how to count...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: The Eclectic Electric | last post by:
I'm very noobish when it comes to Javascript, in fact I wasn't intending to use it at all, but I've got myself distracted by this "problem". I want to use an animated gif as my favicon. This...
1
by: hairlessOrphan | last post by:
Hi! Is there any way to get the Replace() function in MSSQL to replace only the first occurence of a substring? For example, my Name column has the following data: Bob BobBob And I want to...
2
by: Maxington | last post by:
The problem I am left with is that I need to split a string into substrings and determine the character location of the 30th occurance of "\n" string. I have a string that has "\n" within it and I...
9
by: tinnews | last post by:
What's the neatest and/or most efficient way of testing if one of a set of strings (contained in a dictionary, list or similar) is a sub-string of a given string? I.e. I have a string delivered...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.