readln() returns new line charater

Ali Çehreli acehreli at yahoo.com
Sat Dec 28 09:11:32 PST 2013


On 12/28/2013 08:50 AM, Jeroen Bollen wrote:

 > On Saturday, 28 December 2013 at 16:49:15 UTC, Jeroen Bollen wrote:
 >> Why is when you do readln() the newline character (\n) gets read too?

Because it is possible to remove but hard or expensive or even 
impossible (was there a newline?) to add back if needed.

 >> Wouldn't it make more sense for that character to be stripped off?
 >
 > I just want to add to this, that it makes it really annoying to work
 > with the command line, as you kinda have to strip off the last character
 > and thus cannot make the string immutable.

It is pretty easy actually:

import std.stdio;
import std.string;

void main()
{
     string line = readln.chomp;
}

(Or with various combinations of parethesis and without UFCS. :) )

That works even when you wanted the whole string to be immutable:

     immutable char[] line = readln.chomp;

Or, perhaps more preferably:

     immutable(char[]) line = readln.chomp;

Ali



More information about the Digitalmars-d mailing list