Two problems encountered by C # string type

Source: Internet
Author: User

Recently, in maintaining the WPF code written by a departing colleague, I stumbled upon two problems with the C # string type, which I recorded here.

1. Use the TRIM function to remove strings such as spaces, newline characters, and so on in a string.

Csret.trim (new char[] {' \ r ', ' \ n ', ' \ t ', '} ');
if (!csret.equals (string. Empty))

Statement1;

Else

Statement2;

The intent of the above code is to remove the string type variable Csret at the beginning and end of the line break, tab, character space, and then determine whether it is empty.

The actual test found that when Csret contains only the four characters, Statement2 is executed, that is, the trim string is not empty. The study found that the trim function was indeed able to remove the specified string from the beginning and end of the string, but instead of manipulating the original string, the action returned a new string after the removal operation was performed. So if you want to change the value inside the Csret to a trim value, you can use the following method:

Csret =

Csret.trim (new char[] {' \ r ', ' \ n ', ' \ t ', '} ');
if (!csret.equals (string. Empty))

Statement1;

Else

Statement2;

2. After using the Remove function, the value of the original string has not changed

Because the original string does not change the same after using the TRIM function above, the Remove function does not change the original string but returns a new string type variable. To modify the value of the original string type variable, simply perform an assignment and reassign the string returned by the Remove function to the original string.

Two problems encountered by C # string type

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.