Question

Locked

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

By DavidCon999 ·
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.

This conversation is currently closed to new comments.

8 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Finding a string within a string

Collapse -

Is this VB ?

by Tony Hopkinson In reply to How do I check for a stri ...

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

Collapse -

Reponse To Answer

by DavidCon999 In reply to Is this VB ?

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.

Collapse -

Reponse To Answer

by Tony Hopkinson In reply to Is this VB ?

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?

Collapse -

What language?? Hint:

by navdeepz In reply to How do I check for a stri ...

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

Collapse -

Reponse To Answer

by DavidCon999 In reply to What language?? Hint:

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.

Collapse -

Reponse To Answer

by Tony Hopkinson In reply to What language?? Hint:

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

Collapse -

Reponse To Answer

by navdeepz In reply to What language?? Hint:

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 !! ??

Back to Web Development Forum
8 total posts (Page 1 of 1)  

Related Discussions

Related Forums