The note of learning ASP. NET [15] about the use of. Net string classes

Source: Internet
Author: User
Strings are the most commonly used data types during development. The following describes the attributes and methods of string classes:
  • Empty attribute string. Empty represents an empty string, but not equal to null. null is the state of the object not initialized.
    You can use expressions such as emptystring = string. Empty to determine whether the emptystring variable is a null string.
  • The Length attribute returns the number of characters that call this method. For example, myname = "Arvin Rong ",
    Myname. Length returns the int value 10.
  • For example, if the Chars attribute contains a string myname = "Arvin Rong", you can use myname [0] to obtain the string.
    Or replace 0 with any number smaller than the string length. The returned value is of char type.
  • // Method for comparing strings ------------------------------------------------------------------
  • The compare method uses two strings as parameters. If the first parameter is smaller than the second parameter, a negative number is returned.
    0 is returned. If the value is greater than the second parameter, a positive number is returned. The size here refers to the size of the character ASCII code, compare Method
    Is a character-by-character comparison, first compare the first character of the two strings, if not equal, return results (positive, 0 or negative), if
    If the value is equal, the last character of the character string with a shorter length is compared.
  • The equals method uses two strings as the parameter to compare whether the two strings are equal. String. Equals (stringa, stringb)
  • The compareto method is called by the first string, and the second string is used as the parameter. The returned results are the same as those of compare.
  • // Method for retrieving strings --------------------------------------------------------------------
  • Startswith method, endswith method, and contains method are all called by the first string, and the second string is passed in as a parameter,
    Returns true if the second string is retrieved from the first string.
  • The indexof method is called by the first string, and the second string is passed in as a parameter. If the second string is retrieved from the first string
    Returns the position of the second string in the first string. Example: myname = "Arvin Rong"; myname. indexof ("rong ")
    Returns the value 6.
  • // Method for editing strings --------------------------------------------------------------------
  • The insert method is called by the first string to add another string to the specified position of the first string.
  • The remove method is called by a string to remove a string from the string. Example: myname = "Arvin Rong ";
    Myname. Remove (5) returns the string "Arvin", or myname. Remove (5, 1) returns the string "arvinrong"
    5 indicates the start position, and 1 indicates the number of characters to be removed. If this parameter is not set by default, the start position to the last character will be removed.
  • The replace method is called by a string. Two strings are input as the parameter. The first string is the target string to be replaced, and the second string.
    Is the actual string to be replaced.
  • The TRIM method is called by a string to remove spaces on both sides of the string. Example: myname. Trim ();
  • The trimstart method is called by a string to remove spaces at the front end of the string.
  • The trimend method is called by a string to remove spaces at the end of the string.
  • The padleft method. The padright method uses the string call to input an integer value as a parameter and sets a length. If the string used to call this method is insufficient
    Length. Use spaces or specific characters. For example, myfirstname = "Arvin"; myfirstname. padleft (7) will
    The return string "Arvin". Two spaces are added before Arvin to supplement the length of the string to 7 characters. We can also call myfirstname. padleft (7 ,'-')
    The second parameter '-' is used to set the characters that we need to supplement the length. The result of this call is the returned string "-- arvin"
  • The toupper and tolower methods are called by strings to return strings in uppercase or lowercase.
  • The substring method is used to extract a specific string from a string. There can be one or two parameters. The first parameter is the position to start the truncation,
    The second parameter is the truncation length. If the second parameter is not provided, it is extracted to the end of the string by default. Example: myname = "Arvin Rong"; myname. substring (6)
    Will return "rong", myname. substring (6, 2) will return "Ro ".

    The substring method can be used with the indexof method, for example, myname = "Arvin-rong ";
    Myname. substring (myname. indexof ('-') + 1) returns the string "rong"

  • The split method is called by a string, and a string or character array containing a separator is input as a parameter. The String Array generated after the Delimiter is separated by the input is returned.
    Example: teststring = "stringa, stringb/stringc"; char [] separatorlist = {',', '/'}; teststring. Split (separatorlist );
    The returned string array contains three strings: "stringa", "stingb", and "stringc"
  • Note that the preceding Method for string editing does not change the string of the method called, but creates a new string and returns it.
    For example:
    Myname = "Arvin Rong"; tempstring = myname. Remove (5) returns the string "Arvin" assigned to tempstring;
    However, the value of myname itself is still "Arvin Rong ";
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.