writeln Function while reading a Text File is printing appending text "before text" and "after text" at the same position

BoQsc vaidas.boqsc at gmail.com
Wed Jun 3 20:43:43 UTC 2020


On Wednesday, 3 June 2020 at 20:05:52 UTC, ttk wrote:
> On Wednesday, 3 June 2020 at 19:53:03 UTC, BoQsc wrote:
>>> Removing the last element of the string got it resolved.
>>> Might not be the best way and adding additional check for 
>>> carriage return before removing the element would be better, 
>>> so this is only initial proof.
>>
>>
>> Improved example with the above comments resolved.
>
> That works, but consider using chomp() instead.
>
> https://dlang.org/phobos/std_string.html#.chomp

Chomp sounds kind of funny hahaha.
Who came up with these function names? Walter Bright?
Anyways, Chomp's way looks way more simple. Thanks.

> import std.stdio;
> import std.algorithm;
> import std.string;
> import std.uni : lineSep;
> 
> int lineNumber;
> void main(){
> 
> 	File exampleFile = File("exampleText.txt");
> 	lineNumber = 0;
> 	foreach(line; exampleFile.byLine){
> 		
> 		if (line == " Sphinx of black quartz, judge my vow.\u000D"){
> 			writeln(lineNumber, ". hahahahahahaha", chomp(line, "\r"), " 
> nononono");
> 			
> 		} else {
> 			writeln( lineNumber, ". ", line);
> 		}
> 		
> 		lineNumber++;
> 	}
> }


More information about the Digitalmars-d-learn mailing list