How to trim whitespace between characters

后端 未结 9 1821
天命终不由人
天命终不由人 2020-12-05 07:17

How to remove whitespaces between characters in c#?

Trim() can be used to remove the empty spaces at the beginning of the string as well as at the end.

相关标签:
9条回答
  • 2020-12-05 08:11

    Use String.Replace to replace all white space with nothing.

    eg

    string newString = myString.Replace(" ", "");
    
    0 讨论(0)
  • 2020-12-05 08:11
    //Remove spaces from a string just using substring method and a for loop 
    static void Main(string[] args)
    {
        string businessName;
        string newBusinessName = "";
        int i;
    
        Write("Enter a business name >>> ");
        businessName = ReadLine();
    
        for(i = 0; i < businessName.Length; i++)
        {
            if (businessName.Substring(i, 1) != " ")
            {
                newBusinessName += businessName.Substring(i, 1);
            }
        } 
    
        WriteLine("A cool web site name could be www.{0}.com", newBusinessName);
    }
    
    0 讨论(0)
  • 2020-12-05 08:13

    If you want to keep one space between every word. You can do it this way as well:

    string.Join(" ", inputText.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).ToList().Select(x => x.Trim()));
    
    0 讨论(0)
提交回复
热议问题