Java 测试子字符串是否是字符串的一部分
我制作了一个程序来测试一个子字符串是否是另一个字符串的一部分。但它总是返回Java 测试子字符串是否是字符串的一部分,java,Java,我制作了一个程序来测试一个子字符串是否是另一个字符串的一部分。但它总是返回false public class SubString{ public static void main(String... args){ String findFrom = "mouse"; String toFind = "mouse and cat"; boolean flag = false; int toFindLenght = toFi
false
public class SubString{
public static void main(String... args){
String findFrom = "mouse";
String toFind = "mouse and cat";
boolean flag = false;
int toFindLenght = toFind.length();
int findFromLength = findFrom.length();
for (int x = 0; x < toFindLenght; x++) {
char toFindIntermediate = toFind.charAt(x);
for (int y = 0; y < findFromLength; y++) {
char toFindFromIntermediate = findFrom.charAt(y);
int counter = x;
if (toFindIntermediate == toFindFromIntermediate) {
toFindFromIntermediate=toFind.charAt(counter);
counter++;
flag=true;
} else {
flag=false;
}
}
}
System.out.println("IS the substring a part of the string :"+flag);
}
}
公共类子字符串{
公共静态void main(字符串…参数){
字符串findFrom=“鼠标”;
字符串toFind=“鼠标和猫”;
布尔标志=假;
int-tofindlinght=toFind.length();
int findFromLength=findFrom.length();
对于(int x=0;x
这是因为您没有跳出循环,因此即使在一个循环中将条件设置为true
,在另一个循环中将其重置为false
我在这里发布了相同的代码,但有一些小改动
here what i have done is i just change the y counter of inner for loop to start from counter and added a break after flag turs true.
对不起,我没有检查阴性病例。我在这里发布编辑过的代码
public class SubString {
public static void main(String... args) {
String findFrom = "mouse";
String toFind = "mouse and cat";
boolean flag = false;
int toFindLenght = toFind.length();
int findFromLength = findFrom.length();
int counter = 0;
for (int x = 0; x < toFindLenght; x++) {
char toFindIntermediate = toFind.charAt(x);
for (int y = counter; y < findFromLength; y++) {
char toFindFromIntermediate = findFrom.charAt(y);
if(toFindIntermediate == ' '){
break;
}
else if (toFindIntermediate == toFindFromIntermediate) {
toFindFromIntermediate = toFind.charAt(counter);
counter++;
flag = true;
break;
} else if (0 != counter) {
flag = false;
counter = 0;
break;
}
}
}
System.out.println("IS the substring a part of the string :" + flag);
}
公共类子字符串{
公共静态void main(字符串…参数){
字符串findFrom=“鼠标”;
字符串toFind=“鼠标和猫”;
布尔标志=假;
int-tofindlinght=toFind.length();
int findFromLength=findFrom.length();
int计数器=0;
对于(int x=0;x
}我不喜欢使用中断循环,我想这是我最初学习的方式,除非有什么原因,我不能因为额外的一次布尔测试而损失性能,否则我会避免它。以下是注释了更改的代码:
public class SubString {
public static void main(String... args) {
String findFrom = "mouse";
String toFind = "mouse and cat";
boolean flag = false;
int toFindLength = toFind.length();
int findFromLength = findFrom.length(); // fixed spelling of length
for (int x = 0; x <= toFindLength - findFromLength && !flag; x++) {
// Changed the test in the for loop above so you won't get
// index-out-of-bounds exceptions in the next loop.
// char toFindIntermediate = toFind.charAt(x); // Moving this lower
flag = true; // this lets you avoid break
for (int y = 0, counter = x; y < findFromLength && flag; y++) {
char toFindIntermediate = toFind.charAt(counter);
char findFromIntermediate = findFrom.charAt(y);
// took "to" off above variable name for consistancy
// int counter = x;
if (toFindIntermediate == findFromIntermediate) {
// When you change the name of the variable you
// notice pretty quickly the assignment below is wrong.
// Also, you need to increment counter before you store
// the next char
// findFromIntermediate=toFind.charAt(counter);
// counter++;
// flag=true;
counter++;
} else {
flag=false;
}
}
}
System.out.println("IS the substring a part of the string :"+flag);
}
}
公共类子字符串{
公共静态void main(字符串…参数){
字符串findFrom=“鼠标”;
字符串toFind=“鼠标和猫”;
布尔标志=假;
int-toFindLength=toFind.length();
int findFromLength=findFrom.length();//修复了长度的拼写
对于(int x=0;x是否有理由不简单地使用String.contains()
或String.indexOf()
?是的。我希望它能以最艰难的方式完成:)我只是想知道我的逻辑出了什么问题:/n不是挑你的名字,而是考虑到问题的写作和你的代码格式,它非常适合--家庭作业?是吗?请这样标记。这就是为什么我拒绝忽略明显的字符串的潜在候选人。contains()
method。它让同行评审代码变得很痛苦。打败我:)是的,就像Chin在'flag=true;'add'break;'之后说的那样,但是如果我中断了..我将如何测试整个子字符串…索引不会重置为0吗?:/你的计数器做什么?:)好吧,它不工作..它只测试一个单词..而不是整个字符串:/copying,pasting,running你的code给出了这个--4个错误:(它们只是语法/拼写错误吗?我不在这里的开发机器上,所以没有IDE来捕捉简单的东西,但是如果比这更糟糕,逻辑也不好,那是什么,我会编辑我的答案。有机会实际测试一下。我唯一的错误是需要删除“int”从第二个for循环中的counter开始。我还把整个东西放在一个循环中,这样我可以反复运行它,改变toFind和findFrom字符串,它就像一个符咒。如果您仍然有问题,请告诉我。
public class SubString {
public static void main(String... args) {
String findFrom = "mouse";
String toFind = "mouse and cat";
boolean flag = false;
int toFindLength = toFind.length();
int findFromLength = findFrom.length();
for (int x = 0; x <= toFindLength - findFromLength && !flag; x++) {
flag = true;
for (int y = 0, int counter = x; y < findFromLength && flag; y++) {
char toFindIntermediate = toFind.charAt(counter);
char findFromIntermediate = findFrom.charAt(y);
if (toFindIntermediate == findFromIntermediate) {
counter++;
} else {
flag=false;
}
}
}
System.out.println("IS the substring a part of the string :"+flag);
}
}