digitalmars.D.learn - Problem with std.string.strip(): Can't use result in format routine
- Ky-Anh Huynh (18/18) Sep 05 2017 Hi,
- Azi Hassan (4/22) Sep 05 2017 Maybe it has something to do with how you read from STDIN. Can
- Ky-Anh Huynh (7/10) Sep 05 2017 I used `lines(stdin)` as in
- Andrea Fontana (3/13) Sep 05 2017 I think formattedRead is consuming your string.
- Ky-Anh Huynh (11/19) Sep 05 2017 Great catch. You're right. Thanks a lot.
Hi, I read line from STDIN , and strip them [code] auto line_st = line.strip(); [/code] However, I can't use result in another format routine. Assume my input line is "foobar": [code] writeln("Stripped line is %s", line_st); [/code] This code only prints "Stripped line is ". If I use instead [code] writeln("Stripped line is %s", line.strip()); [/code] the result is "Stripped line is foobar". What're the difference between the two uses? I'm using `dmd v2.075.1`. Thanks a lot.
Sep 05 2017
On Tuesday, 5 September 2017 at 12:38:54 UTC, Ky-Anh Huynh wrote:Hi, I read line from STDIN , and strip them [code] auto line_st = line.strip(); [/code] However, I can't use result in another format routine. Assume my input line is "foobar": [code] writeln("Stripped line is %s", line_st); [/code] This code only prints "Stripped line is ". If I use instead [code] writeln("Stripped line is %s", line.strip()); [/code] the result is "Stripped line is foobar". What're the difference between the two uses? I'm using `dmd v2.075.1`. Thanks a lot.Maybe it has something to do with how you read from STDIN. Can you show that part of the code to see if I can reproduce the issue ?
Sep 05 2017
On Tuesday, 5 September 2017 at 13:17:34 UTC, Azi Hassan wrote:Maybe it has something to do with how you read from STDIN. Can you show that part of the code to see if I can reproduce the issue ?I used `lines(stdin)` as in https://dlang.org/phobos/std_stdio.html#.lines . My source code is here https://github.com/icy/dusybox/blob/master/src/plotbar/main.d#L47 . Thanks for your support.
Sep 05 2017
On Tuesday, 5 September 2017 at 13:40:18 UTC, Ky-Anh Huynh wrote:On Tuesday, 5 September 2017 at 13:17:34 UTC, Azi Hassan wrote:I think formattedRead is consuming your string. AndreaMaybe it has something to do with how you read from STDIN. Can you show that part of the code to see if I can reproduce the issue ?I used `lines(stdin)` as in https://dlang.org/phobos/std_stdio.html#.lines . My source code is here https://github.com/icy/dusybox/blob/master/src/plotbar/main.d#L47 . Thanks for your support.
Sep 05 2017
On Tuesday, 5 September 2017 at 13:56:20 UTC, Andrea Fontana wrote:Great catch. You're right. Thanks a lot. `strip()` returns a slice. I use another reference variable to save them and it's fine now. [code] auto line_st = line.strip(); auto line_st2 = line_st; .... writefln("Stripped lined is %s", line_st2); // works fine [/code]I used `lines(stdin)` as in https://dlang.org/phobos/std_stdio.html#.lines . My source code is here https://github.com/icy/dusybox/blob/master/src/plotbar/main.d#L47 . Thanks for your support.I think formattedRead is consuming your string.
Sep 05 2017