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

What are the commonly used methods in java

王林
王林Original
2019-11-16 15:00:198068browse

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() );
    }
}

Result:

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

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)

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)

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);
    }
}

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)

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) );
    }
}

Result:


返回值 :runoob.com
返回值 :runoob

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!

Statement:
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