Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip `\r \n ` in string

Tags:

c#

I am working on a simple converter which converts a text to another language,

suppose i have two textboxes and in 1st box you enter the word Index and press the convert button.
I will replace your text with this فہرست an alternative of Index in urdu language but i have a problem if you enter word index and gives some spaces or gives some returns then i get text of that textbox in c# like this Index \r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n now how can i get rid of this i want to get simple Index always .
Thanks for answer and please feel free to comment if you have any question

like image 659
Smartboy Avatar asked Dec 19 '12 11:12

Smartboy


People also ask

What is \r in a string?

Just (invisible) entries in a string. \r moves cursor to the beginning of the line. \n goes one line down.

How do I remove RN from text?

Try this. text = text . Replace("\\r\\n", "");

Is it n or \n for New Line?

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.


1 Answers

Try using the Trim method if the new lines are only at the end of beginning:

input = input.Trim();

You can use Replace, if you want to remove new lines anywhere in the string:

// Replace line break with spaces
input = input.Replace("\r\n", " ");
// (Optionally) Combine consecutive spaces to one space (probalby not most efficient but should work)
while (input.Contains("  ")) { input = input.Replace("  ", " "); }

If you want to prevent newlines completely, most TextBox Controls have a property like MultiLine or similar, that, when set, prevents entering more than one line.

like image 156
Botz3000 Avatar answered Oct 07 '22 23:10

Botz3000