java 求子字符串问题

R-I-P 2013-03-09 10:51:54
如题:cd 是abcd 的子字符串;ac不是abcd的子字符串;
又如
isSubString("The", "The cat in the hat.") is true
isSubString("hat.", "The cat in the hat.") is true

现在我写了一个Search类

public class Search {
//定义计数器
private int i = 0 ;
private int j = 0 ;
//第一个字母
private boolean first ;

public boolean searchChar(String target,String source){
for(;j<source.length() - target.length() + 1;j++){
if(target.charAt(0) == source.charAt(j) ){
first = true;
break;
}
}

if( first == true ){
if(target.length() == 1){
return true;
}
else{
for(i=1,j++;i<target.length();i++,j++){
if(target.charAt(i) != source.charAt(j)){
return false;
}
}
return true;
}
}
else{
return false;
}
}
}


但是对于isSubString("hat.", "The cat in the hat.") is true
hat 对于后者 因为有两个h,所以无法实现,那么我的代码该如何修改?
...全文
288 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
beiouwolf 2013-03-12
  • 打赏
  • 举报
回复
话说 这是作业吗?要求不使用jdk本身方法? 如果不是的话... public boolean isSubString(String str,String src) { return src != null && src.indexOf(str) != -1; }
Mourinho 2013-03-11
  • 打赏
  • 举报
回复

public class Test {
	public static void main(String[] args) {
		Search search = new Search();
		System.out.println(search.isSubString("The", "The cat in the hat."));
		System.out.println(search.isSubString("hat.", "The cat in the hat."));
		System.out.println(search.isSubString("cat in th", "The cat in the hat."));
	}
}

class Search {
	public boolean isSubString(String str, String src) {
		int index = 0;
		while (index < src.length()) {
			int lastIndex = index;
			for (int i = 0; i < str.length();++i) {
				if (str.charAt(i) != src.charAt(index + i)) {
					index += (i + 1);
					break;
				}
			}
			if (lastIndex == index) {
				return true;
			}
		}
		return false;
	}
}

R-I-P 2013-03-10
  • 打赏
  • 举报
回复
问题已解决,只是使用了length()和charAt(),下面是代码,欢迎测试

public class Search {
	//定义计数器
	private int i = 0 ;
	private int j = 0 ;
	//临时存储
	private int a = 0 ;
	private int b = 0 ;
	//循环计数器
	private int count = 0 ;
	
	public boolean searchChar(String target,String source){
			for(;j<source.length() - target.length() + 1;j++){
				if( target.charAt(0) == source.charAt(j)){
					a = i+1;
					b = j+1;
					
					
					while ( a < target.length()   &&   target.charAt(a) == source.charAt(b)){
						a++;
						b++;
						count++;
					}
					
					if(count == target.length()-1 ){
						return true;
					}
					else{
						continue;
					}
				}
			}
		return false;	
	}
}
z2010490051 2013-03-10
  • 打赏
  • 举报
回复
public class Test { /** * @param args */ public static void main(String[] args) { new Test().match("hat", "the cat in the hat"); } public void match(String reg, String strs) { String[] str = strs.split(" "); for (String s : str) { if (s.matches(reg)) { System.out.println("true"); break; } } } }
ZZZ5512536 2013-03-10
  • 打赏
  • 举报
回复
不能用string.contains(substring)?
bluemoby 2013-03-09
  • 打赏
  • 举报
回复
我写了一个,你可以参考一下

public class Test {

	public static boolean isSubString(String target,String source){
		//每次前进一个字符判断
		for(int i=0,j=source.length(),k=target.length();i+k<=j;i++){
			//取出一个子串
			String current=source.substring(i, i+k);
			//如果子串等于target则证明是子字符串
			if(current.equals(target)){
				return true;
			}
		}
		return false;
	}
	public static void main(String[] args) {
		System.out.println(isSubString("The", "The cat in the hat."));
		System.out.println(isSubString("hat.", "The cat in the hat."));
	}

}
而且String类中本身有子字符串的判断方法,contains()

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧