Java Advanced Learning (i) String class

Source: Internet
Author: User
Tags require

The previous Java Basics series discussed Java's core concepts, especially object-oriented basics. In the Java advanced, I'll complement the Java basics and move on to the application level.

Most programming languages have the ability to handle strings (string). A string is an ordered set of characters, such as "Hello world!". In Java, strings are stored as String class objects. A method that invokes a string object that can implement string-related operations.

The string class is contained in the Java.lang package. This package is 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 use import to introduce the String class.

Creating a String

We used the class to create the object before. When you need to be aware, creating a String class object does not require a new keyword. Like what:

public class Test   
{public   
    static void Main (string[] args)   
    {   
        String s = "Hello world!";   
        System.out.println (s);                        
    }   

In fact, when you write a "Hello world" expression, the object is already created in memory. If you use the new string ("Hello world!"), a string object is created repeatedly.

An Object

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

String manipulation

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

"ABC" + S

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

Method effect

S.length () returns the length of the s string

S.charat (2) returns the character in the middle of the s string labeled 2

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

S.indexof ("Hello") returns the sub-string "Hello" subscript

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

S.endswith ("oo") determines if s ends with "oo"

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

= = can only determine if the string is saved in the same location. You need to use Equals () to determine whether the contents of the string are the same.

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 and "Hello nerd!" Equal.

S.trim () removes the space string before and after S, and returns a 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

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.