Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Line Breaks in a String C#

Tags:

string

c#

.net

How can I replace Line Breaks within a string in C#?

like image 592
YonahW Avatar asked Oct 26 '08 13:10

YonahW


People also ask

Does fgets remove newline?

You can simply do if (fgets(Name, sizeof Name, stdin) == NULL) {} . Not sure why you would want to do this. The point of removing newlines isn't to null-terminate strings; it is to remove newlines. Replacing a \n with a \0 at the end of a string is a way of "removing" the newline.

How do you remove a new line character from a string?

Method 2: Use the strip() Function to Remove a Newline Character From the String in Python. The strip() method in-built function of Python is used to remove all the leading and trailing spaces from a string. Our task can be performed using strip function() in which we check for “\n” as a string in a string.

Does fgets add newline?

The fgets function reads characters from the stream stream up to and including a newline character and stores them in the string s , adding a null character to mark the end of the string.

How do I make scanf ignore newline?

For a simple solution, you could add a space before the format specifier when you use scanf(), for example: scanf(" %c", &ch); The leading space tells scanf() to skip any whitespace characters (including newline) before reading the next character, resulting in the same behavior as with the other format specifiers.


1 Answers

Use replace with Environment.NewLine

myString = myString.Replace(System.Environment.NewLine, "replacement text"); //add a line terminating ; 

As mentioned in other posts, if the string comes from another environment (OS) then you'd need to replace that particular environments implementation of new line control characters.

like image 57
Corin Blaikie Avatar answered Sep 19 '22 12:09

Corin Blaikie