Java Advanced Chapter "Lesson One" string class

Source: Internet
Author: User

Reference

The string class is contained in the Java.lang package. This package will be automatically import when Java is started, so it can be used as a built-in class (built-in classes). We do not need to explicitly introduce the string class using import.

Create

The string class is the only class that does not require the new keyword to create an object. Need to be aware of when using

        String s = "Hello world!" ;        System.out.println (s);           

Operation

You can use + to implement a string connection (concatenate), such as:

"ABC" + S

The operation of the string is mostly implemented by the corresponding method of the string, such as the following method:

Method effect

S.length () returns s string length

S.charat (2) returns the characters in the S string labeled 2

S.substring (0, 4) returns a substring of 0 to 4 in the S string

S.indexof ("Hello") returns the subscript of the substring "Hello"

S.startswith ("") determines whether s starts with a space

S.endswith ("oo") to determine if S is "oo" end

S.equals ("Good world!") Determine if S equals "good world!"

S.compareto ("Hello nerd!") Compare S string with "Hello nerd!" In the order of the dictionaries,

Returns an integer if <0, stating S in "Hello nerd!" Before

If >0, explain s in "Hello nerd!" After

If ==0, explain s with "Hello nerd!" Equal.

S.trim () Remove the space string around s and return the new string

S.touppercase () converts s to uppercase and returns a new string

S.tolowercase () converts s to lowercase and returns a new string

S.replace ("World", "Universe") replaces "world" with "Universe" and returns a new string

Immutable objects

The String class object is immutable (Immutable object). A programmer cannot modify an existing immutable object. We can also create immutable objects ourselves, as long as we do not provide a way to modify the data in the interface.

However, the string class object does have the ability to edit strings, such as replace (). These editing functions are implemented by creating a new object, rather than modifying the original object. Like what:

s = S.replace ("World", "Universe");

The call to S.replace () on the right will create a new string, "Hello universe!", and return the object (the reference). By assigning a value, the reference s will point to the new string. If no other references point to the original string "Hello world!", the original string object will be garbage collected.

Api

Java offers a number of powerful packages. An important aspect of Java learning is understanding these packages and the APIs contained therein (application programming Interface). The String class is defined in java.lang.String. You can find the official documentation for this class by querying the following Oracle URLs:

Http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

This document contains the most comprehensive description of the string class.

In fact, the API documentation contains a wealth of content that you'll get at a glance through the following links:

http://docs.oracle.com/javase/6/docs/api/

Java Advanced Chapter "Lesson One" string class

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.