Boost logo

Boost Users :

Subject: Re: [Boost-users] Concatenating int value to string ..??
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2013-11-03 19:43:24


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