zoukankan      html  css  js  c++  java
  • C#去除字符串空格的几种方法

    1.正规表达式:System.Text.RegularExpressions.Regex.Replace(str, "([ ]+)", "") --  str是输入或要检测的字符串。

    2.使用字符串自带的Replace方法:str.Replace(" ","")-------------  str是输入或要检测的字符串。

    3.由于空格的ASCII码值是32,因此,在去掉字符串中所有的空格时,只需循环访问字符串中的所有字符,并判断它们的ASCII码值是不是32即可。去掉字符串中所有空格的关键代码如下:

    1. CharEnumerator CEnumerator = textBox1.Text.GetEnumerator(); 
    2. while (CEnumerator.MoveNext()) 
    3. byte[] array = new byte[1]; 
    4. array = System.Text.Encoding.ASCII.GetBytes(CEnumerator.Current.ToString()); 
    5. int asciicode = (short)(array[0]); 
    6. if (asciicode != 32) 
    7. textBox2.Text += CEnumerator.Current.ToString(); 

    这里的3种方法只能去除半角空格,不能去除全角空格。

  • 相关阅读:
    hdu2588-GCD-(欧拉函数+分解因子)
    欧拉定理及其扩展定理公式
    hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)
    hdu5391-Zball in Tina Town-威尔逊定理(假证明)
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
  • 原文地址:https://www.cnblogs.com/zjoch/p/1886098.html
Copyright © 2011-2022 走看看