473,698 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7957
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::st ring &target, std::string &that, std::string &with) {
std::string::si ze_type where = target.find(tha t);
if(where != std::string::np os) {
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.comwr ote:
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::st ring &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::st ring &target, std::string &that, std::string &with) {

const correctness?

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

--
OU
May 30 '07 #6

<si************ ***@gmail.comwr ote in message news:11******** *************@u 30g2000hsc.goog legroups.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.fin d("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.1wr ote in message
news:46******** *************** @alt.teranews.c om...
replace(s, "ll", "abc");

void replace(std::st ring &target, std::string &that, std::string &with) {
std::string::si ze_type where = target.find(tha t);
if(where != std::string::np os) {
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.fin d("ll"), 2, "abc");

or more generically:

s.replace(s.fin d(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
21690
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 i found no solution. The original code looks like the following: <!-- The replacement-function that i'm calling --> <xsl:template name="replace-string">
5
39647
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 should I do? Thanks
4
3850
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, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
10
9601
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 Replacement As String) As String '... End Function
1
6710
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 last, const basic_string& s) I am going to replace the substring specified by first and last and replace it with s, where s may longer or short than the substring being replaced. If the replace function suitable for my case ? Anyway, I use
1
2898
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 fewer than 80 characters. It then prompts the user to type the search-substring of 10 or fewer characters. Finally, it prompts the user to type the replace-substring of 10 or fewer characters. The second function call is the search-and-replace...
8
80274
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? Thanks
9
3787
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 regular expression like: rx = /(?:(\.)|\b)(?:name1|name2|name3)\b/g; map = ; map = "a"; map = "b";
3
3959
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 below and it works fine as long as I'm not adding tags around the to parameter. Can anyone explain to me why it doesn't work with tags? And it needs to be XSLT 1.0. This works: X<xsl:value-of select="'little steak'"/>X This doesn't work:...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.