• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    怎么判斷一個字符串是否包含另一個字符串

          String類是final類,也即意味著String類不克不及被擔當,而且它的當作員方式都默認為final方式。在Java中,被final潤色的類是不許可被擔當的,而且該類中的當作員方式都默認為final方式。String類其實是經由過程char數組來保留字符串的。

    東西/原料

    • 電腦
    • intellij IDEA或者eclipse

    方式/步調

    1. 1

      第一步調:判定一個字符串是否包含另一個字符串。

      1、思緒清算

      起首我們可以借助于java中String類供給的indexOf方式

      2、代碼如下

      public class TestString {

      public static void main(String[] args) {

      String str = "aaaaaabbbbbbbccccccccc";

      String st1 = "ab";

      // 若是存在則返回字符串st1呈現在str的位置

      System.out.println(str.indexOf(st1));

      String st2 = "abcd";

      // st2不存在str中返回-1

      System.out.println(str.indexOf(st2));

      }

      }

      3、利用判定前提暗示代碼如下

      public class TestString {

      public static void main(String[] args) {

      String str = "aaaaaabbbbbbbccccccccc";

      String st1 = "ab";

      String st2 = "abcd";

      // 若是存在則返回字符串st1呈現在str的位置

      if (str.indexOf(st1) >= 0) {

      System.out.println("字符串str包含st1!");

      }

      // st2不存在str中返回-1

      if (str.indexOf(st2) == -1) {

      System.out.println("字符串str不包含st2!");

      }

      }

      }

    2. 2

      第二步調:java字符竄String常用的查詢方式。

      本家兒要利用的有

      1、str.indexOf (str代表一個String的實現對象)

      str.indexOf(st1)是從開首標的目的后面檢索st1在str中的位置下標從0起頭,若是找不到返回-1

      str.indexOf(st1,3) 從str的下標3起頭查找st1,查詢不到返回-1

      2、str.lastIndexOf(str代表一個String的實現對象)

      str.lastIndexOf(st1)是從后標的目的后前檢索st1在str中的位置下標從str.length()-1起頭,若是找不到返回-1

      str.lastIndexOf(st1,3) 從str的下標3標的目的前起頭查找st1,查詢不到返回-1

    3. 3

      第三步調:String字符串常用的方式。

      1、替代方式:replace

      2、拆分當作數據的方式:split

      3、是否以固心猿意馬的內容開首:startsWith

      4、截取字符串:substring

      5、轉為字符數組:toCharArray

      6、巨細寫轉換:toLowerCase,toUpperCase

      7、比力字符串:equals,equalsIgnoreCase

      8、去除空格:trim

    注重事項

    • jdk 1.8
    • 發表于 2019-05-05 22:14
    • 閱讀 ( 711 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    admin
    admin

    0 篇文章

    作家榜 ?

    1. xiaonan123 189 文章
    2. 湯依妹兒 97 文章
    3. luogf229 46 文章
    4. jy02406749 45 文章
    5. 小凡 34 文章
    6. Daisy萌 32 文章
    7. 我的QQ3117863681 24 文章
    8. 華志健 23 文章

    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆