Home > Java > JavaBase > What are the commonly used methods in java

What are the commonly used methods in java

王林
Release: 2019-11-16 15:00:19
Original
8074 people have browsed it

What are the commonly used methods in java

1. trim() method

The trim() method is used to remove the leading and trailing whitespace characters of a string.

Example:

public class Test {
    public static void main(String args[]) {
        String Str = new String("    www.runoob.com    ");
        System.out.print("原始值 :" );
        System.out.println( Str );

        System.out.print("删除头尾空白 :" );
        System.out.println( Str.trim() );
    }
}
Copy after login

Result:

原始值 :    www.runoob.com    
删除头尾空白 :www.runoob.com
Copy after login

2. split() method

split( ) method splits a string based on matching the given regular expression.

Note: Escape characters such as ., | and * must be added with \\.

Note: Multiple separators, you can use | as a hyphen.

Syntax:

public String[] split(String regex, int limit)
Copy after login

Parameters:

##regex -- Regular expression delimiter.

limit -- The number of divided parts.

Return value:

String array.

3. charAt() method

The charAt() method is used to return the character at the specified index. The index range is from 0 to length() - 1.

Syntax:

public char charAt(int index)
Copy after login

Parameters:

index -- The index of the character.

Return value:

Returns the character at the specified index.

Instance:

public class Test {

    public static void main(String args[]) {
        String s = "www.runoob.com";
        char result = s.charAt(8);
        System.out.println(result);
    }
}
Copy after login

Result:


o


4. substring() method

The substring() method returns the substring of a string.

Syntax:

public String substring(int beginIndex)
或
public String substring(int beginIndex, int endIndex)
Copy after login

Parameters:

beginIndex<span style="color: rgb(20, 25, 30); font-family: "Microsoft Yahei", "Hiragino Sans GB", Helvetica, "Helvetica Neue", 微软雅黑, Tahoma, Arial, sans-serif; font-size: 14px;"></span> -- Starting index (inclusive), index starts from 0.

endIndex -- End index (not included).

Return value

Substring.

Example:

public class Test {
    public static void main(String args[]) {
        String Str = new String("www.runoob.com");
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4) );
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4, 10) );
    }
}
Copy after login

Result:


返回值 :runoob.com
返回值 :runoob
Copy after login
Recommended tutorial:

Java tutorial

The above is the detailed content of What are the commonly used methods in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template