[C#] 4 ways to remove "\r" or "\n" or "\t" new line or line break and tab characters from a string?
If you have a string, like this:string str = "Line 1\n Tab it.\t Break.\n'
How to remove breakline or tab in this string?
So you can using some methods:Method 1: quick to code
string newstr = Regex.Replace(str, @"\t|\n|\r", ""); // \t is tab, \n and \r is new line
Method 2: for New line onlystring newstr = str.Replace(Environment.NewLine, "");
Method 3: Easy to codestr = str.Replace("\n", String.Empty);
str = str.Replace("\r", String.Empty);
str = str.Replace("\t", String.Empty);
Method 4: For speed and low memory, because is StringBuilder best!var sb = new StringBuilder(s.Length);
foreach (char i in str)
if (i != '\n' && i != '\r' && i != '\t')
sb.Append(i);
str = sb.ToString();
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment