www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Insert a char in string

reply "Alexandre" <alebencz gmail.com> writes:
I have a string X and I need to insert a char in that string...

auto X = "100000000000000";

And I need to inser a ',' in position 3 of this string..., I try 
to use the array.insertInPlace, but, not work...

I try this:
auto X = "100000000000000";
auto N = X.insertInPlace(1,'0');
Jul 10 2014
next sibling parent reply "Alexandre" <alebencz gmail.com> writes:
Sorry..
I mean:

auto X = "100000000000000";
auto N = X.insertInPlace(3,',');

On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:
 I have a string X and I need to insert a char in that string...

 auto X = "100000000000000";

 And I need to inser a ',' in position 3 of this string..., I 
 try to use the array.insertInPlace, but, not work...

 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
Jul 10 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Thursday, 10 July 2014 at 16:20:29 UTC, Alexandre wrote:
 Sorry..
 I mean:

 auto X = "100000000000000";
 auto N = X.insertInPlace(3,',');

 On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:
 I have a string X and I need to insert a char in that string...

 auto X = "100000000000000";

 And I need to inser a ',' in position 3 of this string..., I 
 try to use the array.insertInPlace, but, not work...

 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
`std.array.insertInPlace` doesn't return anything. "In place" here means "in situ", i.e. it will not create a new string, but insert the new elements into the existing one. This operation may still reallocate, in which case the array slice you're passing in will be updated to point to the new memory. Either use this instead: auto x = "100000000000000"; auto n = x.dup; n.insertInPlace(3, ','); // or: insertInPlace(n, 3, ','); ... or use slicing and concatenating to construct a new string: auto g = x[0 .. 3] ~ ',' ~ x[3 .. $]; (Side note about style: It's common practice to use lower-case names for variables, upper-case first letters are used to denote types. But of course, that's a matter of taste.)
Jul 10 2014
parent "Alexandre" <alebencz gmail.com> writes:
Oh, I used that letters in upper case, just for a simple sample...

On Thursday, 10 July 2014 at 16:32:53 UTC, Marc Schütz wrote:
 On Thursday, 10 July 2014 at 16:20:29 UTC, Alexandre wrote:
 Sorry..
 I mean:

 auto X = "100000000000000";
 auto N = X.insertInPlace(3,',');

 On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:
 I have a string X and I need to insert a char in that 
 string...

 auto X = "100000000000000";

 And I need to inser a ',' in position 3 of this string..., I 
 try to use the array.insertInPlace, but, not work...

 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
`std.array.insertInPlace` doesn't return anything. "In place" here means "in situ", i.e. it will not create a new string, but insert the new elements into the existing one. This operation may still reallocate, in which case the array slice you're passing in will be updated to point to the new memory. Either use this instead: auto x = "100000000000000"; auto n = x.dup; n.insertInPlace(3, ','); // or: insertInPlace(n, 3, ','); ... or use slicing and concatenating to construct a new string: auto g = x[0 .. 3] ~ ',' ~ x[3 .. $]; (Side note about style: It's common practice to use lower-case names for variables, upper-case first letters are used to denote types. But of course, that's a matter of taste.)
Jul 10 2014
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:
 I have a string X and I need to insert a char in that string...

 auto X = "100000000000000";

 And I need to inser a ',' in position 3 of this string..., I 
 try to use the array.insertInPlace, but, not work...

 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
insertInPlace works like this: auto X = "100000000000000"; auto X1 = X; X.insertInPlace(3, ','); assert(X == "100,000000000000"); assert(X1 == "100000000000000"); You can also do this: auto X = "100000000000000"; auto N = X[0 .. 3] ~ ',' ~ X[3 .. $]; assert(X == "100000000000000"); assert(N == "100,000000000000");
Jul 10 2014
parent "Alexandre" <alebencz gmail.com> writes:
I used that solution:

string InsertComma(string val)
{
	return val[0 .. $-2] ~ "," ~ val[$-2 .. $];
}

On Thursday, 10 July 2014 at 16:23:44 UTC, John Colvin wrote:
 On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote:
 I have a string X and I need to insert a char in that string...

 auto X = "100000000000000";

 And I need to inser a ',' in position 3 of this string..., I 
 try to use the array.insertInPlace, but, not work...

 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
insertInPlace works like this: auto X = "100000000000000"; auto X1 = X; X.insertInPlace(3, ','); assert(X == "100,000000000000"); assert(X1 == "100000000000000"); You can also do this: auto X = "100000000000000"; auto N = X[0 .. 3] ~ ',' ~ X[3 .. $]; assert(X == "100000000000000"); assert(N == "100,000000000000");
Jul 10 2014
prev sibling next sibling parent reply simendsjo <simendsjo gmail.com> writes:
On 07/10/2014 06:05 PM, Alexandre wrote:
 I have a string X and I need to insert a char in that string...
 
 auto X = "100000000000000";
 
 And I need to inser a ',' in position 3 of this string..., I try to use
 the array.insertInPlace, but, not work...
 
 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
Do you really want to insert a comma in the string, or do you want to format a number as "100,000,000,000.00"?
Jul 10 2014
next sibling parent reply "Alexandre" <alebencz gmail.com> writes:
basically format....
I read a cobol struct file...

 From pos X to Y I have a money value... but, this value don't 
have any format..

000000000041415

The 15 is the cents... bascally I need to put the ( comma ), we 
use comma to separate the cents, here in Brazil...

On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote:
 On 07/10/2014 06:05 PM, Alexandre wrote:
 I have a string X and I need to insert a char in that string...
 
 auto X = "100000000000000";
 
 And I need to inser a ',' in position 3 of this string..., I 
 try to use
 the array.insertInPlace, but, not work...
 
 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
Do you really want to insert a comma in the string, or do you want to format a number as "100,000,000,000.00"?
Jul 10 2014
parent simendsjo <simendsjo gmail.com> writes:
On 07/10/2014 09:58 PM, Alexandre wrote:
 basically format....
 I read a cobol struct file...
 
 From pos X to Y I have a money value... but, this value don't have any
 format..
 
 000000000041415
 
 The 15 is the cents... bascally I need to put the ( comma ), we use
 comma to separate the cents, here in Brazil...
 
 On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote:
 On 07/10/2014 06:05 PM, Alexandre wrote:
 I have a string X and I need to insert a char in that string...

 auto X = "100000000000000";

 And I need to inser a ',' in position 3 of this string..., I try to use
 the array.insertInPlace, but, not work...

 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
Do you really want to insert a comma in the string, or do you want to format a number as "100,000,000,000.00"?
I'm not sure what you're trying to do though. Do you need to fix the file by adding a comma at appropriate places? Or read it into D and write it to the console with your currency format? This is one way of reading in the values using slices and std.conv: import std.stdio, std.conv; void main() { immutable input = "000000000041415"; double amount = input[0..$-2].to!double(); amount += input[$-2..$].to!double() / 100; writeln(amount); }
Jul 10 2014
prev sibling parent "JR" <zorael gmail.com> writes:
On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote:
 On 07/10/2014 06:05 PM, Alexandre wrote:
 I have a string X and I need to insert a char in that string...
 
 auto X = "100000000000000";
 
 And I need to inser a ',' in position 3 of this string..., I 
 try to use
 the array.insertInPlace, but, not work...
 
 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
Do you really want to insert a comma in the string, or do you want to format a number as "100,000,000,000.00"?
For that, one approach would be as in http://dpaste.dzfl.pl/bddb71eb75bb.
Jul 11 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/10/2014 09:05 AM, Alexandre wrote:
 I have a string X and I need to insert a char in that string...

 auto X = "100000000000000";

 And I need to inser a ',' in position 3 of this string..., I try to use
 the array.insertInPlace, but, not work...

 I try this:
 auto X = "100000000000000";
 auto N = X.insertInPlace(1,'0');
Here is another solution, which does not modify the original string: import std.stdio; import std.algorithm; import std.range; import std.string; void main() { auto number = "12345678"; auto formatted = zip(number.retro, sequence!"n + 1") .map!(z => format(!(z[1] % 3) ? "%s," : "%s", z[0])) .join .retro; assert(formatted.equal("12,345,678")); } I am not happy with the .join there because I think it makes an array but I could not get it to compile with the lazy .joiner because I think it dose not look like a bidirectional range to .retro. Ali
Jul 12 2014