Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #12607
| From | red floyd <no.spam.here@its.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Force std::string Width |
| Date | 2011-12-16 20:15 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <jch51d$hs$1@dont-email.me> (permalink) |
| References | <MPG.295587d5a83cdb249896e2@news.eternal-september.org> <cone.1324092843.324977.24565.1000@monster.email-scan.com> |
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!
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Force std::string Width mrc2323@cox.net (Mike Copeland) - 2011-12-16 19:08 -0700
Re: Force std::string Width Sam <sam@email-scan.com> - 2011-12-16 21:34 -0600
Re: Force std::string Width red floyd <no.spam.here@its.invalid> - 2011-12-16 20:15 -0800
Re: Force std::string Width Juha Nieminen <nospam@thanks.invalid> - 2011-12-17 07:44 +0000
Re: Force std::string Width red floyd <no.spam.here@its.invalid> - 2011-12-17 09:17 -0800
Re: Force std::string Width Juha Nieminen <nospam@thanks.invalid> - 2011-12-19 10:18 +0000
Re: Force std::string Width Paavo Helde <myfirstname@osa.pri.ee> - 2011-12-16 23:58 -0600
csiph-web