Java常用方法

1.trim() 方法

trim() 方法用于删除字符串的头尾空白符。

public String trim()

实例:

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

以上程序执行结果为:

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

2.Java indexOf() 方法

indexOf() 方法有以下四种形式:

  • public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

public int indexOf(int ch )

或

public int indexOf(int ch, int fromIndex)

或

int indexOf(String str)

或

int indexOf(String str, int fromIndex)

参数

  • ch -- 字符,Unicode 编码。

  • fromIndex -- 开始搜索的索引位置,第一个字符是 0 ,第二个是 1 ,以此类推。

  • str -- 要搜索的子字符串。

返回值

查找字符串,或字符 Unicode 编码在字符串出现的位置:

public class Main {
    public static void main(String args[]) {
        String string = "aaa456ac";  
        //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
        System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
 
        // 从第四个字符位置开始往后继续查找,包含当前位置  
        System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6  
 
        //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
 
        // 从头开始查找是否存在指定的字符  
        System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7  
        System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7  
 
        //从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。  
        System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6  
        System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6  
    }
}

结果;

-1
6
7
7
6
6

实例2:

public class Test {
    public static void main(String args[]) {
        String Str = new String("菜鸟教程:www.runoob.com");
        String SubStr1 = new String("runoob");
        String SubStr2 = new String("com");
 
        System.out.print("查找字符 o 第一次出现的位置 :" );
        System.out.println(Str.indexOf( 'o' ));
        System.out.print("从第14个位置查找字符 o 第一次出现的位置 :" );
        System.out.println(Str.indexOf( 'o', 14 ));
        System.out.print("子字符串 SubStr1 第一次出现的位置:" );
        System.out.println( Str.indexOf( SubStr1 ));
        System.out.print("从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :" );
        System.out.println( Str.indexOf( SubStr1, 15 ));
        System.out.print("子字符串 SubStr2 第一次出现的位置 :" );
        System.out.println(Str.indexOf( SubStr2 ));
    }
}

结果

查找字符 o 第一次出现的位置 :12
从第14个位置查找字符 o 第一次出现的位置 :17
子字符串 SubStr1 第一次出现的位置:9
从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :-1
子字符串 SubStr2 第一次出现的位置 :16

3.split() 方法

split() 方法根据匹配给定的正则表达式来拆分字符串。

注意: . 、 | 和 * 等转义字符,必须得加 \。

注意:多个分隔符,可以用 | 作为连字符。

语法

public String[] split(String regex, int limit)

参数

  • regex -- 正则表达式分隔符。

  • limit -- 分割的份数。

返回值

字符串数组。

实例

public class Test {
    public static void main(String args[]) {
        String str = new String("Welcome-to-Runoob");
 
        System.out.println("- 分隔符返回值 :" );
        for (String retval: str.split("-")){
            System.out.println(retval);
        }
 
        System.out.println("");
        System.out.println("- 分隔符设置分割份数返回值 :" );
        for (String retval: str.split("-", 2)){
            System.out.println(retval);
        }
 
        System.out.println("");
        String str2 = new String("www.runoob.com");
        System.out.println("转义字符返回值 :" );
        for (String retval: str2.split("\.", 3)){
            System.out.println(retval);
        }
 
        System.out.println("");
        String str3 = new String("acount=? and uu =? or n=?");
        System.out.println("多个分隔符返回值 :" );
        for (String retval: str3.split("and|or")){
            System.out.println(retval);
        }
    }
}

以上程序执行结果为:

以上程序执行结果为:

- 分隔符返回值 :
Welcome
to
Runoob

- 分隔符设置分割份数返回值 :
Welcome
to-Runoob

转义字符返回值 :
www
runoob
com

多个分隔符返回值 :
acount=? 
 uu =? 
 n=?

4.charAt() 方法

charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。

语法

public char charAt(int index)

参数

  • index -- 字符的索引。

返回值

返回指定索引处的字符。

实例

public class Test {

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

以上程序执行结果为:

o

5.substring() 方法

substring() 方法返回字符串的子字符串。

语法

public String substring(int beginIndex)

或

public String substring(int beginIndex, int endIndex)

参数

  • beginIndex -- 起始索引(包括), 索引从 0 开始。

  • endIndex -- 结束索引(不包括)。

返回值

子字符串。

实例:

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

以上程序执行结果为:

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

6.replace() 方法

replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。

语法

public String replace(char oldChar,char newChar)

参数

  • oldChar -- 原字符。

  • newChar -- 新字符。

返回值

替换后生成的新字符串。

实例

public class Test {
    public static void main(String args[]) {
        String Str = new String("hello");

        System.out.print("返回值 :" );
        System.out.println(Str.replace('o', 'T'));

        System.out.print("返回值 :" );
        System.out.println(Str.replace('l', 'D'));
    }
}

以上程序执行结果为:

返回值 :hellT
返回值 :heDDo

7.parseInt() 方法

parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析。

如果方法有两个参数, 使用第二个参数指定的基数,将字符串参数解析为有符号的整数。

语法

所有 Number 派生类 parseInt 方法格式类似如下:

static int parseInt(String s)

static int parseInt(String s, int radix)

参数

  • s -- 十进制表示的字符串。

  • radix -- 指定的基数。

返回值

  • parseInt(String s): 返回用十进制参数表示的整数值。

  • parseInt(int i): 使用指定基数的字符串参数表示的整数 (基数可以是 10, 2, 8, 或 16 等进制数) 。

实例:

public class Test{
    public static void main(String args[]){
        int x =Integer.parseInt("9");
        double c = Double.parseDouble("5");
        int b = Integer.parseInt("444",16);

        System.out.println(x);
        System.out.println(c);
        System.out.println(b);
    }
}

编译以上程序,输出结果为:

9
5.0
1092

7.arraycopy() 方法

arraycopy()方法用于将一个数组的元素快速拷贝到另一个数组。

语法

System.arraycopy(src, srcPos, dest, destPos, length);

参数

  • src表示源数组

  • srcPos表示源数组中拷贝元素的起始位置。

  • dest表示目标数组

  • destPos表示拷贝到目标数组的起始位置

  • length表示拷贝元素的个数

实例

public class Main{
    public static void main(String[] args)throws Exception{
        int[] formArray={101,102,103,104,105,106};
        int[] toArray={201,202,203,204,205,206,207};
        System.arraycopy(formArray, 2, toArray, 3, 2);
        for(int i=0;i<toArray.length;i++){
            System.out.println(i+":"+toArray[i]);
        }
    }
}
原文地址:https://www.cnblogs.com/aiguona/p/10455914.html