C# textbox文本中数字每4位中间用空格隔开,实际保存时又清除所有空格

textbox文本中数字每4位中间用空格隔开,实际保存时又清除所有空格

//每4位加空格隔开

if (this.textEdit1.Text.Trim().Length > 4)
            {
                string resultValue = this.textEdit1.Text.Trim();// "123456789";
                char[] char1 = resultValue.ToCharArray();
                StringBuilder sb = new System.Text.StringBuilder("");
                for (int i = 0; i < char1.Length; i++)
                {
                    if (i != 0 && i % 4 == 0)
                    {
                        sb.Append(" ");

                    }
                    sb.Append(char1[i]);
                }

                this.textEdit1.Text = sb.ToString();
            }

 

//清除所有空格

  MessageBox.Show( Regex.Replace( this.textEdit1.Text.Trim(), @"\s", "" ) );

//Regex.Replace( this.textEdit1.Text.Trim(), @"\s", "" )

 

 

这些都是在网上搜索拼成的

你可能感兴趣的:(textbox,c#,string)