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);
Share:
26,521
goofyui
Author by

goofyui

Programmer - Interested in Big Data, Statistical Data Analysis, Cloud Computing, Mobile Apps

Updated on August 08, 2022

Comments

  • goofyui
    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 ?

  • softwaredeveloper
    softwaredeveloper almost 13 years
    I would recommend .Replace(" ",String.Empty);
  • Martin Törnwall
    Martin Törnwall almost 13 years
    Note that String.Replace returns the result of the replace operation; it doesn't actually mutate the string it's called on.
  • Ed S.
    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
    lhan almost 13 years
    I've used "" and String.Empty interchangably - is there a difference?
  • manji
    manji almost 13 years
    String.Empty = "", there is no difference.
  • Fredrik Mörk
    Fredrik Mörk almost 13 years
    There is no technical difference between "" and string.Empty, but I think there is a readability difference. " " and "" look much more alike than " " and string.Empty, so I personally read and understand the code quicker with string.Empty in this case.
  • softwaredeveloper
    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
    BrunoLM almost 13 years
    @software that depends. Jon Skeet prefers to use "" (check the link I've posted previously in the comments)
  • Admin
    Admin almost 13 years
    @software: That blog link puts cold water on your suggestion.