Solved

Appending strings onto a string

Posted on 2004-10-18
5
241 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

22 Experts available now in Live!

Get 1:1 Help Now