反转部分字符串
package JavaExercise.String;
public class StringReverse {
public static void main(String[] args) {
String str01="123asd456wqe";
String reverseResult = reverse1(str01, 2, 6);
System.out.println(reverseResult);
}
public static String reverse(String str,int stratindex,int endindex){
if(str!=null) {
char[] arr1 = str.toCharArray();
for (int x = stratindex, y = endindex; x < y; x++, y--) {
char demo = arr1[x];
arr1[x] = arr1[y];
arr1[y] = demo;
}
return new String(arr1);
}else return null;
}
public static String reverse1(String str,int stratindex,int endindex){
String str02=str.substring(0,stratindex);
for(int x=endindex;x>=stratindex;x--){
str02+=str.charAt(x);
}
str02+=str.substring(endindex+1);
return str02;
}
}
获取一个字符串出现的次数
package JavaExercise.String;
public class GetStringCount {
public static int getcount(String mainstr,String seekstr){
int mainstrlength=mainstr.length();
int seekstrlength=seekstr.length();
int account=0;
int index;
if(seekstrlength<=mainstrlength){
wwhile ((index=mainstr.indexOf(seekstr,index))!=-1){
account++;
index+=seekstrlength;
}
return account;
}
else return 0;
}
public static void main(String[] args) {
String str01="123asd456wqe12341234";
int acc=getcount(str01,"1234");
System.out.println(acc);
}
}