7

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 ?

goofyui
  • 3,362
  • 20
  • 72
  • 128
  • 2
    As 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. – Zachary Jun 23 '11 at 19:59
  • possible duplicate of [How can I replace a character in a string with something else?](http://stackoverflow.com/questions/5242323/how-can-i-replace-a-character-in-a-string-with-something-else) –  Jun 23 '11 at 20:03

2 Answers2

44
string trimmed = "careteam order4-26-11.csv".Replace(" ", string.Empty);
manji
  • 47,442
  • 5
  • 96
  • 103
  • 2
    I would recommend .Replace(" ",String.Empty); – softwaredeveloper Jun 23 '11 at 19:58
  • 3
    Note that String.Replace returns the result of the replace operation; it doesn't actually mutate the string it's called on. – Martin Törnwall Jun 23 '11 at 19:58
  • 1
    @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. – Ed S. Jun 23 '11 at 19:59
  • 1
    I've used `""` and `String.Empty` interchangably - is there a difference? – lhan Jun 23 '11 at 19:59
  • 2
    `String.Empty = ""`, there is no difference. – manji Jun 23 '11 at 20:01
  • 3
    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. – Fredrik Mörk Jun 23 '11 at 20:03
  • @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 http://blogs.msdn.com/b/brada/archive/2003/04/22/49997.aspx) to appreciate the suggestion. – softwaredeveloper Jun 23 '11 at 20:03
  • 1
    @software that depends. Jon Skeet prefers to use `""` (check the link I've posted previously in the comments) – BrunoLM Jun 23 '11 at 20:06
  • 2
    @software: That blog link puts cold water on your suggestion. –  Jun 23 '11 at 20:07
0
    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;
        }
    }
Brandon
  • 68,708
  • 30
  • 194
  • 223