溫馨提示×

java字符串函數有哪些

養魚的貓咪
461
2021-04-19 12:13:32
欄目: 編程語言

java中常用的字符串函數有:1.equals()函數,比較字符串是否相等;2.split()函數,拆分字符串;3.replace()函數,替換字符串;4.indexof()函數,返回字符索引;5.substring()函數,返回子字符串;6.concat()函數,連接字符串;7.toLowerCase()函數,將字符串轉換為小寫;8.toUpperCase()函數,將字符串轉換為大寫;9.length()函數,返回字符串長度;

java字符串函數有哪些

java中常用的字符串函數有以下幾種

1.equals()函數

java中equals()函數的作用是用于比較兩個字符串的變量值是否相等。

s1 = new String("abc");s2 = new String("abc");s1.equals(s2) //返回 true

2.split()函數

java中split()函數的作用是用于根據給定的正則表達式對字符串進行拆分。

String str = "one two three, four";

String[] tokens = str.split(" ");

for (String s: tokens)

System.out.println(s);


3.replace()函數

java中replace()函數的作用是用于將字符串中某一個相同的信息替換為另外一個字符串。

String a = "hello wrold!";

String b = "China!";

a = a.replace("wrold!",b);

System.out.println(a); //返回hello China!


4.indexof()函數

java中indexof()函數的作用是用于返回一個字符在字符串中的索引,如字符串中沒有該字符,則返回-1。

String str = "01234567890123456789";System.out.println(str.indexOf("123")); //返回 1System.out.println(str.indexOf("321")); //返回 -1

5.substring()函數

java中substring()函數的作用是用于返回字符串的子字符串。

public class RunoobTest {

    public static void main(String args[]) {

        String Str = new String("This is text");

        System.out.print("返回值 :" );//返回is text

        System.out.println(Str.substring(4) );

        System.out.print("返回值 :" );//返回is te

        System.out.println(Str.substring(4, 10) );

    }

}

6.concat()函數

java中concat()函數的作用是用于將指定的字符串參數連接到字符串上。

public class Test {

    public static void main(String args[]) {

        String s = "你好";

        s = s.concat("世界");

        System.out.println(s);

    }

}

輸出結果為:

你好世界

7.toLowerCase()函數

java中toLowerCase()函數的作用是將字符串大寫字符轉換為小寫。

public class Test {

    public static void main(String args[]) {

        System.out.println(Character.toLowerCase('HELLO WORLD!'));

    }

}

輸出結果為:

hello world!

8.toUpperCase()函數

java中toUpperCase()函數的作用是將字符串小寫字符轉換為大寫。

public class Test {

    public static void main(String args[]) {

        System.out.println(Character.toUpperCase('hello world!'));

    }

}

輸出結果為:

HELLO WORLD!

9.length()函數

java中length()函數的作用是用于返回字符串的長度。

public static void main(String args[]) {

        String site = "hello world!";

        int len = site.length();

        System.out.println( len );    //返回12

   }

}


0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女