C # string

Source: Internet
Author: User
String processing

• A single character in C # is of the char type ('A') enclosed in single quotes and can only be put in one character. ASCII code

• A single character can also be expressed as a string or a string of 0 characters. Difference between null and "", String. isnullorempty • Use the S. Length attribute to obtain the number of characters in the string • string can be seen as a read-only array of char. Char c = s [1];. Example: traverse each element in the output string. • The character string in C # has an important feature: immutable. Once declared, the character string can no longer be changed. Therefore, only the char at the specified position can be read through the index, and the char at the specified position cannot be modified.

If you want to modify char, you must create a new string using S.
The tochararray () method gets the char array of the string. After modifying the array, call the new string (char []) constructor (no further study is required) to create the char array string. Once a string is created, the modification of the char array will not change the string. Example: replace a with a in the string

// The string is immutable. Therefore, you must convert it into a char [] And then modify the char [].

// Generate a new string based on char [], but the original string has not changed (he is there)

// You can create a model based on your life, or generate an adult based on the model, but you cannot modify the model.

// In this way, the model is generated based on LI Yong, and then the face is shortlisted. Then, LI Yong 2 and LI Yong are two objects.

// S = new string (s); equivalent to LI Yong 2 killing LI Yong and replacing LI Yong. It seems that LI Yong's face has become shorter, but it is two instances.

// String S = "ABC"; declares a variable S and points s to the string "ABC.

// Strings1 = "123456 ";

// Char [] chars1 = s1.tochararray (); // copy a char according to S1 []

// Chars1 [0] = 'W'; // S1 is not affected.

// String S2 = new string (chars1); // generate a new string based on chars1

// Console. writeline (S2 );

// Console. writeline (S1 );

// Chars1 [0] = 'C'; // does not affect S2

// Console. writeline (S2 );

// Interview exercise questions: Convert string to uppercase, lowercase, flip, inttostring

Is a copy process:

// Strings1 = "123456 ";

// Char [] chars1 = s1.tochararray (); // copy a char according to S1 []

// Chars1 [0] = 'W'; // S1 is not affected.

// String S2 = new string (chars1); // generate a new string based on chars1

// Console. writeline (S2 );

// Console. writeline (S1 );

// Chars1 [0] = 'C'; // does not affect S2

// Console. writeline (S2 );

String [] C = new string [] {"zero", "one", "two", "three", "Si", "Wu", "Lu ", "Week", "year", "month", "day"
};

Try

{

String input = "September 5, 2009 ";

Stringbuilderstr = new
Stringbuilder ();

For (intcount = 0; count <
Input. length; count ++)

{

String temp =
Input. substring (count, 1 );

Switch (temp)

{

Case "1 ":
Str. append (C [1]); break;

Case "2 ":
Str. append (C [2]); break;

Case "3 ":
Str. append (C [3]); break;

Case "4 ":
Str. append (C [4]); break;

Case "5 ":
Str. append (C [5]); break;

Case "6 ":
Str. append (C [6]); break;

Case "7 ":
Str. append (C [7]); break;

Case "8 ":
Str. append (C [8]); break;

Case "9 ":
Str. append (C [9]); break;

Case "0 ":
Str. append (C [0]); break;

Case "year ":
Str. append (C [10]); break;

Case "month ":
Str. append (C [11]); break;

Case "day ":
Str. append (C [12]); break;

Default: break;

}

}

This. button1.text =
Str. tostring ();

}

Catch (exception ex)

{

Throw new exception ("tested ");

}

It can be further optimized by using the look-up table method.


FAQ

• Who said the string is unchangeable? Strings = "ABC"; S = "123", isn't s changed? • Differentiate between variable names and values pointed to by variables. There can be many strings in the program, and then the string variables point to them. The variables can point to other strings, but the strings themselves have not changed. The immutable character string refers to the immutable character string in the memory, rather than the variable. • Strings10 = S1; // S10 points to the string pointed to by S1, rather than S10 points to S1. Even if S1 points to other memory, S10 points to "hello" • tolower (): returns the string in lowercase. • Note that the string is unchangeable, so these functions do not directly change the content of the string, but return the modified value of the string in the form of the function return value. S. tolower () and S = S. tolower () • toupper (): Obtain the string in upper case. Trim () removes the white space at both ends of the string (the middle is not removed ). • S1.equals (S2, stringcomparison. ordinalignorecase), two strings for case-sensitive comparison. This usage is recommended. • Strings2 = string. Format ("{0}-{1}-{2 ",
Year, month, day); • Use reflector to view the relationship between writeline and string. format. Look at the implementation of other programs and Microsoft's implementation! • Strings1 = string. Join ("|", values); // The first parameter is a string separator.

String segmentation

• String [] Split (Params char [] separator): Splits a string into a String Array Based on the specified delimiter. • string [] Split (char [] separator, stringsplitoptions options) splits the string into a String Array Based on the specified char delimiter (options removes the blank string in the result when removeemptyentries is used); • string [] Split (string [] separator, stringsplitoptions options) splits a string into a String Array Based on the specified string delimiter. • Example 1: year, month, and day are analyzed from the date string ("2008-08-08. • Example 2: a text document that records a student's score. Each student's score is one row and each row is separated by |, separated by | is the name, age, and score. Write a program to retrieve the name and score of the highest score student. Reference: Use string []
Lines = system. Io. file. readalllines (@ "C: \ Root. ini ",
Encoding. Default); reads data from a text file. The returned value is a string array, and each element is a row. (5 min)
Related Article

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.