Use of C # string strings

Source: Internet
Author: User

String Learning

The important character of a string: immutability; Whatever you do with the string, a new instance is generated in memory.

The staging pool for the string,

Property:

Length (): Gets the number of characters in the string

Method:

1, IsNullOrEmpty: The judgement string is null or ""; String str= ""; bool B = string. IsNullOrEmpty (str);

The difference between null and void: Empty has opened up space, storage is empty; null does not open space.

2. Can access the string by subscript: char ch=str[0]; returns the char type

3, ToCharArray (); Convert to character array, char[] ch = str. ToCharArray ();

Converts a character array to a string: string Str=new string (ch);

A string can be thought of as a read-only group of char types, so it can be accessed by subscript. Note that we can only access through the subscript can not be changed, Console.WriteLine (Str[3]) (right) can not str[3]= "A" (Error)

If you want to change the value of a string, you cannot assign a value directly because of the immutability of the string, but you can turn to a char array, change it, and then convert to string.

            string str = "Hello"; Change the string to "Hhllo"
            char[] ch = str. ToCharArray ();
            ch[1]= ' h ';
            ch[3]= ' o ';
            str = new string (ch);  This time the string has changed string
	    SS = Encoding.Default.GetString (ch,0,5);  Byte[] and String type conversions

4, ToLower: lowercase must accept the return value (because the character is not denatured), the operation will produce new characters (new space), must accept.

5, ToUpper: capital

6, Equals to determine whether two strings are the same, if (S1). Equals (S2)) {}

if (S1. Equals (S2,stringcomparison. OrdinalIgnoreCase)) {}//is case-insensitive when compared

for string, = = equals the value itself (as long as the value is the same, equal).

For value types, = = and equals are all addresses, but when equals are used in defined classes, they are typically overridden to compare to our own needs

7, Constain () to determine whether the inclusion of a character

8, Indexof () to find the first occurrence of a string position, if not found, return-1;

String str= "Hello"; int INDEX=STR. Indexof ("O", num)//looks for the first ' o ' position at the start of the subscript Num.

9, LastIndexOf () to find the last occurrence of a string position, if not found, return-1;

10. Substring () Intercept string

S1. Substring (1), S1 the string after the 1th character is intercepted. Substring (1,5); intercept the first consecutive 5-character start

11. Split () Split string: Splits a string into an array of strings

Chararray (): Dividing a string into a character array

      string [] ss=s1. Split (new char []{', ', '; ', '. ', '/', ' O '},stringsplitoptions.) Removeemptyentries); The following argument indicates that the space of the intercepted character is removed, and the string array
     string is converted to a character array Char[]:char ch[]= str. Chararray ()
     string string to string array strinig[]: string[]  ss=str.split (new  char[] {  

Split (new char[] {', '});//intercept characters in char[]

12, Join to place the specified characters behind each element, return the string

String. Join ("&", 34,56.99, "Hello", "can connect any type of data", "true");

            String[] St = {"W", "O", "L", "D"};
            String s=string. Join ("&", St);     St Here is an object array.
13, replace replacement
          String s2 = "Newnenfe";
          String s=s2. Replace  ("W", "&");

14. Trim () Remove the spaces at both ends of the string, trim (', ', ', ', ')//Remove the characters from the parentheses that appear on both sides of the string

TrimStart () Remove the space before the string

TrimEnd () Remove the space after the string

15, 1, flip the string:

   String S=new  string (array.reverse (char  new {' 1 ', ' e ', ' e ', F ',}));

2, the string date = "May 31, 2017";

           string[] da = date. Split (new char[] {' Year ', ' Month ', ' Day '},stringsplitoptions.removeemptyentries);
            foreach (var item in Chas)
            {
              Console.Write (item);
             }
16, read every line in the file

      string pth = @ "D:\vivang\desktop\a.txt";
            String[] St11=file.readalllines (PTH); 
            foreach (var item in st11)
           {
              Console.Write (item);
           }
17, Int. Parse (); Representation of a number as a string of 32-bit equivalent signed digits

Min=int. Maxvalue;??

18. ToString () is a virtual method of the object class that can be overridden or not rewritten.

To have an object ToString (), we get the namespace of the object, such as Class A; A.tostring ()

StringBuilder s=new StringBuilder ();

S.append ("1234"); Then S. ToString () does not get the namespace but the content of S, because the ToString () method has been overridden in class StringBuilder.


When you want to get the object's ToString () method, you can override the method.



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.