-2

I need to format the string so that it should ignore case. if the word is "it" then it should consider "It" an "IT" also.How can i achieve that.I have a search function which looks for the word in word document. The below format is to search for the wholeword "it" and not between any word like w"it"nessed. I also want to include the ignore case format. How do i do that

   string Pattern = String.Format("<{0}>", text);
leppie
  • 115,091
  • 17
  • 196
  • 297
Robin
  • 87
  • 3
  • 14

3 Answers3

2

It sounds like you need "CaseInsensitive Find". IndexOf method can do that as an alternative to Regex.

int index = source.IndexOf(searchKeyword, StringComparison.OrdinalIgnoreCase);
if(index >= 0)
{
    Console.WriteLine("Found at index " + index);
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
0

Use .ToLower() or .ToUpper() on both the pattern and the source when searching, which makes sure that regardless of the initial casing, the cases are identical during the search.

Tobberoth
  • 9,327
  • 2
  • 19
  • 17
  • That sounds really inefficient for searching in a large body of text like a word document. You'd need to transform the entire text for every search. – Rotem Nov 12 '13 at 11:08
  • @Rotem, you could just save the converted text and search that immediately, so you only need to convert once. It's not optimal for a massive body of text, but definitely the easiest way to get it done if performance is not critical. Should be noted that regexp is not always faster than ToUpper conversion. Here's an example reference: http://stackoverflow.com/questions/17579368/regex-ismatch-vs-string-toupper-contains-performance – Tobberoth Nov 12 '13 at 11:09
  • The OP never stated how much data there is, so I don't see why this answer is downvoted. – Dave Nov 12 '13 at 11:12
  • "I have a search function which looks for the word in word document" - word documents tend to not be very small. I'm not the downvoter btw, the answer is technically correct. – Rotem Nov 12 '13 at 11:26
0

You can use:

String.Compare(first_string, second_string, StringComparison.OrdinalIgnoreCase) ;

It'll return -1,0,1 ... so, if you want the same, ignoring case, compare that with 0.

Noctis
  • 11,507
  • 3
  • 43
  • 82