'Comparing' two strings.
-
Hello, I am facing a little problem with strings. In my application, I need to make sure that a long string, let's say "Hello world, I am currently on CodeProject." contents a small string, lets say "CodeProject". I'd like to find a function or a way to return true if the small string is in the large string and false if it isn't present. Thank you for your help. - Heel
-
Hello, I am facing a little problem with strings. In my application, I need to make sure that a long string, let's say "Hello world, I am currently on CodeProject." contents a small string, lets say "CodeProject". I'd like to find a function or a way to return true if the small string is in the large string and false if it isn't present. Thank you for your help. - Heel
It's easy to write a function for this, but a better idea is to get in the habit of doing things in a standard way. The
String
class has a method calledIndexOf
that lets you get the first occurrence of a substring; that's how people usually accomplish your task. So thisif (myString.IndexOf(mySubstring) >= 0)
{
}is better than a hand-rolled function because it's faster and it's standard, which means easy to understand at a glance. - Jeff
-
It's easy to write a function for this, but a better idea is to get in the habit of doing things in a standard way. The
String
class has a method calledIndexOf
that lets you get the first occurrence of a substring; that's how people usually accomplish your task. So thisif (myString.IndexOf(mySubstring) >= 0)
{
}is better than a hand-rolled function because it's faster and it's standard, which means easy to understand at a glance. - Jeff
-
Hello, I am facing a little problem with strings. In my application, I need to make sure that a long string, let's say "Hello world, I am currently on CodeProject." contents a small string, lets say "CodeProject". I'd like to find a function or a way to return true if the small string is in the large string and false if it isn't present. Thank you for your help. - Heel
Jeff's solution is recommended for your case, but if you ever need a bit more power (pattern matching) you may want to look into regular expressions. http://www.ondotnet.com/pub/a/dotnet/2002/03/11/regex2.html[^] HTH
R.Bischoff .NET, Kommst du mit?
Great Freeware -> Abilon - Rss Reader | Zip Genius | SmartFTP -
You're welcome!