Solved

Appending strings onto a string

Posted on 2004-10-18
5
242 Views
Last Modified: 2010-04-01
I am trying to do the following:
If there is a match, I need to append a string onto an existing one.
How is the implementation done using std strings?

std::string fscript;

for .....
{
   //if match
    add on a string and " " to end of existing string...

0
Comment
Question by:jewee
5 Comments
 
LVL 5

Accepted Solution

by:
allmer earned 250 total points
ID: 12338570
The simplest way would be to
simply add the new string to fscript in the loop:
for (..do whatever..) {
    ....
    fscript += yourHit;
}

Also look here:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20980456.html
if there are speed, performence issues attached.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 12338917
Apart from using the operator '+=', 'std::string' also has a member function 'append()' which pretty much does what the name suggests.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 50 total points
ID: 12339483
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12340208
>> add on a string and " " to end of existing string...

maybe it's better to add space character first ...

   std::string fscript, word;
 
   while ( !end )
   {
      ...
      word = nextWord();
      if ( match )
      {
           fscript += ' ';
           fscript += word;
      }
    }

Note, you cannot not do it by one statement

    fscript += (' ' + word);   // compile error

as std::string has no operator + (..) but only operator += (..) .

Regards, Alex





0
 
LVL 1

Assisted Solution

by:gugario
gugario earned 150 total points
ID: 12340440
Alex, your solution is good... but you should be able to do
     fscript += " " + word;

I just tried in g++ on my pc and it worked like that.  

Weird, eh?

Gustavo.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now