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种方法只能去除半角空格,不能去除全角空格。


    作者:古梅飞动
    公司:蜜团科技
    出处:http://www.mitsofts.com/index.php
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    MP教程-入门
    [15213] Assembly
    Crack the code interview
    [interview questions] 资料总结
    [Two Sigma OA] Longest Chain
    [Tow Sigma OA] friend cycles
    [security]
    [security] GNUpg
    [coursera] 面试前准备
    [coursera] [design] Hangman
  • 原文地址:https://www.cnblogs.com/luhuan860/p/1493007.html
Copyright © 2011-2022 走看看