Computer Science Canada

Index function

Author:  Raknarg [ Wed May 25, 2011 9:58 am ]
Post subject:  Index function

Is there a string indexing function in VB like there is in Turing?

Author:  Nick [ Wed May 25, 2011 10:10 am ]
Post subject:  RE:Index function

string.IndexOf ("pattern")

Author:  2goto1 [ Wed May 25, 2011 10:11 am ]
Post subject:  RE:Index function

There are quite a few more things you can with strings, http://msdn.microsoft.com/en-us/library/system.string.aspx

Author:  Raknarg [ Wed May 25, 2011 10:36 am ]
Post subject:  RE:Index function

Ok then, one more question: What value does String.Compare return if the string isn't inside the other string? I tried 0, but it doesn't seem to work.

Author:  2goto1 [ Wed May 25, 2011 12:24 pm ]
Post subject:  Re: Index function

Compare returns an integer that indicates their relative position in the sort order, as mentioned at http://msdn.microsoft.com/en-us/library/84787k22.aspx

It tells you the alphabetical ordering of two strings. Here are some test cases that you can try:

code:
String.Compare("A", "B")
Returns -1 since A is alphabetically before B
code:
String.Compare("a", "B")
Returns -1 since a is alphabetically before B
code:
String.Compare("B", "A")
Returns 1 since B is alphabetically after A
code:
String.Compare("B", "B")
Returns 0 since B is alphabetically the same as B

Here?s where it can get confusing:
code:
String.Compare("B", "b")
Returns 1 since although B is alphabetically the same as b, B?s character code is less than b
code:
String.Compare("b", "B")
Returns -1 since although b is alphabetically the same as B, b?s character code is greater than B

The API seems to differ once you start comparing the same letter, but different case. If your intent is to determine that b is alphabetically the same as B, then your best bet is to use this method:

code:
String.Compare("b", "B", true)
Returns 0 - equivalent ignoring case

Where true means to ignore case.

The same thing applies when comparing longer strings, i.e.:

code:
String.Compare("Apple", "Apricot", true)
Returns -1 since apple is alphabetically before Apricot
code:
String.Compare("Apricot", "Apple", true)
Returns 1 since apricot is alphabetically before apple

Author:  Raknarg [ Wed May 25, 2011 3:05 pm ]
Post subject:  RE:Index function

Oh, thanks, I was using it wrong.


: