Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# String Contains Whole Case

Status
Not open for further replies.

FlyinBrian

Programmer
Jun 6, 2006
3
CA
Im using the .contains method to find a paticular phrase in a long string. However I want it to be whole case, in that I don't want it to return "Whatever" if I search for "What" It should only return that specific word. Any ideas?
 
Try .IndexOf() with .Substring()

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
You can do something like this

Code:
string theExpression = @"\bwhat\b";
Regex theRegex = new Regex(theExpression, RegexOptions.IgnoreCase);
		
string test1 = "What the deuce?";
string test2 = "whatever, punk.";
string test3 = "I'm down for whatever.";
string test4 = "Jigga, what?";

			Console.WriteLine(theRegex.IsMatch(test1).ToString());
			Console.WriteLine(theRegex.IsMatch(test2).ToString());
			Console.WriteLine(theRegex.IsMatch(test3).ToString());
			Console.WriteLine(theRegex.IsMatch(test4).ToString());

That returns true, false, false, true, which is the expected result.

You can easily generate that expression on the fly by dynamically building the string I declare as theExpression.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Is there any easier way to do it? I have conditional statements like
if (input.Contains("Blah")) {
//....

}

I dont want to change my code too much.
 
Like two lines of that is doing all of the work. You can put the offending code into a code library and call it. Something like this.

Code:
public static bool StringContains (string pStringToSearch, string pWhatToSearchFor)
{
string theExpression = String.Format(@"\b{0}\b", pWhatToSearchFor);
Regex theRegex = new Regex(theExpression, RegexOptions.IgnoreCase);
//alternatively, if you want it to be case sensitive, the line would be
// Regex theRegex = new Regex(theExpression);

return theRegex.IsMatch(pStringToSearch);
}

Then you would call it like this
Code:
if (StringContains(input, "Blah"))
{
//...
}

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Thanks, however, it is returning true when searching for
70.66.160 when there is 70.66.160.196

 
That is a result of the RegEx that I threw together. I am by no means a regex expert, perhaps someone else here could help. The one I put up is for words only. If you are searching for strings where a period is between a yes and a no, it won't work. The \b on either side looks for the word to be in its own word block, meaning either a space on either side, a punctuation, or the beginning or end of the string.

Since the IP address-like string you put there has a period between the searcher and the searchee, it returns true.

You would probably need a more robust regex to do that. I especially don't see any way to do it using what .Net gives you as methods of the String Object.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top