public class StringUtil { static int varlen1; static int varlen2; static String varstr1; static String varstr2; static String varstr3; public static String indexOf(String sourceStr,String indexStr,int indexNum){ /** * 原始字符串:sourceStr * 索引字符串:indexStr * 固定字符串截取後索引次數的indexNum * * indexOf("ws++rq++sl++dl","++",4) * 結果:dl */ varlen1 = sourceStr.length(); varlen2 = indexStr.length(); int i=0;//字符串下標 int n=0;//某字符串在指定字符串中的個數 int m=0; int x=0; while (i< varlen1){ varstr1 = sourceStr.substring(i, i);//substr(sourceStr,i,1); varstr2 = indexStr.substring(1,1);//獲取指定用於截串的字符串的第一個字符 if(varstr1.equals(varstr2)){ if(i+varlen2<=varlen1){//判斷字符下標 if(indexStr.equals(sourceStr.substring(i, i+varlen2))){ n++; i=i+varlen2; } } } i++; } x=n; if(indexNum <= 0){ return ""; } if(n==0){ return sourceStr; } if(indexNum==1){ i=0; n=0; while(i<varlen1){ varstr1 = sourceStr.substring(i, i); varstr2 = indexStr.substring(1, 1); if(null==varstr3){ varstr3=sourceStr.substring(i, i); }else varstr3.concat(sourceStr.substring(i, i)); if(varstr1.equals(varstr2)){ if(indexStr.equals(sourceStr.substring(i, i+varlen2))){ n++; if(i==0&&n==1){ m=varlen2; }else{ return sourceStr.substring(m, i); } } } i++; } }./*歡迎加入java交流Q君樣:909038429一塊兒吹水聊天 if(indexNum<=n+1){ i=0; n=0; int c=0; while(i<varlen1){ varstr1 = sourceStr.substring(i, i); varstr2 = indexStr.substring(1, 1); if(varstr1.equals(varstr2)){ if(indexStr.equals(sourceStr.substring(i, i+varlen2))){ n++; if(n+1==indexNum){ m=i+varlen2; c=n;//存儲指字符串出現的次數 } if(n-1==c&&c!=0){ //當上一個條件知足時,下次出現時返回指定索引的字符串 return sourceStr.substring(m, i); } i=i+varlen2; }else i++; }else i++; } } //獲取截取後的最後一個字符串 return sourceStr.substring(sourceStr.lastIndexOf(indexStr)+indexStr.length(),sourceStr.length()); } public static void main(String[] args) { System.out.println(indexOf("ws++rq++sl++dl","++",3)); } }
最新2020整理收集的一些高頻面試題(都整理成文檔),有不少乾貨,包含mysql,netty,spring,線程,spring cloud、jvm、源碼、算法等詳細講解,也有詳細的學習規劃圖,面試題整理等,須要獲取這些內容的朋友請加Q君樣:909038429
/./*歡迎加入java交流Q君樣:909038429一塊兒吹水聊天java