Solved

Appending strings onto a string

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

756 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