CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2005
    Posts
    568

    find substring in a string

    is string provide any function that allow to calculate there is how many substring inside a string??
    example: string str = "how are you?"
    is it possible to tell how many "o" inside the str?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: find substring in a string

    Use the substring() command with a variable. when you find the string, move the variable past it, and then LOOP until you don't find it again.

    that's actually pseudo-code that will solve the problem
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: find substring in a string

    Another way would be to use Regular expressions.
    Code:
                string str = "how are you?";
                System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("o");
                int i = rx.Matches(str).Count;

  4. #4
    Join Date
    Apr 2006
    Posts
    220

    Re: find substring in a string

    Quote Originally Posted by Shuja Ali
    Another way would be to use Regular expressions.
    Code:
                string str = "how are you?";
                System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("o");
                int i = rx.Matches(str).Count;
    Excellent.

  5. #5
    Join Date
    Feb 2005
    Posts
    568

    Re: find substring in a string

    Quote Originally Posted by Shuja Ali
    Another way would be to use Regular expressions.
    Code:
                string str = "how are you?";
                System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("o");
                int i = rx.Matches(str).Count;
    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

Featured