C # several methods to Remove string Spaces

Source: Internet
Author: User

1. Regular Expression: system. Text. regularexpressions. RegEx. Replace (STR, "([] + )","")-- STR is the input or string to be checked.

2. Use the string's built-in replace method: Str. Replace ("","")------------- STR is the input or string to be checked.

3. Because the ASCII value of space is 32, when removing all spaces in the string, you only need to cyclically access all characters in the string and determine whether their ASCII value is 32. The key code to remove all spaces in the string is as follows:

  1. Charenumerator cenumerator = textbox1.text. getenumerator ();
  2. While (cenumerator. movenext ())
  3. {
  4. Byte [] array = new byte [1];
  5. Array = system. Text. encoding. ASCII. getbytes (cenumerator. Current. tostring ());
  6. Int asciicode = (short) (array [0]);
  7. If (asciicode! = 32)
  8. {
  9. Textbox2.text + = cenumerator. Current. tostring ();
  10. }
  11. }

The three methods can only remove halfwidth spaces, but not fullwidth spaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.