Java Learning Note-the Nineth chapter string

Source: Internet
Author: User

The Nineth Chapter strings string

  1. Java uses the string class to create a string variable, which is a class-type variable and is an object. The syntax format for declaring a string is as follows:string S;

  2. Create a string: You can create a string by using the construction method provided by the string class in the following ways:

    (1) Assign a value directly when creating a string object, for example:

    String S1 = "Hello";

    String s2 = new string ("Hello");

    (2) Create another string from a string, for example:

    String S1 = "Hello";

    String s2 = new string (S1);

    (3) Creates a string from a character array, for example:

    Char[] C = {' A ', ' B ', ' C '};

    string s = new string (c);

  3. gets the length of the string : Use the Length () method to get the lengths of a string.

  4. string Connection: In addition to the "+" operator, there is also the concat () method, for example:

    String S1 = "Hello";

    String s2 = "World";

    String s3 = s1.concat (S2);

  5. Comparison of strings:

    (1) Equals method: Compares the contents of the current string object with the contents of the string object specified by the parameter. For example:

    string S1 = new String ("Hello");

    String s2 = new string ("Hello");

    S1.equals (2); Result is True

    It is not possible to compare strings for equality with "= =", when you compare two objects with "= =", you are actually judging whether two strings are the same object. Therefore, even if the string contents are the same, the return value is false because it is a different object (corresponding to the reference address), that is, the result of the expression "S1==s2" is false.

  6. Euqalsignorecase Method: ignores case to compare whether the contents of two strings are the same. For example:

    string S1 = new String ("Hello");

    String s2 = new String ("Hello");

    S1.equalsignorecase (S2); Result is True

  7. CompareTo Method: compares the size of two characters in a dictionary order. For example:

    string s = new String ("ABCDE");

    S.compareto ("Boy"); Less than 0

    S.compareto ("ABA")//greater than 0

    S.compareto ("ABCDE")//Equals 0

  8. string Retrieval:

    (1) indexOf (string s): retrieves the string s from the head of the current string, returning the position of the first occurrence of S. If none is put back-1; (Specifies that the argument string s can also be a specified character char a)

    (2) lastIndexOf (string s): retrieves the current string, returns the position of the last occurrence of S, and returns 1 if none.

    For example:

    string str = new String ("I am a Good cat");

  9. Interception of strings:

    (1) substring (int index): truncate from the index position of the current string to the end to get a substring.

    (2) substring (int start,int end): starts at the start (including start) position of the current string and intercepts the end (not including end) position to get a substring.

    For example:

    String S1 = "HelloWorld";

    String s2 = s1.substring (2); S2 to Lloworld

    String s3 = s1.substring (3,5); S3 for Lo

  10. String Case Conversion:

    (1) tolowercase (): converts all characters of the current string to lowercase.

    (2) touppercase (): converts all characters of the current string to uppercase.

    For example:

    string s = new String ("Hello");

    S.tolowercase (); Hello

    S.touppercase (); HELLO

  11. character substitution:

    (1) Replace (char old, char new): replaces the current string with the character specified by the old with the characters specified by new.

    (2) replaceall (string old, string new): replaces the string specified by old in the current string with the string specified by new.

    (3) Trim (): remove space from the end of the current string

    For example:

    string s= "I Mist sheep ";

    string S1 = "I am a student";

  12. string conversion to numeric value: by calling the class method parseint (string s) of the integer class, you can convert a number-formatted string, such as "1234", to the int type data. For example:

    String s = "1234";

    int x = Integer.parseint (s); x=1234

    Similar other data types also have corresponding class methods that convert strings to the corresponding base type. such as Parsebyte (), Parseshort (), parsedouble (), Parselong ().

  13. numeric conversions to strings: Use the valueof method of the string class to convert a numeric value to a string. For example:

    String s = string.valueof (789.456); S is "789.456"

  14. StringBuffer class: used to create and manipulate dynamic strings, the memory allocated for the class object is automatically expanded to accommodate the new text. Applies to handling mutable strings.

    (1) Append () method: appends the given character to the end of the current string.

    (2) Insert () method: Inserts a character at the specified position of the current string.

    Either append or insert is manipulated on the basis of the original object, and no new objects are created.

Java Learning Note-the Nineth chapter string

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.