当前位置:编程学习 > JAVA >>

java 求子字符串问题

如题: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,所以无法实现,那么我的代码该如何修改?
java string --------------------编程问答-------------------- 我写了一个,你可以参考一下

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() --------------------编程问答-------------------- 问题已解决,只是使用了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;
}
}
--------------------编程问答-------------------- 不能用string.contains(substring)? --------------------编程问答-------------------- 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;
}
}
}
} --------------------编程问答--------------------

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;
}
}

--------------------编程问答-------------------- 话说 这是作业吗?要求不使用jdk本身方法?
如果不是的话...
public boolean isSubString(String str,String src) {
  return src != null && src.indexOf(str) != -1;
}
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,