Force std::string Width

Discussion in 'C++' started by Mike Copeland, Dec 17, 2011.

  1. Is there any std::string function that will either force the value of
    a variable to be no more than a maximum number of characters or fill it
    if it's less? For example:

    std::string someStr1 = "The Phantom Spitter was here.";

    where I want 20, but _no more_ than 20, characters to be displayed. But
    with:

    std::string someStr2 = "Kilroy here.";

    I want the output display to be 20 characters.
    In other words, I want a substr of a string value if it's >20
    characters, but I want to have the value "padded" when it's less than 20
    characters. I know that setw() will pad/fill the data, but is there
    something that will either fill or truncate a string to a specified
    length, depending on its length? TIA
    Mike Copeland, Dec 17, 2011
    #1
    1. Advertising

  2. Mike Copeland

    red floyd Guest

    On 12/16/2011 7:34 PM, Sam wrote:
    > Mike Copeland writes:
    >
    >> Is there any std::string function that will either force the value of
    >> a variable to be no more than a maximum number of characters or fill it
    >> if it's less? For example:
    >>
    >> std::string someStr1 = "The Phantom Spitter was here.";
    >>
    >> where I want 20, but _no more_ than 20, characters to be displayed. But
    >> with:
    >>
    >> std::string someStr2 = "Kilroy here.";
    >>
    >> I want the output display to be 20 characters.
    >> In other words, I want a substr of a string value if it's >20
    >> characters, but I want to have the value "padded" when it's less than 20
    >> characters. I know that setw() will pad/fill the data, but is there
    >> something that will either fill or truncate a string to a specified
    >> length, depending on its length? TIA

    >
    > s=(s+std::string(20, ' ')).substr(0, 20);
    >
    >


    Oh, that's a nice one! I'll have to remember that one!
    red floyd, Dec 17, 2011
    #2
    1. Advertising

  3. red floyd <> wrote:
    >> s=(s+std::string(20, ' ')).substr(0, 20);

    >
    > Oh, that's a nice one! I'll have to remember that one!


    Was that sarcasm? It's doing the same thing as s.resize(20, ' '), just
    a lot less efficiently.
    Juha Nieminen, Dec 17, 2011
    #3
  4. Mike Copeland

    red floyd Guest

    On 12/16/2011 11:44 PM, Juha Nieminen wrote:
    > red floyd<> wrote:
    >>> s=(s+std::string(20, ' ')).substr(0, 20);

    >>
    >> Oh, that's a nice one! I'll have to remember that one!

    >
    > Was that sarcasm? It's doing the same thing as s.resize(20, ' '), just
    > a lot less efficiently.


    Actually, no. It's been a while since I've done some serious C++
    (currently mostly C) and I'm a bit rusty. I had completely forgotten
    resize.
    red floyd, Dec 17, 2011
    #4
  5. red floyd <> wrote:
    > On 12/16/2011 11:44 PM, Juha Nieminen wrote:
    >> red floyd<> wrote:
    >>>> s=(s+std::string(20, ' ')).substr(0, 20);
    >>>
    >>> Oh, that's a nice one! I'll have to remember that one!

    >>
    >> Was that sarcasm? It's doing the same thing as s.resize(20, ' '), just
    >> a lot less efficiently.

    >
    > Actually, no. It's been a while since I've done some serious C++
    > (currently mostly C) and I'm a bit rusty. I had completely forgotten
    > resize.


    Admittedly the line is "clever" in that it demonstrates many of the
    features of std::string (in particular, and C++ in general). However,
    one should always know what is and isn't efficient, and if there might
    be a better way of doing it (in this particular case the better way is
    not only more efficient, but also shorter and simpler).

    (C++11 might make that line of code slightly more efficient thanks to
    move constructors, but it will still be significantly less so than a
    simple resize.)
    Juha Nieminen, Dec 19, 2011
    #5
    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. AndrewF
    Replies:
    1
    Views:
    741
    Bruce Barker
    Oct 10, 2005
  2. Peter Jansson
    Replies:
    5
    Views:
    6,265
    Ivan Vecerina
    Mar 17, 2005
  3. Vinu
    Replies:
    4
    Views:
    343
    Jim Langston
    Jul 7, 2005
  4. Fei Liu
    Replies:
    9
    Views:
    433
  5. Jeffrey Walton
    Replies:
    10
    Views:
    928
    Mathias Gaunard
    Nov 26, 2006
Loading...

Share This Page