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
Just (invisible) entries in a string. \r moves cursor to the beginning of the line. \n goes one line down.
Try this. text = text . Replace("\\r\\n", "");
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With