
-----------------------------------
Raknarg
Wed May 25, 2011 9:58 am

Index function
-----------------------------------
Is there a string indexing function in VB like there is in Turing?

-----------------------------------
Nick
Wed May 25, 2011 10:10 am

RE:Index function
-----------------------------------
string.IndexOf ("pattern")

-----------------------------------
2goto1
Wed May 25, 2011 10:11 am

RE:Index function
-----------------------------------
There are quite a few more things you can with strings, http://msdn.microsoft.com/en-us/library/system.string.aspx

-----------------------------------
Raknarg
Wed May 25, 2011 10:36 am

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.

-----------------------------------
2goto1
Wed May 25, 2011 12:24 pm

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")[/code]  Returns -1 since A is alphabetically before B
[code]String.Compare("a", "B")[/code]  Returns -1 since a is alphabetically before B
[code]String.Compare("B", "A")[/code]  Returns 1 since B is alphabetically after A
[code]String.Compare("B", "B")[/code]  Returns 0 since B is alphabetically the same as B

Here?s where it can get confusing:
[code]String.Compare("B", "b") [/code] Returns 1 since although B is alphabetically the same as b, B?s character code is less than b
[code]String.Compare("b", "B")[/code]  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)[/code] 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)[/code]  Returns -1 since apple is alphabetically before Apricot
[code]String.Compare("Apricot", "Apple", true)[/code]  Returns 1 since apricot is alphabetically before apple

-----------------------------------
Raknarg
Wed May 25, 2011 3:05 pm

RE:Index function
-----------------------------------
Oh, thanks, I was using it wrong.
