www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about the $ sign in arrays and strings

reply Namal <sotis22 mail.ru> writes:
Hello, I wanted to remove the lastchar in a string and figured 
that you can do that wit

str = str[0..$-2];

but why is

str = str[0..$] and str=str[0..$-1]

the same ?
Feb 18 2020
parent reply mipri <mipri minimaltype.com> writes:
On Wednesday, 19 February 2020 at 07:04:48 UTC, Namal wrote:
 Hello, I wanted to remove the lastchar in a string and figured 
 that you can do that wit

 str = str[0..$-2];

 but why is

 str = str[0..$] and str=str[0..$-1]

 the same ?
Why do you think that they are the same? $ rdmd --eval 'auto str = "hello"; writeln(str = str[0..$]); writeln(str = str[0..$-1])' hello hell
Feb 18 2020
parent reply Namal <sotis22 mail.ru> writes:
oooh... I used

str = std.readln();

to get my string and there must have been some other sign, line 
break or whitespace or something at the end  :(

Now I understand it, thx
Feb 18 2020
parent mipri <mipri minimaltype.com> writes:
On Wednesday, 19 February 2020 at 07:49:36 UTC, Namal wrote:
 oooh... I used

 str = std.readln();

 to get my string and there must have been some other sign, line 
 break or whitespace or something at the end  :(

 Now I understand it, thx
That makes sense. readln includes the newline: $ echo hello | rdmd --eval 'readln.map!(std.uni.isWhite).writeln' [false, false, false, false, false, true] You can use std.string.chomp to drop it: $ echo hello | rdmd --eval 'readln.chomp.map!(std.uni.isWhite).writeln' [false, false, false, false, false]
Feb 19 2020