I have a string in the following format
string s = "This is a Test String.\n This is a next line.\t This is a tab.\n'
I want to remove all the occurrences of \n
and \r
from the string above.
I have tried string s = s.Trim(new char[] {'\n', '\r'});
but it didn't help.
String text = readFileAsString("textfile. txt"); text. replace("\n", "");
#include <algorithm> #include <string> std::string str; str. erase(std::remove(str. begin(), str. end(), '\n'), str.
Use the strip() Function to Remove a Newline Character From the String in Python. The strip() function is used to remove both trailing and leading newlines from the string that it is being operated on. It also removes the whitespaces on both sides of the string.
In order to remove \n from the string using the str. strip() method, we need to pass \n and \t to the method, and it will return the copy of the original string after removing \n and \t from the string. The str. strip() method only removes the substrings from the string's start and end position.
I like to use regular expressions. In this case you could do:
string replacement = Regex.Replace(s, @"\t|\n|\r", "");
Regular expressions aren't as popular in the .NET world as they are in the dynamic languages, but they provide a lot of power to manipulate strings.
You want to use String.Replace
to remove a character.
s = s.Replace("\n", String.Empty);
s = s.Replace("\r", String.Empty);
s = s.Replace("\t", String.Empty);
Note that String.Trim(params char[] trimChars)
only removes leading and trailing characters in trimChars
from the instance invoked on.
You could make an extension method, which avoids the performance problems of the above of making lots of temporary strings:
static string RemoveChars(this string s, params char[] removeChars) {
Contract.Requires<ArgumentNullException>(s != null);
Contract.Requires<ArgumentNullException>(removeChars != null);
var sb = new StringBuilder(s.Length);
foreach(char c in s) {
if(!removeChars.Contains(c)) {
sb.Append(c);
}
}
return sb.ToString();
}
I know this is an old post, however I thought I'd share the method I use to remove new line characters.
s.Replace(Environment.NewLine, "");
References:
MSDN String.Replace Method and MSDN Environment.NewLine Property
If speed and low memory usage are important, do something like this:
var sb = new StringBuilder(s.Length);
foreach (char i in s)
if (i != '\n' && i != '\r' && i != '\t')
sb.Append(i);
s = sb.ToString();
just do that
s = s.Replace("\n", String.Empty).Replace("\t", String.Empty).Replace("\r", String.Empty);
A LINQ approach:
string s = "This is a Test String.\n This is a next line.\t This is a tab.\n'";
string s1 = String.Join("", s.Where(c => c != '\n' && c != '\r' && c != '\t'));
The right choice really depends on how big the input string is and what the perforce and memory requirement are, but I would use a regular expression like
string result = Regex.Replace(s, @"\r\n?|\n|\t", String.Empty);
Or if we need to apply the same replacement multiple times, it is better to use a compiled version for the Regex like
var regex = new Regex(@"\r\n?|\n|\t", RegexOptions.Compiled);
string result = regex.Replace(s, String.Empty);
NOTE: different scenarios requite different approaches to achieve the best performance and the minimum memory consumption
string remove = Regex.Replace(txtsp.Value).ToUpper(), @"\t|\n|\r", "");
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