How do I find one string inside another?

Discussion in 'C#' started by dylanj, Jul 11, 2008.

  1. #1
    As above.

    For example, if I wanted to find "hi" inside "lorem ipsum hi amet". I know ASP.Net has a function for this - Contains().

    Thanks in advance.
     
    dylanj, Jul 11, 2008 IP
  2. dylanj

    dylanj Peon

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Nevermind. Found it - InStr()
     
    dylanj, Jul 11, 2008 IP
  3. VishalVasani

    VishalVasani Peon

    Messages:
    560
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,

    Following is an example:
    
    Dim SearchString, SearchChar, MyPos
    SearchString ="XXpXXpXXPXXP"   ' String to search in.
    SearchChar = "P"   ' Search for "P".
    MyPos = InstrRev(SearchString, SearchChar, 10, 0)   ' A binary comparison starting at position 10. Returns 9.
    MyPos = InstrRev(SearchString, SearchChar, -1, 1)   ' A textual comparison starting at the last position. Returns 12.
    MyPos = InstrRev(SearchString, SearchChar, 8)   ' Comparison is binary by default (last argument is omitted). Returns 0.
    
    Code (markup):
    You can also used InStrRev Function
    Returns the position of an occurrence of one string within another, from the end of string.
     
    VishalVasani, Jul 13, 2008 IP