String and stringbuider in C #

Source: Internet
Author: User

The string object is unchangeable, including the length and any characters in it.

String STR = "";

STR = STR + "B ";

The code above makes people mistakenly think that STR can increase the length. In fact, the first sentence of code is to re-create a new object, and 2nd objects are discarded, it will become the object collected by the garbage collector.

Stringbuilder is variable. It is a class used to perform dynamic operations on strings and characters.

Stringbuilder sb = new stringbuilder ("");

SB. append ("B ");

Stringbuilder has an internal character array that is long enough to store string objects. When the string length does not exceed the character array length, all operations are performed on the same character array. When the length exceeds, stringbuilder automatically creates a longer array and copies the original array to the new array.

String common operations:

1. Take the string length

String STR = "China ";
Int Len = Str. length; // obtain the length of the STR string.

2. convert a string to a bit code
Byte [] bytstr = encoding. Default. getbytes (STR );
Then we can get the bit length.
Len = bytstr. length;

3. String Addition
Stringbuilder sb = new stringbuilder ();
SB. append ("you ");
SB. append ("good ");
It works the same as STR = "you" + "good", but stringbuilder has better performance.

4. truncate part of the string

Variable. substring (starting position, number of truncated digits );

String sq = Str. substring (0, 3 );

5. Check whether the specified position is a null character.

Char. iswhitespce (STRING variable, number of digits); // string starts from 0

Char. iswhitespace (STR, 2 );

6. Check whether the character is a punctuation mark.

Char. ispunctuation ("");

7. Convert characters to numbers
(INT) '中 ';

8. convert numbers to characters
(Char) 22269;

9. Clear the spaces before and after the string

String STR = "China ";
STR = Str. Trim ();

10. Replace string

String variable. Replace (replace the original string with the new string)

String STR = "China ";
STR = Str. Replace ("country", "Central"); // Replace "country" with "Central"

Formats of several output strings:

1234. tosstring ("N"); // generate 12,345.00

1234. tosstring ("C"); // generate $12,345.00

1234. tosstring ("e"); // generate 1.234500e + 004

1234. tosstring ("F4"); // generate 12345.0000

1234. tosstring ("X"); // generate 3039 (hexadecimal)

1234. tosstring ("p"); // generate 1,234,500.00%

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.