import java.util.*;
/**
*功能描述:判断一个字符串中是否只含有相同的子字符串(子串长度>=2)
输入:abab
返回:true
输入:abcd
返回:false
循环查找的办法:从首字符处截取长度为2的字符串,在以后的字符串里看,是否包含。然后往后移,接着截长度为2的字符串并判断。
接着是长度为3的字符串,长度为4的字符串。直到字符串的一半长度。对长度为1,2,3的字符串单独处理。
*/
public class CheckStr {
/**
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean result=false;
System.out.println("请输入一串字符:");
String str = in.nextLine();
in.close();
if(str.length()==1 || str.length()==2){
System.out.println("输入的字符串长度为"+str.length()+",不足以拆分,请输入3以上长度的字符串。");
}else{
result = checkString(str);
}
System.out.println("结果--" + result);
}
public static boolean checkString(String str) {
boolean result=false;
if(str.length()==3){
if((str.charAt(0)==str.charAt(1))&&(str.charAt(0)==str.charAt(2))){
result=true;
}
}else{
for(int i=2;i<=str.length()/2&&(result==false);i++){
for(int j=0;j<=str.length()-i;j++){
String strP=str.substring(j,j+i);
if(str.indexOf(strP,j+1)!=-1){
result=true;
break;
}
}
}
}
return result;
}
}
判断一个字符串中是否只含有相同的子字符串(子串长度>=2)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.