Prepend to basic::string

M

Mike Copeland

Is there a type-safe and simple way to prepend some data to a string?
For example, I have:

string str1;
str1 = "swim"; // this data varies
if(str1.length() < 7)
{
// insert blanks in front of str1 to produce " swim"
}

Please advise. TIA
 
J

Joshua Maurice

   Is there a type-safe and simple way to prepend some data to a string?
For example, I have:

    string str1;
    str1 = "swim";  // this data varies
    if(str1.length() < 7)
    {
        // insert blanks in front of str1 to produce "   swim"
    }

   Please advise.  TIA

Arguably the simplest is:

string str1;
str1 = "swim"; // this data varies
if(str1.length() < 7)
{
str1 = " " + str1;
// insert blanks in front of str1 to produce " swim"
}

As mentioned else-thread, .insert() would work too. It may be (?) more
efficient as well.
 
H

Henrik Faber

That is not a solution as the requirement is that "this data varies" and
your " " doesn't.

Read the problem again. "swim" is the data that may vary. The spaces do
not. Joshua's solution is perfectly fine (and perfectly obvious).

Regards,
Henrik
 
G

gwowen

On 20.01.2012 13:09, Leigh Johnston wrote:

Read the problem again. "swim" is the data that may vary. The spaces do
not. Joshua's solution is perfectly fine (and perfectly obvious).

Regards,
Henrik

Gentlemen,

The problem is not precisely specified. Both interpretations are
possible, and anyone getting shouty, sweary or sarcastic over which
interpretation was intended needs to take a good hard look at
themselves.

Or become a Fundamentalist theologian.

Either way, calm the hell down, you're making yourselves look silly.
 
G

Goran

   Is there a type-safe and simple way to prepend some data to a string?
For example, I have:

    string str1;
    str1 = "swim";  // this data varies
    if(str1.length() < 7)
    {
        // insert blanks in front of str1 to produce "   swim"
    }

Formatting stuff, huh? If so...

string right_aligned(const std::string& s, streamsize size)
{
stringstream sss;
sss.width(size);
sss << s;
return sss.str();
}

Goran.
 
V

Victor Bazarov

Is there a type-safe and simple way to prepend some data to a string?

What do you mean by "type-safe"? What would be a "type-unsafe" way?
For example, I have:

string str1;
str1 = "swim"; // this data varies
if(str1.length()< 7)
{
// insert blanks in front of str1 to produce " swim"
}

Please advise. TIA

Something like

if (str1.length() < 7)
str1.insert(0, 7 - str1.length(), ' ');

?

V
 
M

Mike Copeland

Bullshit; to me it is "obvious" that if str1 can vary and the length
check is less than 7 then the length of str1 can vary ergo the number of
required spaces can vary.
As the OP, I apologize for not stating the problem better: I want the
resulting string to be _7_characters_in_length_, right-justified, blank
filled as necessary. In the example, I want "swim" to be preceded by 3
blanks, "run" to be padded with 4 blanks, etc.
My query was focused on learning if there was a way to "shift a
string to the right and pad with blanks" (a la` C-style sprintf with
"%-7s") - I was hoping for some sort of "left padding" capability like
that...
 
J

Jorgen Grahn

.
As the OP, I apologize for not stating the problem better: I want the
resulting string to be _7_characters_in_length_, right-justified, blank
filled as necessary. In the example, I want "swim" to be preceded by 3
blanks, "run" to be padded with 4 blanks, etc.
My query was focused on learning if there was a way to "shift a
string to the right and pad with blanks" (a la` C-style sprintf with
"%-7s") - I was hoping for some sort of "left padding" capability like
that...

std::string has nothing like that -- formatting lives in the
iostreams. You can format to a std::stringstream if you want.

Personally I often use sprintf() and wrap it into a function which
translates back to std::string, if I can be sure that I can make the
intermediate buffer long enough.

/Jorgen
 
G

Goran

   As the OP, I apologize for not stating the problem better: I want the
resulting string to be _7_characters_in_length_, right-justified, blank
filled as necessary.  In the example, I want "swim" to be preceded by 3
blanks, "run" to be padded with 4 blanks, etc.
   My query was focused on learning if there was a way to "shift a
string to the right and pad with blanks" (a la` C-style sprintf with
"%-7s") - I was hoping for some sort of "left padding" capability like
that...

;-)

So your question is as mundane as "how to right-align a string". But
you used words such as "type-safe way".

Tsk, tsk, tsk...

Goran.
 
H

Henrik Faber

My query was focused on learning if there was a way to "shift a
string to the right and pad with blanks" (a la` C-style sprintf with
"%-7s") - I was hoping for some sort of "left padding" capability like
that...

(boost::format("%-7s") % str1).str()

Best regards,
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top