C#不用Replace()去掉字符串中的空格

来源:互联网 发布:同步语文软件下载 编辑:程序博客网 时间:2024/05/16 10:04
        public static void Space(ref string str)        {            //空格在字符串中所处在的位置            int index = str.IndexOf(' ');            if (index >= 0 && index <= str.Length - 1)            {                try                {                    //分成前后两个部分进行字符串的缩减                    string start = str.Substring(0, index).Trim();                    string end = str.Substring(index + 1, (str.Length - index - 1)).Trim();                    //递归处理前后端                    Space(ref end);                    str = start + end;                }                catch (Exception e)                {                    throw e;                }            }        }

原创粉丝点击