Java basics --- string, java --- string

Source: Internet
Author: User

Java basics --- string, java --- string

1. Concept of Character Creation

A java string is a Unicode Character Sequence. For example, the string "Java \ u2122" consists of five Unicode characters J, a, v, a, and TM. Java does not have a built-in String type, but provides a predefined String class in the standard library Java class library.

2. substrings

The substring method of the String class can extract a substring from a large String.

String    greeting ="Hello";String s = greeting.substring(0,3);

Creates a string consisting of the character "El. The second parameter of the substring method is the first location that you do not want to copy. Here we need to copy the characters from 0, 1, and 2 (from 0 to 2, including 0 and 2. In substring, it starts from 0 until 3, but does not contain 3.

Substring has the following advantages: it is easy to calculate the length of a substring. The length of string s. substring (a, B) is ba. For example, the length of the sub-string "El" is 3-0 = 3.

3. Splicing

Like most programming languages, Java allows two strings to be connected using the plus sign (+.

String  str1 = "I Love java!";String str2= "I Love China!";String str3 = str1+str2;

I Love Java! I Love China "is assigned to str3; e (note that there is no space between words, and the + sign concatenates two strings in the given order ). When a string is concatenated with a non-string value, the latter is converted to a string.

4. Immutable string

The String class does not provide a method to modify the String. If you want to change the greeting content to "Help !", You cannot directly change the characters at the last two locations of greeting to 'p' and '! '! However, it can be replaced to make a "change.

Greeting = greeting. substring (0, 3) + "p! ";

The preceding statement changes the current value of greeting to Help!

Because the characters in the Java String cannot be modified, the String class object in the Java document is called an immutable String, just as number 3 is always a number 3, the string "Hello" always contains the code unit sequence of characters H, e, l, l, and o, but cannot modify any of them. Of course, you can modify the string variable greeting to reference another character string, just as you can change the value variable that stores 3 to 4.

5. Check whether the strings are equal

You can use the equals () method to check whether the content of two strings is equal.

S. equals (t); returns true if string s is equal to string t; otherwise, returns false. Note that s and t are both string variables or string constants.

You must not use "=" to check whether two strings are equal! This operator can only determine whether two strings are not placed in the same position. When the content and address are equal, the two strings are equal. Of course, if strings are placed in the same position, they must be equal, but it is entirely possible that multiple strings with the same content are copied and placed in different locations.

String greeting = "hello ";

If (gerrting = "hello ")

// Probably true

If (greeting. substring (0, 3) = "hel ")

// Probably false;

If the virtual machine always shares the same string, you can use the "=" operator to determine whether it is equal. However, only string constants are shared, the results produced by operations such as + or substring are not shared.

6. String API

• Char charAt (int index) returns the code unit at the given position. You do not need to call this method unless you are interested in the underlying code unit.

• Int codePointAt (int index) 5.0 returns the code point that begins or ends at a given position.

• Int offsetByCodePoints (int startIndex, int cpCount) 5.0 returns the code index after the cpCount is shifted from the startIndex code point.

• Int compareTo (String other) returns a negative number in alphabetical order if the String is prior to the other. If the String is after the other, a positive number is returned. If the two strings are equal, 0 is returned.

• Boolean endsWith (String suffix) returns true if the String ends with suffix.

• Boolean equals (Object other) returns true if the string is equal to other.

• Boolean inclusignorecase (String other) returns true if the String is equal to other (case-insensitive.

• Int index0f (String str)

• Int index0f (String str, int fromIndex) Java basic programming structure 47 java. lang. string 1.0

• Int index0f (int cp) • int index0f (int cp, int fromIndex) returns the starting position of the first substring that matches the string str or code point cp. This position starts from index 0 or fromIndex. If str does not exist in the original string,-1 is returned.

• Int lastIndex0f (String str)

• Int lastIndex0f (String str, int fromIndex)

• Int lastindex0f (int cp) • int lastindex0f (int cp, int fromIndex) returns the starting position of the last substring that matches the string str or code point cp. This position is calculated from the end of the original string or fromIndex.

• Int length () returns the length of the string.

• Int codePointCount (int startIndex, int endIndex) 5.0 returns the number of code points between startIndex and the endIndex-1. Substitute characters without matching will be included in the Code Point.

• String replace (CharSequence oldString, CharSequence newString) returns a new String. This string replaces all oldstrings in the original string with newString. You can use a String or StringBuilder object as the CharSequence parameter.

• Boolean startsWith (String prefix) returns true if the String starts with a preffix String.

• String substring (int beginIndex) • String substring (int beginIndex, int endIndex) returns a new String. This string contains all the code units in the original string from beginIndex to the end of the string or the endIndex-1.

• String toLowerCase () returns a new String. This string changes all uppercase letters in the original string to lowercase letters.

• String toUpperCase () returns a new String. This string changes all lowercase letters in the original string to uppercase letters.

• String trim () returns a new String. This string removes spaces at the header and tail of the original 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.