Prepend to basic::string

Discussion in 'C++' started by Mike Copeland, Jan 20, 2012.

  1. 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
     
    Mike Copeland, Jan 20, 2012
    #1
    1. Advertising

  2. On Jan 19, 4:51 pm, (Mike Copeland) wrote:
    >    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.
     
    Joshua Maurice, Jan 20, 2012
    #2
    1. Advertising

  3. Mike Copeland

    Henrik Faber Guest

    On 20.01.2012 13:09, Leigh Johnston wrote:

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

    >
    > 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
     
    Henrik Faber, Jan 20, 2012
    #3
  4. Mike Copeland

    gwowen Guest

    On Jan 20, 1:17 pm, Henrik Faber <> wrote:
    > On 20.01.2012 13:09, Leigh Johnston wrote:


    > > 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


    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.
     
    gwowen, Jan 20, 2012
    #4
  5. Mike Copeland

    Goran Guest

    On Jan 20, 1:51 am, (Mike Copeland) wrote:
    >    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.
     
    Goran, Jan 20, 2012
    #5
  6. On 1/19/2012 7:51 PM, Mike Copeland wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 20, 2012
    #6
  7. In article <>,
    says...
    > On 20/01/2012 13:17, Henrik Faber wrote:
    > > On 20.01.2012 13:09, Leigh Johnston wrote:
    > >
    > >>>> string str1;
    > >>>> str1 = "swim"; // this data varies
    > >>>> if(str1.length()< 7)
    > >>>> {
    > >>>> // insert blanks in front of str1 to produce " swim"
    > >>>> }
    > >>
    > >> 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).

    >
    > 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...
     
    Mike Copeland, Jan 20, 2012
    #7
  8. Mike Copeland

    Jorgen Grahn Guest

    On Fri, 2012-01-20, Mike Copeland wrote:
    ....
    > 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jan 21, 2012
    #8
  9. Mike Copeland

    Goran Guest

    On Jan 21, 12:01 am, (Mike Copeland) wrote:
    > In article <>,
    > says...
    >
    >
    >
    >
    >
    >
    >
    > > On 20/01/2012 13:17, Henrik Faber wrote:
    > > > On 20.01.2012 13:09, Leigh Johnston wrote:

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

    >
    > > >> 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).

    >
    > > 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...


    ;-)

    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.
     
    Goran, Jan 21, 2012
    #9
  10. Mike Copeland

    Henrik Faber Guest

    On 21.01.2012 00:01, Mike Copeland wrote:

    > 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
     
    Henrik Faber, Jan 23, 2012
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    2,216
  2. flamesrock

    how to prepend string to a string?

    flamesrock, May 31, 2005, in forum: Python
    Replies:
    6
    Views:
    16,530
    newcoder
    May 31, 2005
  3. Replies:
    37
    Views:
    2,099
    Flash Gordon
    Jan 9, 2006
  4. dspfun

    Prepend name to function names?

    dspfun, Jan 29, 2008, in forum: C Programming
    Replies:
    8
    Views:
    894
    dspfun
    Jan 29, 2008
  5. bdb112
    Replies:
    6
    Views:
    4,386
    Robert Kern
    Jul 25, 2009
Loading...

Share This Page