String, stringbuilder, statement type, escape character lesson 4

Source: Internet
Author: User
Tags control characters
String class, stringbuilder class should be a common class in project development.
In
StringA character array declared by the keyword, string S = "Hello, world! ";
When considering the performance of the connection string, it is best to use the stringbuilderstring connection string.
String S1 = "orange ";
String S2 = "red ";

S1 + = S2;
System. Console. writeline (S1 );
// Output the connection string of "orangered" stringbuilder
System. Text. stringbuilder sb = new system. Text. stringbuilder ();
SB. append ("one ");
SB. append ("two ");
SB. append ("three ");
String STR = sb. tostring ();
System. Console. writeline (STR );
// Output onetwothree

A string can contain escape characters, such as "\ n" (new line) and "\ t" (Tab ),
There is a special symbol @
@The symbol tells the string constructor to ignore the Escape Character and the branch character,
The results are as follows:
String p1 = "\\\\ My Documents \ My files \\";
String P2 = @ "\ My Documents \ My files \";

There are several common methods in string:
1. Split
This method returns a string array where each element is a word split.
Class teststringsplit
{
Static void main ()
{
Char [] delimiterchars = {'', '.', ':', '\ t '};

String text = "one \ tTwo three: four, five six seven ";
System. Console. writeline ("original text: '{0}'", text );

String [] words = text. Split (delimiterchars );
System. Console. writeline ("{0} words in text:", words. Length );

Foreach (string s in words)
{
System. Console. writeline (s );
}
}
}

The output result is:

Original text: 'one     two three:four,five six seven'7 words in text:onetwothreefourfivesixseven
2. Search for strings
 ,
 ,
 And
 Method
SEARCH strings
Class stringsearch
{
Static void main ()
{
String STR = "a silly sentence used for silly purposes .";
System. Console. writeline ("'{0}'", STR );

Bool test1 = Str. startswith ("A silly ");
System. Console. writeline ("starts with 'a silly '? {0} ", test1 );

Bool Test2 = Str. startswith ("A silly", system. stringcomparison. ordinalignorecase );
System. Console. writeline ("starts with 'a silly '? {0} (ignoring case) ", Test2 );

Bool test3 = Str. endswith (".");
System. Console. writeline ("ends '.'? {0} ", test3 );

Int first = Str. indexof ("silly ");
Int last = Str. lastindexof ("silly ");
String str2 = Str. substring (first, last-first );
System. Console. writeline ("between two 'silly 'words: '{0}'", str2 );
}
}
Output:
'A silly sentence used for silly purposes .'
Starts with 'a silly '? False
Starts with 'a silly '? True (ignore case)
Ends '.'? True
Between Two 'silly 'words: 'silly sentence used'


3. Modify the string
 Method
The string is unchangeable, so the content of the string cannot be modified. However, you can extract the content of a string to a non-Immutable form and modify it to form a new string instance.
 Method to extract the content of a string to an array of the char type. Then modify some elements in the array. Then, useCharCreate a string instance in several groups
String Modification
Class modifystrings
{
Static void main ()
{
String STR = "The quick brown fox jumped over the fence ";
System. Console. writeline (STR );

Char [] chars = Str. tochararray ();
Int animalindex = Str. indexof ("Fox ");
If (animalindex! =-1)
{
Chars [animalindex ++] = 'C ';
Chars [animalindex ++] = 'a ';
Chars [animalindex] = 'T ';
}

String str2 = new string (chars );
System. Console. writeline (str2 );
}
}




Appendix: escape characters
Escape characters
General characters
Except. $ ^ {[(|) * +? \, Other characters match themselves.
 
\
It matches the Bell (Alarm) \ u0007.
 
\ B
If it is in the [] character class, it matches with the Escape Character \ u0008. If this is not the case, see the "NOTE" section after this table.
 
\ T
Match with the tab \ u0009.
 
\ R
Match the carriage return \ u000d.
 
\ V
Match the vertical tab \ u000b.
 
\ F
Match with the newline \ u000c.
 
\ N
Match the linefeed \ u000a.
 
\ E
Matches the ESC character \ u001b.
 
\ 040
Match the ASCII character to the octal number (up to three digits). If the number without leading zero is only one digit or corresponds to the capture group number, the digit is backward referenced. (For more information, see reverse reference .) For example, the character \ 040 represents a space.
 
\ X20
The hexadecimal representation (exactly two digits) matches the ASCII character.
 
\ CC
Matches ASCII control characters. For example, \ CC is Ctrl-C.
 
\ U0020
The hexadecimal representation (exactly four digits) matches the Unicode character.

Note:
. NET Framework does not support Perl 5 character escaping for specified Unicode. The Perl 5 character escape format is \ x {####...}, "####…" It is a sequence of hexadecimal numbers. Use the. NET Framework character escape described in this line instead.
 
 
\
It matches the character that is not recognized as an escape character. For example, \ * is the same as \ x2a.
 
Note:
Escape Character \ B is a special case. In a regular expression, \ B indicates the word boundary (between \ W and \ W). However, in the [] character class, \ B indicates the return character. In the replacement mode, \ B always indicates the return character.
 

 
Why is it more efficient to use stringbuilder to connect strings than string? It will be written in the C # analysis directory. Of course, this is my opinion and it is not necessarily a positive solution,
Hope that the high people can know.

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.