Solved

Appending strings onto a string

Posted on 2004-10-18
5
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

624 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