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一起吹水聊天
来源:oschina
链接:https://my.oschina.net/u/4303372/blog/4882477