亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    java中判斷字符串中是否包含某個特定字符串的方法有哪些

    java中判斷字符串中是否包含某個特定字符串的方法有哪些

    判斷一個字符串是否包含某個子串的n種方法:

    1、startsWith()方法

    2、contains()方法

    3、indexOf方法

    startsWith()方法

    這個方法有兩個變體,用于檢測字符串是否以指定的前綴開始。

    此方法定義的語法如下:

    public boolean startsWith(String prefix, int toffset) or public boolean startsWith(String prefix)

    參數(shù)說明:

    prefix – 要匹配的前綴。

    toffset – 從哪里開始尋找字符串。

    返回值:

    true和false

    相關(guān)視頻教程推薦:java學習視頻

    示例:

    import java.io.*;   public class Test{    public static void main(String args[]){       String Str = new String("Welcome to Yiibai.com");         System.out.print("Return Value :" );       System.out.println(Str.startsWith("Welcome") );         System.out.print("Return Value :" );       System.out.println(Str.startsWith("Tutorials") );         System.out.print("Return Value :" );       System.out.println(Str.startsWith("Yiibai", 11) );    } }

    contains方法

    java.lang.String.contains()

    方法返回true,當且僅當此字符串包含指定的char值序列。

    返回值:

    true和false

    示例如下:

    public static void main(String[] args) {           String str = "abc";           boolean status = str.contains("a");           if(status){             System.out.println("包含");           }else{             System.out.println("不包含");         }       }

    indexOf方法

    java.lang.String.indexOf()

    用途是在一個字符串中尋找一個字的位置,同時也可以判斷一個字符串中是否包含某個字符。

    返回值:

    int

    示例如下:

    public static void main(String[] args) {     String str1 = "abcdefg";     int result1 = str1.indexOf("ab");     if(result1 != -1){         System.out.println("字符串str中包含子串“ab”"+result1);     }else{         System.out.println("字符串str中不包含子串“ab”"+result1);     } }

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號