Reputation: 3466
Dear fellow programmers,
I am coding something in C# Visual Studio 2013 and I have just realized I might not need to use Trim()
when I do Replace(" ", string.Empty)
.
An example follows:
SanitizedString = RawString
.Replace("/", string.Empty)
.Replace("\\", string.Empty)
.Replace(" ", string.Empty)
.Trim();
As I previously had this code structured differently, I haven't noticed it:
SanitizedString = RawString.Trim()
.Replace("/", string.Empty)
.Replace("\\", string.Empty)
.Replace(" ", string.Empty);
I am aware these methods work different, as Trim()
removes all whitespace characters, whereas Replace(" ", string.Empty)
removes only space characters.
That's why I have a different question.
I don't see any obvious way to achieve that with Replace. My question is how would I go about it when I wish to remove all whitespace characters from the string?
I found the following:
Efficient way to remove ALL whitespace from String?
But as I have never used regular expressions, I hesitate on how to apply it to string?
Upvotes: 12
Views: 47423
Reputation: 4405
The answer to this question is not as easy as it seems. The problem is not to actually code the replacement, but to define what a white space is.
For example, this Wikipedia article lists dozens of characters (Unicode code points) which have the Unicode attribute WSpace
, plus many related characters which most people would consider white space, but which don't have the WSpace
attribute.
Given that, I never would rely on what some regex parser considers \s
, because this is actually not standardized. I am quite sure that C#'s regex parser does not treat code points like U+2001
as white space, so they won't be removed from your string.
This may or may not be a problem for your application; it depends on how the strings you have to handle are filtered in the first place. But if you are going to handle strings in foreign languages (in other words: strings which contain characters outside the ASCII range), you will have to think about it.
When doing so, keep in mind that regex expression execution is slow. If you need to define your own replacements anyway (for the reasons mentioned above), you should use a more lightweight replace function (if C# or its assemblies provide one - I don't use C#, so I don't know).
Upvotes: 4
Reputation: 1
This might be handy:
string message = Console.ReadLine().Trim().ToLower();
string message_without_space = message.Split(' ').Aggregate((a, b) => a + b).ToString();
Upvotes: 0
Reputation: 664
For anyone coming to this page, there is an excellent CodeProject article where the author benchmarks several different solutions to this problem. The fastest fully managed solution he came up with is (essentially) the following:
public static string RemoveAllWhitespace(string str) {
var len = str.Length;
var src = str.ToCharArray();
var dstIdx = 0;
for (var i = 0; i < len; i++) {
var ch = src[i];
switch (ch) {
case '\u0020': case '\u00A0': case '\u1680': case '\u2000': case '\u2001':
case '\u2002': case '\u2003': case '\u2004': case '\u2005': case '\u2006':
case '\u2007': case '\u2008': case '\u2009': case '\u200A': case '\u202F':
case '\u205F': case '\u3000': case '\u2028': case '\u2029': case '\u0009':
case '\u000A': case '\u000B': case '\u000C': case '\u000D': case '\u0085':
break;
default:
src[dstIdx++] = ch;
break;
}
}
return new string(src, 0, dstIdx);
}
There are of course many caveats and potentially differences of opinion on what is the correct set of whitespace characters, but the basic information is pretty useful (like RegEx was the slowest by far).
Full article here: https://www.codeproject.com/Articles/1014073/Fastest-method-to-remove-all-whitespace-from-Strin
Note: I have no affiliation with the author or CodeProject. I just found this article through a normal web search.
Upvotes: 3
Reputation: 376
Just Pass The string on Method Calling, it will return the string without whitespaces.
public static string RemoveSpaces(this String Value)
{
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
return regex.Replace(Value.Trim(), @" ");
}
Upvotes: 1
Reputation: 21
string Abcd = Console.ReadLine();
string wspace = "";
int len = Abcd.Length;
for (int i = 0; i <= len-1; i++)
{
if (Abcd[i] != ' ')
{
wspace = wspace + Abcd[i];
}
}
Console.WriteLine("The Sring Without Space Is= '"+wspace+"'");
Console.ReadLine();
Upvotes: 0
Reputation: 186688
Try using Linq in order to filter out white spaces:
using System.Linq;
...
string source = "abc \t def\r\n789";
string result = string.Concat(source.Where(c => !char.IsWhiteSpace(c)));
Console.WriteLine(result);
Outcome:
abcdef789
Upvotes: 28
Reputation: 111
One way is to use Regex
public static string ReplaceAllWhiteSpaces(string str) {
return Regex.Replace(str, @"\s+", String.Empty);
}
Taken from: https://codereview.stackexchange.com/questions/64935/replace-each-whitespace-in-a-string-with-20
Upvotes: 11