Results 1 to 5 of 5

Thread: Removing first occurrence of string within a string?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    6

    Question Removing first occurrence of string within a string?

    Hi everyone

    I'm wondering how i could remove the first occurrence of a set of characters from a string, and then looping until there are no characters left in the string.

    For example:
    I want to remove the first "wer" from "qwertyqwertyqwerty", so that the string then reads "qtyqwertyqwerty".

    Any ideas on how to do that?

    EDIT: Clarified example.
    Last edited by h33t; Oct 23rd, 2012 at 03:35 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Removing first occurrence of string within a string?

    string.replace()

    Code:
    Dim str As String = "qwerty"
            Console.WriteLine(str.Replace("wer", ""))
            Console.ReadLine()
    generally when you have a question about string manipulation the first place to look is msdn.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Removing first occurrence of string within a string?

    vb.net Code:
    1. Dim testString As String = "Chumbawumba"
    2.         MsgBox(Replace(testString, "umb", "", 1, 1)) 'source, search string, replacement, start, number of repetitions
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    6

    Re: Removing first occurrence of string within a string?

    Sorry, I meant if the substring you want to remove occurs more than once in a string. Like removing the first "wer" from "qwertyqwertyqwerty".

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Removing first occurrence of string within a string?

    Quote Originally Posted by h33t View Post
    Sorry, I meant if the substring you want to remove occurs more than once in a string. Like removing the first "wer" from "qwertyqwertyqwerty".
    Yes, I know. That's what my code does. If you'd tried it you would know that it changes Chumbawumba to Chawumba.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Tags for this Thread

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