清空字符串中所有空格字符


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConPra
{
    class Program
    {
        public static void Main(string[] Args)
        {

            string name = "asfafasfasf  fasf";
            char n = name[0];
            Console.WriteLine("获取的字符为:" + n);//输出a
            string str = TrimBlankSpace(name);
            Console.WriteLine(str);//输出"asfafasfasffasf"
            char []chs={'a',' ','b'};
            string m =string.Empty;
            //m = chs[1];//直接相等则报错
            m+= chs[1];
            Console.Read();
        }

        /// <summary>
        /// 清空字符串中空格字符
        /// </summary>
        /// <param name="tem"></param>
        /// <returns></returns>
        private static string TrimBlankSpace(string tem)
        {
            char[] chars = tem.ToCharArray();
            string res = string.Empty;
            int len = chars.Length;
            Console.WriteLine("字符长度"+len);
            for (int i = 0; i < len; i++)
            {
                if (chars[i]!=' ')
                {
                    res += chars[i];
                }
            }
            return res;
        }
    }
}

posted @ 2013-04-25 20:43  Predator  阅读(258)  评论(0编辑  收藏  举报