locked
remove empty space RRS feed

  • Question

  • remove emprty space in string

    For example 

     Input: "Phone Number"

    I want output: "PhoneNumber"

    please resolve this problem

    Monday, July 23, 2012 9:57 AM

Answers

  • Hi

    string output = input.Replace(" ", "");

    Monday, July 23, 2012 10:00 AM

All replies

  • Hi

    string output = input.Replace(" ", "");

    Monday, July 23, 2012 10:00 AM
  • if you want to get rid of any kind of whitespace including " ", \t, \r \n and
    others of the SpaceSepareator unicode category, try

    string spaceless = RemoveWhiteSpaces("Phone Number");
    
    static string RemoveWhiteSpaces(string input)
    {
         char[] chars = input.Where(c => !char.IsWhiteSpace(c)).ToArray();
         return new string(chars);
    }

    Chris

    • Proposed as answer by insigniya Monday, July 23, 2012 10:45 AM
    Monday, July 23, 2012 10:32 AM
  • Use this code:

    string name is input:

    input.Trim();

    It removes all the whitespaces


    Sivalingam

    Monday, July 23, 2012 11:38 AM
  • Use this code:

    string name is input:

    input.Trim();

    It removes all the whitespaces


    Sivalingam

    No, it doesn't.  It removes all whitespace at the start and end of the string, not throughout the entire string.  It doesn't even work on the single example provided by the OP.  Muki's answer is exactly what is being asked for.
    Monday, July 23, 2012 2:50 PM