我是将空格来分别字段...所以空格多的话又不能老是手动删掉,比如
我是谁    男     136412098,这样空格就占了10多个,从TXT导进数据库的时候就将空格也当成一个字段导入了...
所以我想处理成有多个空格的地方缩成一个空格.
我是谁 男 1361240989.这是我写判断的代码 string m_BaseContent = this.richTextBox1.Text.ToString();                int i = 0;//计量数字
                string sql1 = GetIndexOf(sb.ToString());
                string SQL = "";
                if (m_BaseContent != "")
                {
                    char[] split = "\r\n".ToCharArray();
                    string[] arrRows = m_BaseContent.Split(split);
                    string[] arrCells = new string[25];
                    for (int j = 0; j < arrRows.Length; j++)
                    {
                        SQL = "";
                        i++;
                        arrCells = arrRows[j].Split(' ');
                        
                        //if (arrCells.Length != 10)
                        //{
                        //    msg += "第" + i + "数据不全,应为10列。\n";
                        //    continue;
                        //}
                        if (checkname2.Checked)
                            SQL += ",'" + arrCells[0]+"'";                        if (checksex2.Checked)
                            SQL += ",'" + arrCells[1] + "'";                        if (checkmoble2.Checked)
                            SQL += ",'" + arrCells[2] + "'";                        if (checkPhone2.Checked)
                            SQL += ",'" + arrCells[3] + "'";                        if (checkCity2.Checked)
                            SQL += ",'" + arrCells[4] + "'";                        if (checkCardtype2.Checked)
                            SQL += ",'" + arrCells[5] + "'";                        if (checkAddress2.Checked)
                            SQL += ",'" + arrCells[6] + "'";                        if (checkFund2.Checked)
                            SQL += ",'" + arrCells[7] + "'";                        if (checkFund3.Checked)
                            SQL += ",'" + arrCells[8] + "'";                        if (checkCredential2.Checked)
                            SQL += ",'" + arrCells[9] + "'";                        if (checkCredit2.Checked)
                            SQL += ",'" + arrCells[10] + "'";                        if (checkNotes2.Checked)
                            SQL += ",'" + arrCells[11] + "'";                        if (checkResidence2.Checked)
                            SQL += ",'" + arrCells[12] + "'";                        try
                        {
                            SQL = " VALUES (" + SQL.Substring(1, SQL.Length - 1) + ") ";
                            baseAccess.ExecuteNonQuery(sql1 + SQL);
                        }
                        catch (Exception ex)
                        {
                            msg += ex.Message;
                        }
 private static string GetIndexOf(string sql)
        {
            int i = sql.IndexOf(',');
            if (i < 0)
                return sql;
            else
                return sql.Remove(i, 1);
        }        private static string GetIndex(string sql)
        {
            int i = sql.IndexOf("',");
            if (i < 0)
            {
                return sql;
            }
            else
                return sql.Remove(i, 2);
        }请帮我改动一下哪里能做成我这样的需求...

解决方案 »

  1.   

    s = System.Text.RegularExpressions.Regex.Replace(s, "\\s+", " ");
      

  2.   

    regexp.replace("aa    aaa", "[\s]+", " ")
      

  3.   

    eg:
                String str = "ab   cd   eedd";
                String[] arr = str.Split(new Char[]{' '},StringSplitOptions.RemoveEmptyEntries);            StringBuilder sb = new StringBuilder();
                for (Int32 i = 0; i < arr.Length; i++)
                    sb.Append(arr[i] + " ");            String res = sb.ToString();            Console.WriteLine(res);
      

  4.   

    这样就行了:
    string str = "我是谁     男       136412098   23";
    Regex re = new Regex(@"\s+");
    string ret = re.Replace(str, " ");
      

  5.   

    char[] split = "\r\n".ToCharArray();
                        string[] arrRows = s.Split(split);
                        string[] arrCells = new string[30];
                        for (int j = 0; j < arrRows.Length; j++)
                        {
                            SQL = "";
                            i++;
                            arrCells = arrRows[j].Split(' ');在arrRows将把有字段都定义为一行了…… 读第二行的时候就从for跳出了……
      

  6.   

    最好、最简单的方法是这个:
    string s="asd               sdf        sasdf     aa aa       s"
    s.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);如果中间有多个空格,加上StringSplitOptions.RemoveEmptyEntries这个选项后,会自动帮你把多余的空格去掉。
      

  7.   

    s = System.Text.RegularExpressions.Regex.Replace(s, "\\s+", " ");
    要\t才行,谢谢,已经可以正确运行了