Java how to program (Third Edition) -- My java notes (5)

Source: Internet
Author: User
Chapter 10 string
1. The String class provides nine constructors to initialize the String object in various ways. Seven of them are as follows:
S1 = new String (); // no parameter constructor;
S2 = new String (s); // The constructor whose parameter is a String;
S3 = new String (charArray); // A function whose parameter is a character array;
S4 = new String (charArray, 6, 3); // The parameter is a character array and the second parameter is copied as the starting position. The third parameter // is the number of copied characters as the initial value;
S5 = new String (byteArray,); // The parameter is a bit array and the second parameter is copied as the starting position. The third parameter is the number of copied characters as the initial value;
S6 = new String (byteArray); // The parameter is a bit array;
S7 = new String (buffer); // The parameter is StringBuffer;
2. The charAt method returns the character at the specified position in the String. For example, String1.charAt (I); the getChars method obtains a substring of a String. For example: String1.getChars (, charArray, 0); the first parameter is the subscript of the Start copy in the copied string, and the second parameter is the number of characters to be copied, the third parameter is the target character array, and the fourth parameter is the starting subscript of the copied character in the character array.
3. string comparison: equals, inclusignorecase, CompareTo, and reginMathes. The condition signorecase is case-insensitive. When CompareTo returns 0, the two strings are equal, if the value-1 is returned, the string that calls this method is smaller than the comparison string. If the value 1 is returned, the reginMathes method compares the two string objects to see if they are equal, for example, String1.regionMatches (0, s1, 0, 5); the first parameter is the start subscript of the string that calls this method, the second parameter is the string to be compared, and the third parameter is to compare the start subscript of the string, the fourth parameter is the number of characters to be compared. True is returned only when the specified number of characters are compared in the dictionary.
When "=" is used to compare references, if the two references point to the same object in the memory, the result is true. When comparing the basic data types, if the two values are the same, the result is true.
4. The startsWith and endsWith methods of the String class are used to test whether the elements of the array start or end with some characters. For example, string [I]. startsWith ("st"); string [I]. startsWith ("art", 2) determines whether the substring starting with subscript 2 in strings starts with the character "art"; string [I]. endsWith ("ed ");
5. The indexOf method locates characters and substrings in strings, for example, String1.indexOf ('C'); String1.indexOf ('A', 1 );
The second parameter is the starting subscript for starting search in the string.
6. the substring method is used to extract a substring from a string. It has two forms, for example, String1.substring (20). Among them, 20 is the start subscript of the substring in the source string; String1.substring ); the first parameter specifies the start subscript to be copied in the source string, and the second parameter specifies the number of copied characters.

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.