|
Boost Users : |
Subject: Re: [Boost-users] Concatenating int value to string ..??
From: Mukkaysh Srivastav (srimks11_at_[hidden])
Date: 2013-11-03 22:00:15
Try this it should work -
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <boost/lexical_cast.hpp>
using namespace std;
int main() {
std::string APPLE = "Apple", AppleID = "01", APPLEID, FINAL_PRINT;
int age = 1;
APPLEID = APPLE + boost::lexical_cast<std::string>(AppleID);
std::cout << "APPLEID .." << APPLEID << endl;
std::string age_s = boost::lexical_cast<std::string>(age);
string dest = std::string( 5, '0').append(age_s);
FINAL_PRINT = APPLEID + boost::lexical_cast<std::string>(dest);
std::cout << "FINAL PRINT .." << FINAL_PRINT << endl;
}
Good luck !!
On Mon, Nov 4, 2013 at 6:13 AM, Gavin Lambert <gavinl_at_[hidden]> wrote:
> On 3/11/2013 01:12, Quoth Rahul Mathur:
>
>> YES, the string should contain a valid integer. Thanks all ..!!
>>
>
> Actually the string must consist entirely of a valid integer, not merely
> contain one. Any non-numeric characters are invalid.
>
> 1. One string as "Apple"
>> 2. Another string or int value as "01" or 1.
>> 3. Final third being a string or int value as "000" or 1.
>>
>> CASE:A I have to print as -> Apple01001 with the last three digits from
>> right being incremented when needed e.g Apple01001, Apple01002,
>> Apple01003 and so on..
>>
>> CASE:B Also, the fourth and fifth digit from right being incremented
>> when needed, alongwith last three digits from right as - Apple02001,
>> Apple02002, Apple02003 and so on ..
>>
>
> If you already have the components broken out like that, then it is
> trivial to convert "001" to 1 via lexical_cast, and then do whatever
> operations you want on each component separately. (If you don't have them
> broken out, then you will need to use some kind of parser to break them out
> into components first.)
>
> You can't use lexical_cast to go the other way and produce "001" from 1 --
> it will produce only "1", and you would have to use something else to
> insert the leading zeroes that you desire.
>
> You might want to look at Boost.Format, eg:
>
> (boost::format("%1%%|2$02|%|3$03|") % tid % tagId % value).str()
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net