獲取一個字元串在另一個字元串中出現的次數
1 package com.yhqtv.java; 2 3 /* 4 *獲取一個字元串在另一個字元串中出現的次數 5 * 比如:獲取「ab」在「abskfjdlabaljlkjbalkjlk」中出現的次數 6 * @author XMKJ yhqtv.com Email:[email protected] 7 * @create 2020-04-30-17:25 8 * 9 */ 10 public class Test { 11 public static void main(String[] args) { 12 String s = "abskfjdlabaljlkjbablkjlk"; 13 String s1="ab"; 14 Test t = new Test(); 15 t.cha(s, s1); 16 } 17 18 private void cha(String s, String a) { 19 20 int count=0; 21 int index=s.indexOf(a); 22 23 while (index!=-1){ 24 count++; 25 index=index+a.length(); 26 index=s.indexOf(a,index); 27 28 } 29 System.out.println(count); 30 31 } 32 33 }