Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Appending strings onto a string

Posted on 2004-10-18
5
Medium Priority
?
249 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 1000 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 200 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 200 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 600 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

721 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