Home All Groups Group Topic Archive Search About

IndexOf string method

Author
15 Aug 2006 4:09 PM
sh
The IndexOf string method returns the position of the first occurrence
of a character in a string. Is there a method to find the x occurrence
of a character, for example, the 28th occurrence of a character in a string?

Thanks

Author
15 Aug 2006 4:57 PM
Cor Ligthert [MVP]
Shamanda,

I don't know if you are the same as gonzosez where is asked almost the same
question in the second message after yours.

\\\
dim mystring as string = "I contain 70 characters"
if mystring.length > 27 then
dim TwentyEightchar = mystring(27)
end if
///
I hope this helps,

Cor

Show quoteHide quote
"sh" <sham***@prupipe.com> schreef in bericht
news:PkmEg.6252$Qf.139@newsread2.news.pas.earthlink.net...
> The IndexOf string method returns the position of the first occurrence of
> a character in a string. Is there a method to find the x occurrence of a
> character, for example, the 28th occurrence of a character in a string?
>
> Thanks
Author
15 Aug 2006 5:18 PM
sh
Won't that give me what the 27th character is? I'm looking for the
position of the 27th occurrence of some character. For example

MyString = "12x34x56x78x90x12x34x56x78x90x"

So the position of the 7th occurrence of x is 21 (or 20 if zero based)

Cor Ligthert [MVP] wrote:
Show quoteHide quote
> Shamanda,
>
> I don't know if you are the same as gonzosez where is asked almost the same
> question in the second message after yours.
>
> \\\
> dim mystring as string = "I contain 70 characters"
> if mystring.length > 27 then
> dim TwentyEightchar = mystring(27)
> end if
> ///
> I hope this helps,
>
> Cor
>
> "sh" <sham***@prupipe.com> schreef in bericht
> news:PkmEg.6252$Qf.139@newsread2.news.pas.earthlink.net...
>
>>The IndexOf string method returns the position of the first occurrence of
>>a character in a string. Is there a method to find the x occurrence of a
>>character, for example, the 28th occurrence of a character in a string?
>>
>>Thanks
>
>
>
Author
15 Aug 2006 5:43 PM
Cor Ligthert [MVP]
Oh,

That is not to be done simple, you can use repeatly an indexof char, which
is many times quicker than a indexof from a string but you would have to
build code for that something as beneath not really tested see it as pseudo

\\\
dim mychar as char = "x"c
dim start as integer = 0
for x = 0 to 6
    if start < mystring.lenth
    start = mystring.indexof(start, mychar)
    else
    exit for
    end if
next
///

I hope this helps,

Cor

Show quoteHide quote
"sh" <sham***@prupipe.com> schreef in bericht
news:HlnEg.8225$xp2.2617@newsread1.news.pas.earthlink.net...
> Won't that give me what the 27th character is? I'm looking for the
> position of the 27th occurrence of some character. For example
>
> MyString = "12x34x56x78x90x12x34x56x78x90x"
>
> So the position of the 7th occurrence of x is 21 (or 20 if zero based)
>
> Cor Ligthert [MVP] wrote:
>> Shamanda,
>>
>> I don't know if you are the same as gonzosez where is asked almost the
>> same question in the second message after yours.
>>
>> \\\
>> dim mystring as string = "I contain 70 characters"
>> if mystring.length > 27 then
>> dim TwentyEightchar = mystring(27)
>> end if
>> ///
>> I hope this helps,
>>
>> Cor
>>
>> "sh" <sham***@prupipe.com> schreef in bericht
>> news:PkmEg.6252$Qf.139@newsread2.news.pas.earthlink.net...
>>
>>>The IndexOf string method returns the position of the first occurrence of
>>>a character in a string. Is there a method to find the x occurrence of a
>>>character, for example, the 28th occurrence of a character in a string?
>>>
>>>Thanks
>>
>>
Author
15 Aug 2006 5:26 PM
Chris Dunaway
Cor Ligthert [MVP] wrote:
>
> \\\
> dim mystring as string = "I contain 70 characters"
> if mystring.length > 27 then
> dim TwentyEightchar = mystring(27)
> end if
> ///

Cor, you've missed what he wants to achieve.  He wants to find the 28th
occurrence of a substring within a string.

One possible method (watch for typos):

Public Function FindNthOccurrence(source As String, substring As
String, occurrence As Integer) As Integer
    Dim index As Integer = -1
    For i As Integer = 0 to occurrence - 1
        index = source.IndexOf(substring, index + 1)
        If index = -1 Then
            Exit For
        End If
    Next

    Return index
End Function