String reference type of C # in ASP. NET

Source: Internet
Author: User

I have talked about the String object of javascript before. Here we will talk about the string type of C #. I hope you can differentiate the two and learn different usage of string in the two languages.

First, the method for defining strings in C:

String str; // declare a string reference type variable str

Str = "Tom Cruise"; // str points to the string "Tom Cruise"

String str1 = "Audrey Hepburn"; // declare a string reference type variable str1 and set the Initial Value


Common Methods of string (in fact, it is very similar to the string of js, but the method names of the same function are different. I hope you will not confuse them)

1. string search (IndexOf () // here is the same as the js search function, but two parameters can be used in the js search function.

String str = "Audrey Hepburn ";

Int I = str. indexOf ("he"); // I = 3; the index of the first character starts with 0, and the return value of the IndexOf () method is int type; if no matching string is found,-1 is returned.


2. String comparison (Compare)

String str1 = "Tom Cruise ";

String str2 = "Tom ";

Int n = string. Compare (str1, str2); // n> 0

If n = 0, the two strings are the same. If n <0, str1 Str2


3. Find the string Length (Length) // js side is also Length, this is the same

String str = "Audrey Hepburn ";

Int I = str. Length; // I = 5


4. Substring // This usage is the same as that of js substr, but different from that of js substring.

String str = "123456 ";

String str1 = str. Substring (2, 2); // str1 = "34"

Str1 is an index that starts from 2 to 2 Characters in str.


5. Remove some strings)

String str = "123456 ";

String str1 = str. Remove (3456); // str1 ="

Str1 is to delete 2 Characters from str index from 0


6. Insert a string)

String str = "123456 ";

String str1 = str. Insert (3, ""); // str1 = "123 456"

Str1 starts from str index 3 and inserts the string "atango"


7. String replacement (Replace)

String str = "123456 ";

String str1 = str. Replace ("456", ""); // str1 = "123"

Str1 Replace "456" in str with the string "atango"


8. convert other data types to strings (ToString) // This is the most common

Stirng I = 456;

String str = I. ToString (); // str = "456"

Converts an integer value of 456 to a string of "456"


9. case-insensitive conversion (ToLower, ToUpper) // note the difference between the toLowerCase () and toUpperCase () methods in js. In fact, the function is the same, but the write is different.

String str = "AaBbCcDd ";

String str1 = str. ToLower (); // str1 = "aabbccdd", converts the character to lowercase

String str2 = str. ToUpper (); // str2 = "AABBCCDD", converts the character to uppercase


10. Remove leading and trailing spaces (Trim)

String str = "123456 ";

String str1 = str. Trim (); // str1 = "123456"


There are other methods, but these are commonly used and I hope to help you.

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.