Question Locked

How do I check for a string within a string irrespective of case?

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am extracting strings from HTML pages:
1 "Test <font size = '4'>text size</font> sample"
but I could have
2 "Test <FONT size = '4'>text size</font> sample"
or perhaps
3 "Test <font SIZE = '4'>text size</font> sample"
or even
4 "Test <FONT SIZE = '4'>text size</font> sample"

Ok so what I want to do is confirm that the string I have extracted contains the phrase "FONT SIZE", irrespective of case.

So, having extracted the HTML into a string called HTMLText and thinking that CompareMethod.Text did just this, I coded:

Dim FontSizeTagStart as integer

FontSizeTagStart = HTMLText.IndexOf("FONT SIZE", CompareMethod.Text)

but unless I have string 4 above (which correctly gives a result for FontSizeTagStart of 6) I get a result for FontSizeTagStart of -1.

Clearly I'm doing something wrong but can anyone point out what it is please.

9 replies

by tony hopkinson

Use String.Compare(str1, str2, true)
Alternative but ineffecient and messy force the case e.g.
HTMLText.ToUpper().IndexOf("FONT SIZE", CompareMethod.Text)

by davidcon999

Thanks. I'm using VB.net 2010.

But what's the point in using CompareMethod=text if we're going to convert everything to upper case anyway? It seems to me the whole point of CompareMethod is to avoid having to convert to case.

Anyway, I'm not fretting over it and happy to use the work around - thanks again.

by tony hopkinson

Alternative, ie instead of, either or. this or that etc.

Why would I suggest using both, that would make me some sort of twit wouldn't it?

by navdeepz

well since i do not know what language are you using , i can just give u a advice maybe it works. Try something like this ...
1. convert the entire string into UPPER or LOWER and store it in a different string variable.
2. now compare the string with the text that you want to compare it with, keeping in mind the CASE of the string. i.e incase of UPPER use the UPPER CASE to compare or vice versa.
3 get the value from the string comparing function.

and your done :) hope this might help

by davidcon999

Thanks. I'm using VB.net 2010. Thing is both .IndexOf and .Compare include CompareMethod=text which according to the documentation should do what I need to do without the workaround suggested. Don't get me wrong I'm happy to use the work around but what's the point in CompareMethod if it doesn't work.

What really gets me is this has been around for many many years going back to pre-Windows days even. So it's nearly (only nearly cos it is a MS product) inconceivable that it really doesn't work.

by tony hopkinson

It does work Compare.Text is case sensitive. That's what CompareType.Text means....

by navdeepz

well i dont think there is any other way to go about it .. its either CompareMethod.Text or changing the method ... well yes that makes me think why do we really need CompareMethod.Text , and that also makes me wonder what would be the use of CompareMethod.Binary !! ??

This discussion is closed to new replies.