Results 1 to 7 of 7

Thread: [2005] Howto search through a string

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    9

    [2005] Howto search through a string

    I am trying to search for a certain string inside another string. Thanks alot for any help

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: [2005] Howto search through a string

    you can use [string].Contains

    vb Code:
    1. Dim string1 As String = "some text"
    2. MsgBox(string1.Contains("text"))

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2005] Howto search through a string

    You can also use the IndexOf method or Regular Expressions. If you just purely need to see if a string contains another string then Paul's method will work fine but if you need to find that string (within the original string) and then manipulate it or get its position etc then the IndexOf or RegEx methods may be better.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    9

    Re: [2005] Howto search through a string

    What's the IndexOf method?

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2005] Howto search through a string

    From MSDN:
    Reports the index of the first occurrence of the specified Unicode character in this string.
    Usage:
    Code:
    Dim myStringVariable as string = "Hello World"
    Messagebox.Show(myStringVariable.IndexOf("World").ToString())

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: [2005] Howto search through a string

    The string class exposes a lot of different methods to help with this:
    Code:
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
            Dim myStringVariable As String = "Hello World"
    
            ' Using StartsWith method:
            MessageBox.Show("String starts with word 'Hello': " & _
                            myStringVariable.StartsWith("Hello"))
    
            ' Using IndexOf method:
            MessageBox.Show("Position of word 'World' " & _
                            "in the string is: " & _
                            myStringVariable.IndexOf("World"))
    
            ' Using LastIndexOf method:
            MessageBox.Show("Last Position of word 'World' " & _
                            "in the string is: " & _
                            myStringVariable.LastIndexOf("World"))
    
            ' Using Contains method:
            MessageBox.Show("String contains word 'World': " & _
                            myStringVariable.Contains("World").ToString)
    
            ' Using Regular Expressions:
            Dim worldSearchregex As New  _
            System.Text.RegularExpressions.Regex("([.|\n]*)World([.|\n]*)")
            MessageBox.Show("String includes 'World' expression: " & _
                            worldSearchregex.Match(myStringVariable).Success.ToString)
    
            ' Using Equals method:
            MessageBox.Show("String equals text 'Hello World': " & _
                            myStringVariable.Equals("Hello World").ToString)
        End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    9

    Re: [2005] Howto search through a string

    This is very helpful, Thanks alot.

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