readln() returns new line charater
Marco Leise
Marco.Leise at gmx.de
Tue Dec 31 06:45:40 PST 2013
Am Tue, 31 Dec 2013 13:09:34 +0000
schrieb "Jeroen Bollen" <jbinero at gmail.com>:
> On Monday, 30 December 2013 at 02:59:23 UTC, Marco Leise wrote:
> > Am Sun, 29 Dec 2013 22:03:14 +0000
> > schrieb "Jeroen Bollen" <jbinero at gmail.com>:
> >
> >> On Sunday, 29 December 2013 at 18:13:30 UTC, Jakob Ovrum wrote:
> >> > On Sunday, 29 December 2013 at 17:25:39 UTC, Jeroen Bollen
> >> > wrote:
> >> >> Wouldn't byline return an empty string if the inputstream
> >> >> is exhausted but not closed?
> >> >
> >> > No, both `readln` and `byLine` will block until either EOL
> >> > or EOF. They differ in their handling of EOF - `readln`
> >> > returns an empty string, while the result of `byLine`
> >> > reports empty (it is a range) and calling `front` is an
> >> > error.
> >>
> >> But wouldn't that mean I'd still end up making my char[]
> >> mutable, as I still need to manually remove the last
> >> character, AFTER I checked it's not empty?
> >
> > No, strings have immutable characters, but there is nothing
> > wrong with using only part of it as an array slice:
> >
> > string s = readln();
> > s = s[0 .. $-1];
> >
> > (just to illustrate)
> I'm not talking about string though, I know you can resize a
> string, as it's an alias for immutable(char)[], but an immutable
> string would be immutable(immutable(char)[]), which is an
> immutable(charr[]). A mutable string would be immutable(char)[]
> which is the problem! Why does it need to be mutable if it won't
> ever change anyway!
I guess I just don't see what an immutable string buys you.
The mutable part in a string is just a pointer and length pair.
Just write:
immutable s = readln()[0 .. $-1];
and you have an immutable string at no cost.
--
Marco
More information about the Digitalmars-d
mailing list