Java 在字符串中查找字符串
我试图在str2中找到str1的一部分(两个字符串)。目前,我有一个工作程序,但在我看来,它并不完全正常工作或使用正确的逻辑Java 在字符串中查找字符串,java,Java,我试图在str2中找到str1的一部分(两个字符串)。目前,我有一个工作程序,但在我看来,它并不完全正常工作或使用正确的逻辑 public static void main(String[] params) { String str1 = "blis"; String str2 = "grisdisbliszis"; //String str2 = "adasdsfgsdfdcx"; System.out.println(isCommonSubstring(str
public static void main(String[] params) {
String str1 = "blis";
String str2 = "grisdisbliszis";
//String str2 = "adasdsfgsdfdcx";
System.out.println(isCommonSubstring(str1, str2));
}
public static boolean isCommonSubstring(String str1, String str2) {
char char1, char2;
boolean match = true;
String check = "";
for(int i = 0; i < str1.length(); i++) {
char1 = str1.charAt(i);
for(int j = 0; j < str2.length(); j++) {
char2 = str2.charAt(j);
if(char1 == char2) {
check += char1;
System.out.println("Matched!: "+str1.charAt(i)+"[Char "+i+"] <==> " // DEBUG
+str2.charAt(j)+"[Char "+j+"]"); // DEBUG
break; // Break because we've found our first match and we need to check others
}
}
}
System.out.println(check+" || "+str1);
if(check.equals(str1)) match = true;
else match = false;
return match;
}
publicstaticvoidmain(字符串[]参数){
字符串str1=“blis”;
字符串str2=“grisbliszis”;
//字符串str2=“adasdsfgsdfdcx”;
System.out.println(isCommonSubstring(str1,str2));
}
公共静态布尔isCommonSubstring(字符串str1、字符串str2){
char char1,char2;
布尔匹配=真;
字符串检查=”;
对于(int i=0;i
这可能会起作用,但如果我把这两个字符串转过来,它就不再起作用了
如何改进查找匹配项的逻辑?我试图以某种方式使用substring(),但我不确定如何将其放入其中
我考虑的解决方案可能是使用if语句来比较长度,并使str2的长度更长,因此它总是更长,并且不会导致错误。但这是一个体面的解决方案吗
我只能使用长度、子字符串和字符。String.contains()
是您需要的
除非您有不使用内置功能的要求,否则不要重新发明轮子
以上教程链接中的示例:
String str1 = "tutorials point", str2 = "http://";
CharSequence cs1 = "int";
// string contains the specified sequence of char values
boolean retval = str1.contains(cs1);
System.out.println("Method returns : " + retval);
// string does not contain the specified sequence of char value
retval = str2.contains("_");
System.out.println("Methods returns: " + retval);
}
如果希望不区分大小写:
str1.toLowerCase.contains(str2.toLowerCase());
编辑
结果是OP不能使用
内置功能。将答案留在这里,以防其他人不想重新发明轮子:)这里的算法如下:
使用一个主循环,该循环将在找到单词时停止,或者当我们达到其长度减去第二个字符串的长度时停止。(例如:如果str1的长度为6,str2的长度为4,我们知道str2不是从索引3开始的)
使用第二个循环检查str2的索引是否适合str1。我们在内部一个接一个地检查每个索引。如果其中一个与主循环的循环索引不对应,我们将从内部循环中断,并将一个添加到主循环的索引中
如果内部循环成功完成,我们就知道找到了单词
这里有一个简短的解决方案,可以只使用charAt()
和lenght()
:
static boolean isCommonSubstring(字符串一、字符串二){
if(一.length()<二.length()){
字符串tmp=1;
一等于二;
2=tmp;
}
主要内容:
对于(int i=0;i
我只能使用长度、子字符串和字符
因为您只能使用这些方法。您可以这样做:
//Self-defined method to check whether sub exist in str
public static boolean contains(String str, String sub){
if(sub.length() > str.length())
return false;
for(int x=0; x<= str.length() - sub.length(); x++)
if(str.substring(x, sub.length()+x).equals(sub))
return true;
return false;
}
测试:
String a = "apple";
String b = "ple";
System.out.println(a + " contains " + b + ": " + contains(a, b));
apple contains ple: true
输出:
String a = "apple";
String b = "ple";
System.out.println(a + " contains " + b + ": " + contains(a, b));
apple contains ple: true
你不使用内置字符串函数或StringUtils之类的实用程序是有原因的吗?你应该清楚你是否不能使用现有的Java字符串API。这是家庭作业吗?忘了提到我不能使用长度、子字符串和字符以外的任何东西。对不起。你真的在试图找到一个公共子字符串吗?如果是的话,nameisSubstring
有误导性,因为它会让读者认为您的方法是检查str1是否是str2的子字符串。@Xylus不要放弃一个,您的代码很接近……只需检查注释i删除了我的upvote,似乎OP无法使用内置函数,因此您的解决方案(无论多么正确)不会有帮助。@Ceelos。是的,这是可以理解的。但是其他人投了反对票,只是想知道我是否犯了错误。如果我们不提供自己的代码,也许我们会帮助更多,但帮助自己弄清楚如何做……,我不会投反对票,因为你是对的,Petter。这就是我添加解释的原因。提供一些告诉xylus他需要做什么才能使他的代码正常工作,你就会得到我的支持票(特别是因为他不想被迫先通过最长的,然后通过最短的……因此,如果你颠倒一和二,你会得到与xylus相同的结果。如果String one=“blis”和String two=“grisbliszis”,你的metod会返回什么?查看问题下的注释以了解..只需反转字符串..不能让字符串包含更大的字符串…注意xylus不喜欢先传递最长的字符串,然后传递最短的字符串…这是他的问题…检查注释…@PetterFriberg如果是这样,他可以简单地交换参数“str”和“sub”。事实上,这根本不是问题。通过对我的代码进行一点即兴创作,str1可以比str2长,反之亦然。如果你阅读了关于这个问题的评论,你会发现问题出在哪里。。我会继续玩下去。。。