Replace Between

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • disent
    New Member
    • May 2010
    • 3

    #1

    Replace Between

    Hello,

    I should tell you I don't know much about C++ before i get started.

    Ok, what I need is a way to replace some letters between the marks i choose.

    For example:

    string str1 = "whatsup<1> the string<2>hello" ;

    I call a function that replaces everything between "<1>" and "<2>" with "is gone".

    str1 now becomes "whatsup<1> is gone<2>hello";

    the length of the string will always be different and so i can't just tell it to start removing at some static position from the start.

    What I came up with is maybe make a loop that goes over each letter in the string and looking for <1> while counting how many letters have past and then count the distance between <1> and <2>. Delete the letters between those two some how and then use some replace method with the "position where to start replacing" method and replace the blank spot between the two.

    But then again I might be an idiot so please give me some ideas.
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    string member functions

    Below is a link to a reference on the string class in C++. If I were you I would take a look at the find and replace methods that you can use for string manipulation.


    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      C++ has a number or functions for manipulating strings.e.g.
      Code:
      string s="ABCDEFGH"
      s1=s.erase(4,2)//s1=ABCDGH
      s2=s1.replace3,2,"xyz")//s2=ABCxyzH
      Any text book will cover this.

      Comment

      Working...