string::replace() acting strangely

Hi!

So, I'm trying to use the string::replace() function in a program. My program as a whole wasn't working, so i created an absolutely minimal program consisting of only one line of code (inside the main() function; I won't put braces on a single line). It is like this:

cout << string("**********").replace(2,5,"......") << endl;

(the string of '.'s is intentionally 6 characters long)

The output I was expecting was:
**......**

Instead, I got:
**......***

as if all characters after the 5th in the replace() call were inserted, instead of replacing....

Any ideas?

Sincerely,
Jakykong (Jack Mudge)
jack_mudge@hotmail.com

Comments

  • I would guess, it replaces the last character with all the remaining characters in the replace list, but perhaps you should fiddle around with this.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : Hi!
    :
    : So, I'm trying to use the string::replace() function in a program.
    : My program as a whole wasn't working, so i created an absolutely
    : minimal program consisting of only one line of code (inside the
    : main() function; I won't put braces on a single line). It is like
    : this:
    :
    : cout << string("**********").replace(2,5,"......") << endl;
    :
    : (the string of '.'s is intentionally 6 characters long)
    :
    : The output I was expecting was:
    : **......**
    :
    : Instead, I got:
    : **......***
    :
    : as if all characters after the 5th in the replace() call were
    : inserted, instead of replacing....
    :
    : Any ideas?
    :
    : Sincerely,
    : Jakykong (Jack Mudge)
    : jack_mudge@hotmail.com


    [blue]It's not behaving strangely, that is exactly what is supposed to happen with that overloaded version of the [italic]replace[/italic] member function. It replaces 5 characters of the string beginning at index 2 (the third character) with whatever is in the third argument (the 6 '.' characters). You could get your "expected" output by calling:

    [code]cout << string("**********").replace(2,[blue]6[/blue],"......") << endl;[/code][/blue]
  • : : Hi!
    : :
    : : So, I'm trying to use the string::replace() function in a program.
    : : My program as a whole wasn't working, so i created an absolutely
    : : minimal program consisting of only one line of code (inside the
    : : main() function; I won't put braces on a single line). It is like
    : : this:
    : :
    : : cout << string("**********").replace(2,5,"......") << endl;
    : :
    : : (the string of '.'s is intentionally 6 characters long)
    : :
    : : The output I was expecting was:
    : : **......**
    : :
    : : Instead, I got:
    : : **......***
    : :
    : : as if all characters after the 5th in the replace() call were
    : : inserted, instead of replacing....
    : :
    : : Any ideas?
    : :
    : : Sincerely,
    : : Jakykong (Jack Mudge)
    : : jack_mudge@hotmail.com
    :
    :
    : [blue]It's not behaving strangely, that is exactly what is supposed
    : to happen with that overloaded version of the
    : [italic]replace[/italic] member function. It replaces 5 characters
    : of the string beginning at index 2 (the third character) with
    : whatever is in the third argument (the 6 '.' characters). You could
    : get your "expected" output by calling:
    :
    : [code]: cout << string("**********").replace(2,[blue]6[/blue],"......") << endl;[/code]: [/blue]


    So... is there some standard C++ function to replace a certain number of characters and no more?

    What I'm looking for is to do some fixed-width editing, for each row of a table. There is a text field 20 characters long, and I get a string from the user -- it may be less or more than 20 characters.
    i was hoping string::replace() would replace UP TO 20 characters, but no more, and not overwrite anything that wasn't replaced.

    I suppose I may be stuck fiddling with substrings...
    Sincerely,
    Jakykong (Jack Mudge)
    jack_mudge@hotmail.com
  • : : : Hi!
    : : :
    : : : So, I'm trying to use the string::replace() function in a program.
    : : : My program as a whole wasn't working, so i created an absolutely
    : : : minimal program consisting of only one line of code (inside the
    : : : main() function; I won't put braces on a single line). It is like
    : : : this:
    : : :
    : : : cout << string("**********").replace(2,5,"......") << endl;
    : : :
    : : : (the string of '.'s is intentionally 6 characters long)
    : : :
    : : : The output I was expecting was:
    : : : **......**
    : : :
    : : : Instead, I got:
    : : : **......***
    : : :
    : : : as if all characters after the 5th in the replace() call were
    : : : inserted, instead of replacing....
    : : :
    : : : Any ideas?
    : : :
    : : : Sincerely,
    : : : Jakykong (Jack Mudge)
    : : : jack_mudge@hotmail.com
    : :
    : :
    : : [blue]It's not behaving strangely, that is exactly what is supposed
    : : to happen with that overloaded version of the
    : : [italic]replace[/italic] member function. It replaces 5 characters
    : : of the string beginning at index 2 (the third character) with
    : : whatever is in the third argument (the 6 '.' characters). You could
    : : get your "expected" output by calling:
    : :
    : : [code]: : cout << string("**********").replace(2,[blue]6[/blue],"......") << endl;[/code]: : [/blue]
    :
    :
    : So... is there some standard C++ function to replace a certain
    : number of characters and no more?
    :
    : What I'm looking for is to do some fixed-width editing, for each row
    : of a table. There is a text field 20 characters long, and I get a
    : string from the user -- it may be less or more than 20 characters.
    : i was hoping string::replace() would replace UP TO 20 characters,
    : but no more, and not overwrite anything that wasn't replaced.
    :
    : I suppose I may be stuck fiddling with substrings...
    : Sincerely,
    : Jakykong (Jack Mudge)
    : jack_mudge@hotmail.com


    [blue]Give an example of a string you've got, some potential strings a user might enter, and the expected results you want to get. There are several variations of this member function, one of them may be more appropriate to use in your situation.[/blue]
  • : : : : Hi!
    : : : :
    : : : : So, I'm trying to use the string::replace() function in a program.
    : : : : My program as a whole wasn't working, so i created an absolutely
    : : : : minimal program consisting of only one line of code (inside the
    : : : : main() function; I won't put braces on a single line). It is like
    : : : : this:
    : : : :
    : : : : cout << string("**********").replace(2,5,"......") << endl;
    : : : :
    : : : : (the string of '.'s is intentionally 6 characters long)
    : : : :
    : : : : The output I was expecting was:
    : : : : **......**
    : : : :
    : : : : Instead, I got:
    : : : : **......***
    : : : :
    : : : : as if all characters after the 5th in the replace() call were
    : : : : inserted, instead of replacing....
    : : : :
    : : : : Any ideas?
    : : : :
    : : : : Sincerely,
    : : : : Jakykong (Jack Mudge)
    : : : : jack_mudge@hotmail.com
    : : :
    : : :
    : : : [blue]It's not behaving strangely, that is exactly what is supposed
    : : : to happen with that overloaded version of the
    : : : [italic]replace[/italic] member function. It replaces 5 characters
    : : : of the string beginning at index 2 (the third character) with
    : : : whatever is in the third argument (the 6 '.' characters). You could
    : : : get your "expected" output by calling:
    : : :
    : : : [code]: : : cout << string("**********").replace(2,[blue]6[/blue],"......") << endl;[/code]: : : [/blue]
    : :
    : :
    : : So... is there some standard C++ function to replace a certain
    : : number of characters and no more?
    : :
    : : What I'm looking for is to do some fixed-width editing, for each row
    : : of a table. There is a text field 20 characters long, and I get a
    : : string from the user -- it may be less or more than 20 characters.
    : : i was hoping string::replace() would replace UP TO 20 characters,
    : : but no more, and not overwrite anything that wasn't replaced.
    : :
    : : I suppose I may be stuck fiddling with substrings...
    : : Sincerely,
    : : Jakykong (Jack Mudge)
    : : jack_mudge@hotmail.com
    :
    :
    : [blue]Give an example of a string you've got, some potential strings
    : a user might enter, and the expected results you want to get. There
    : are several variations of this member function, one of them may be
    : more appropriate to use in your situation.[/blue]



    I am constructing a table, so I was thinking of starting out with
    " | | |"
    this represents an empty row. The first field (In this case) is a name, the third field is the number of hours spent, the second field is the price (if the first field describes an object), or the hourly wage (if the third field has a value).

    So, if I'm given a string like:
    "Replace an empty garbage can", which is longer than the first field, it should replace the spaces in the first field with the first 20 characers of the string. If the string is less than 20 characters, I want to leave the extra spaces intact (they help with formatting.)

    So, it should end up as
    "Replace an empty gar| | |"


    Sincerely,
    Jakykong (Jack Mudge)
    jack_mudge@hotmail.com
  • You should look into the boost libraries.

    www.boost.org

    There is a string algorithms library that might help. In addition, the regex library contains powerful find and replace (regex / sed-style) functionality.

    Good luck!
    Matt.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories