Results 1 to 8 of 8

Thread: [2005] findstring(IndexOf)

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    [2005] findstring(IndexOf)

    Hi is there any function that would return the indexOf all apples in this string :

    string = orange apple apple orange grape grape apple orange

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] findstring(IndexOf)

    I believe you need to writh a function to loop through the string and get the indexes.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Re: [2005] findstring(IndexOf)

    I dont know how, that's why i need help..

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] findstring(IndexOf)

    It would be relatively inefficient but Regex.Matches would do the job in one go. I'd be inclined to write a method call IndexOf repeatedly and return the results in a single array or collection and then call that method as needed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] findstring(IndexOf)

    this is very simple stuf so you should do it your self.
    VB Code:
    1. Private Function GetIndexs(ByVal str As String, ByVal exp As String) As Integer()
    2.         Dim indexes As New List(Of Integer)
    3.         Dim helper, index As Integer
    4.         While str <> "" AndAlso helper <> -1
    5.             helper = str.Substring(index).IndexOf(exp)
    6.             If helper <> -1 Then
    7.                 index = helper + index
    8.                 indexes.Add(index)
    9.                 If index + 1 < str.Length - 1 Then
    10.                     index += 1
    11.                 End If
    12.             End If
    13.         End While
    14.         Return indexes.ToArray
    15.     End Function

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] findstring(IndexOf)

    IndexOf is overloaded:
    VB Code:
    1. Private Function IndexesOf(ByVal str As String, ByVal substr As String) As Integer()
    2.     Dim indexes As New List(Of Integer)
    3.     Dim index As Integer = 0
    4.  
    5.     While index < str.Length
    6.         index = str.IndexOf(substr, index)
    7.  
    8.         If index = -1 Then
    9.             Exit While
    10.         End If
    11.  
    12.         indexes.Add(index)
    13.         index += 1
    14.     End While
    15.  
    16.     Return indexes.ToArray()
    17. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] findstring(IndexOf)

    Yes that defiantly looks cleaner.

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Re: [2005] findstring(IndexOf)

    this is great, thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width