Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove carriage returns, newlines, spaces from a string

Tags:

string

c#

regex

xml

how do I remove the spaces, carriage returns from in between < and >

**START BELOW THIS LINE***  </TestItem1Request>   <Username>admin</Username>   <Password>123abc..@!</Password>   <Item1>this is an item</Item1> </TestItem1Request>  **END HERE ** 

I want the string result to end up as the following...

</TestItem1Request><Username>admin</Username><Password>123abc..@!</Password><Item1>this is an item</Item1></TestItem1Request> 

How do I do this?

like image 755
001 Avatar asked May 24 '11 14:05

001


People also ask

How do I remove a carriage return from a string?

In the Find box hold down the Alt key and type 0 1 0 for the line feed and Alt 0 1 3 for the carriage return. They can now be replaced with whatever you want.

How do you remove spaces from a string?

Use the String. replace() method to remove all whitespace from a string, e.g. str. replace(/\s/g, '') . The replace() method will remove all whitespace characters by replacing them with an empty string.

Does string trim remove carriage return?

Removes all carriage return values ( Chr(13) ) from a string.

How do I remove a carriage return from a string in Java?

replaceAll("\\n", ""); s = s. replaceAll("\\r", ""); But this will remove all newlines. Note the double \ 's: so that the string that is passed to the regular expression parser is \n .


1 Answers

If this is valid XML you could use the SaveOptions.DisableFormatting enumeration:

string input = @"<TestItem1Request>   <Username>admin</Username>   <Password>123abc..@!</Password>   <Item1>this is an item</Item1> </TestItem1Request>";  string result = XElement.Parse(input).ToString(SaveOptions.DisableFormatting); Console.WriteLine(result); 
like image 133
Ahmad Mageed Avatar answered Oct 11 '22 18:10

Ahmad Mageed