Remove White Space Characters From String
26,521
Solution 1
public string RemoveSpaceAfter(string inputString)
{
try
{
string[] Split = inputString.Split(new Char[] { ' ' });
//SHOW RESULT
for (int i = 0; i < Split.Length; i++)
{
fateh += Convert.ToString(Split[i]);
}
return fateh;
}
catch (Exception gg)
{
return fateh;
}
}
Solution 2
string trimmed = "careteam order4-26-11.csv".Replace(" ", string.Empty);
Author by
goofyui
Programmer - Interested in Big Data, Statistical Data Analysis, Cloud Computing, Mobile Apps
Updated on August 08, 2022Comments
-
goofyui over 1 year
String variable stores value as like ..
careteam order 4-26-11.csv
i need to trim the value .. in c# as like
careteamorder4-26-11.csv
- by removing the space ..!how to remove empty space in the string variable ?
-
Colin Chen almost 13 yearsAs pointed out below, "trim" is for removing white space on the front and/or end of your text value. Use "replace" to remove white space from anywhere in your string.
-
Admin almost 13 yearspossible duplicate of How can I replace a character in a string with something else?
-
-
softwaredeveloper almost 13 yearsI would recommend .Replace(" ",String.Empty);
-
Martin Törnwall almost 13 yearsNote that String.Replace returns the result of the replace operation; it doesn't actually mutate the string it's called on.
-
Ed S. almost 13 years@softwaredeveloper: Why? I use
String.Empty
as well, but honestly there is no good reason to prefer one over the other as long as you're consistent. -
lhan almost 13 yearsI've used
""
andString.Empty
interchangably - is there a difference? -
manji almost 13 years
String.Empty = ""
, there is no difference. -
Fredrik Mörk almost 13 yearsThere is no technical difference between
""
andstring.Empty
, but I think there is a readability difference." "
and""
look much more alike than" "
andstring.Empty
, so I personally read and understand the code quicker withstring.Empty
in this case. -
softwaredeveloper almost 13 years@all because it is more readable. It can be hard to spot " " a single space and no space in the default Visual Studio Font. Just my experience. (Also technically please do read this blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx) to appreciate the suggestion.
-
BrunoLM almost 13 years@software that depends. Jon Skeet prefers to use
""
(check the link I've posted previously in the comments) -
Admin almost 13 years@software: That blog link puts cold water on your suggestion.