Results 1 to 5 of 5

Thread: [RESOLVED] finding a word in a string

  1. #1

    Thread Starter
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Resolved [RESOLVED] finding a word in a string

    Hi,

    How to find a substring inside the string in c# and return true if found?
    ex.
    Manila, Philipines = Phils343 = true
    Manila, Philipines = Manila = true
    Manila, Philippines = phili= true
    Manila, Philippines = Cebu= false

    thanks,
    Popskie

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: finding a word in a string

    Use string.IndexOf method. Something similar to this
    VB Code:
    1. someName = "Manila, Philipines";
    2.             if (someName.IndexOf("Phili") >= 0)
    3.                 MessageBox.Show ("True");
    4.             else
    5.                 MessageBox.Show ("False");
    Use [code] source code here[/code] tags when you post source code.

    My Articles

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

    Re: finding a word in a string

    If it's C# 2.0 (please specify in future) and you don't actually want the index then use String.Contains.
    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

  4. #4

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: finding a word in a string

    Quote Originally Posted by jmcilhinney
    If it's C# 2.0 (please specify in future) and you don't actually want the index then use String.Contains.
    Contains() is just a bool wrapper for IndexOf(). Use IndexOf when its a speed intensive bit of code.
    I don't live here any more.

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