How to trim whitespace between characters

后端 未结 9 1859
天命终不由人
天命终不由人 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 07:50

    If you want to keep one space between every word. this should do it..

     public static string TrimSpacesBetweenString(string s)
        {
            var mystring  =s.RemoveTandNs().Split(new string[] {" "}, StringSplitOptions.None);
            string result = string.Empty;
            foreach (var mstr in mystring)
            {
                var ss = mstr.Trim();
                if (!string.IsNullOrEmpty(ss))
                {
                    result = result + ss+" ";
                }
            }
            return result.Trim();
    
        }
    

    it will remove the string in between the string so if the input is

    var s ="c           sharp";
    result will be "c sharp";
    

提交回复
热议问题