473,385 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how can I replace a substring in a string

Hi,

If I have a string like this:
char buff[10];
buff[0] ='h';
buff[1] ='e';
buff[2] ='l';
buff[3] ='l';
buff[4] ='o';

string s(buff);

How can i replace 'll' with 'abc'
and what if I replace 'll' with 'a', will the 'o' move up by 1 index?

Thank you.

May 30 '07 #1
7 7940
si***************@gmail.com skrev:
Hi,

If I have a string like this:
char buff[10];
buff[0] ='h';
buff[1] ='e';
buff[2] ='l';
buff[3] ='l';
buff[4] ='o';
buff[5] = 0;
string s(buff);

How can i replace 'll' with 'abc'
and what if I replace 'll' with 'a', will the 'o' move up by 1 index?
replace(s, "ll", "abc");

void replace(std::string &target, std::string &that, std::string &with) {
std::string::size_type where = target.find(that);
if(where != std::string::npos) {
target.replace(target.begin() + where,
target.begin() + where + that.size(),
with.begin(),
with.end());
}
}

--
OU
May 30 '07 #2
On May 30, 10:04 am, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
Hi,

If I have a string like this:
char buff[10];
buff[0] ='h';
buff[1] ='e';
buff[2] ='l';
buff[3] ='l';
buff[4] ='o';
You need to insert a null character at postion 5.
string s(buff);

How can i replace 'll' with 'abc'
s.replace(2,2,"abc");
and what if I replace 'll' with 'a', will the 'o' move up by 1 index?
yes
>
Thank you.

May 30 '07 #3
si***************@gmail.com wrote:
Hi,

If I have a string like this:
char buff[10];
buff[0] ='h';
buff[1] ='e';
buff[2] ='l';
buff[3] ='l';
buff[4] ='o';

string s(buff);

How can i replace 'll' with 'abc'
and what if I replace 'll' with 'a', will the 'o' move up by 1 index?

Thank you.
Here are all the string class member functions:

http://cppreference.com/cppstring/index.html
May 30 '07 #4
On Wed, 30 May 2007 19:30:52 +0200, Obnoxious User wrote:
[...]
>void replace(std::string &target, std::string &that, std::string &with) {
const correctness?
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
May 30 '07 #5
Roland Pibinger skrev:
On Wed, 30 May 2007 19:30:52 +0200, Obnoxious User wrote:
[...]
>void replace(std::string &target, std::string &that, std::string &with) {

const correctness?

void replace(std::string & target,
std::string const & that,
std::string const & with);

--
OU
May 30 '07 #6

<si***************@gmail.comwrote in message news:11*********************@u30g2000hsc.googlegro ups.com...
Hi,

If I have a string like this:
char buff[10];
buff[0] ='h';
buff[1] ='e';
buff[2] ='l';
buff[3] ='l';
buff[4] ='o';
Triply wrong:
1. Firstly, null-terminated strings need to be, well, null-terminated.
So you'd need to set:
buff[5] = '\0';
2. Why not just initialize the buffer to the string?
char buff[10] = "hello";
3. Why use a char[] buffer at all? Just do this:
std::string MyString ("hello");
string s(buff);
std::string
How can i replace 'll' with 'abc'
Like so (tested, working program; compile this for demo):

#include <iostream>
#include <string>
int main (void)
{
std::string S ("hello");
S.replace(S.find("ll"), 2, "abc"); // replace 2 chars at "ll" by "abc"
std::cout << S << std::endl;
return 0;
}
and what if I replace 'll' with 'a', will the 'o' move up by 1 index?
It decrements by one, from 4 to 3.
Thank you.
You're welcome.

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org
May 31 '07 #7

"Obnoxious User" <OU@127.0.0.1wrote in message
news:46***********************@alt.teranews.com...
replace(s, "ll", "abc");

void replace(std::string &target, std::string &that, std::string &with) {
std::string::size_type where = target.find(that);
if(where != std::string::npos) {
target.replace(target.begin() + where,
target.begin() + where + that.size(),
with.begin(),
with.end());
}
}
Ewww. I could replace all of the above with just this:

s.replace(s.find("ll"), 2, "abc");

or more generically:

s.replace(s.find(target), target_size, replacement);

Far more convenient, and far less prone to error. "find" and "replace" are
already built into the std::string class, so might as well use them.

--
Cheers,
Robbie Hatley
lone wolf aatt well dott com
triple-dubya dott Tustin Free Zone dott org
May 31 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: K.Simon | last post by:
Hello, it's very often neccessary to replace strings or a single character in my stylesheets. My solution looks awful and very long. Now i thought to solve this with an array like structure but...
5
by: mp | last post by:
when I write this in c# : strFileToLoad = strFileToLoad.Substring(0, 2) + strFileToLoad.Substring(2).Replace("\\", "\"); it doesnt like the last "\", and I want to replace \\ with \ what...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
10
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal...
1
by: MCH | last post by:
Hi there, I am using std:string and try to replace a substring with replace function. From the document, I found one prototype of string.replace basic_string& replace(iterator first, iterator...
1
by: coolami4u | last post by:
I need a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in main. The first function prompts the user to type a string of...
8
by: Johny | last post by:
Let's suppose s='12345 4343 454' How can I replace the last '4' character? I tried string.replace(s,s,'r') where 'r' should replace the last '4'. But it doesn't work. Can anyone explain why?...
9
by: Mark Szlazak | last post by:
I don't think this is "do-able" but thought I'd better check. Say I want to replace certain names in some source code as long as they are not properties (dot properties) of objects. I could use a...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.