String and stringbuilder

Source: Internet
Author: User

C # supports two types of strings: Rule string and verbatim string.

I previously saw that when the @ + "" structure is used to process strings, I just wrote down how to use it. In fact, this is a standard verbatim string and the characters between separators are interpreted word-by-word, the only exception is quotation marks. In a verbatim string, a string does not process simple escape sequences, hexadecimal and Unicode escape sequences, and can span multiple rows. For example:

 

Static void main (string [] ARGs)
{
String S = "this major \" teacher \ "is too Spam! ";
String S1 = @ "this major" "teacher" "is too Spam! ";
Console. writeline ("s-{0}", S1 );
Console. writeline ("S1-{0}", S1 );
String S2 = @ "this major
Instructor
Too Spam! ";
Console. writeline ("S2-{0}", S2 );
Console. readkey ();

 The difference between string and stringbuilder can be summarized as stringbuilder is a variable character sequence similar to the string class. We all know that string is a new reference of a new value and must be allocated with new memory. In this way, we can use stringbuilder to improve performance when we change or connect many strings multiple times. First, we will demonstrate some representative methods and attribute usage of string and stringbuilder respectively: 

Using system;

Namespace tstring
{
Class Test
{
Static void main ()
{
// Connection string demonstration
String S1 = "today is ";
String S2 = "Tuesday! ";
String S = S1 + S2;
Console. writeline (s );
Console. writeline ("equivalent to: String. Concat (S1, S2) S = {0}", String. Concat (S1, S2 ));
// Verbatim access string demonstration
Console. writeline ("verbatim access string demonstration \ n Method 1 :");
Foreach (char C in S) // method 1
{
Console. Write (C + "");
}
Console. writeline ("\ n method 2 ");
Char [] STR = S. tochararray (); // copy the characters in S to the Unicode character array
Foreach (char R in Str)
{
Console. Write (R + "");
}
Console. writeline ("\ n method 3 ");
For (INT I = 0; I <S. length; I ++) // The index starts with 0, and the second is not the location of the Unicode character.
{
Console. Write (s [I] + "");
}
S = S1 + S2;
// Method for searching test characters
Console. writeline ("Method for searching test characters ");
Console. writeline ("the index of the first character matching \" today \ "is {0}", S. indexof ("today "));
String target = "Ido"; // note that I only found this experiment, comparing it with the target character by character.
Char [] anyof = target. tochararray ();
Int start = 0;
Int;
At = S. indexofany (anyof, start );
If (at>-1)
Console. writeline ("the index of the first character matching \" ID \ "in S is {0}", );
Else
Console. writeline ("(not found )");
Char [] n = {'I', s '};
// Example:
Char [] AR = {'I', s '};
Console. writeline (the index of the first character matching \ "is \" in "s is {0}", S. indexofany (AR ));
Console. writeline ("the substring corresponding to index 0-5 is {0}", S. substring (0, 5 ));
// Modify the string
Console. writeline ("modifying string test ");
// Insert
Console. writeline ("insert test ");
Console. writeline (S. insert (17, "What a nice weather! "));
// Padleft
Console. writeline ("padleft test ");
S = S. padleft (S. Length + 8 ,'♀');
Console. writeline (s );
// Padright
Console. writeline ("padright test ");
S = S. padright (S. Length + 8 ,'♂');
Console. writeline (s );
Console. readkey ();
}
}
}

 

 

: To reduce power consumption, only the string class can be demonstrated. You can study the strigbuilder class by yourself, which is similar to the string class. Several typical methods include append, appendformat, and tostring.

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.