为什么我的函数在搜索字符串以查找某些子字符串时只有 return 0?
Why does my function only return 0 when searching through strings to find certain sub strings?
此函数的要点是搜索字符串以查找以 "foo" 开头并以 "bar".
结尾的子字符串
例如foobar 应该 return 1. foobarafoobbbarabar 应该 return 5 因为第一个 foo 和它后面的 bar 算作 1,第一个 foo 和最后一个 bar 算作另一个 1,第二个 foo 和它后面的 bar算作另一个 1 后开始 3 个字符,最后第二个 foo 和最后一个 bar 也算作 1,总共 5.
目前,我已将我的函数设置为如下工作:
public static int foobarCounter(String s)
{
int count = 0;
while(s.length() >0)
{
int startCharacter = 4; //Start character to check for bar start right after the last character of foo
while(startCharacter + 2 < s.length()) //Prevent a string out of bounds exception
{
if(s.startsWith("foo"))
{
if(s.substring(startCharacter, startCharacter + 2) == "bar")
{
++count; //add one to the count of foobars
}
else
{
++startCharacter; //else check from the start one more character along
}
}
else
{
s.replace(s.substring(0,1), ""); //Doesn't start with foo, remove the first character and try again
}
} //End of inner while loop
} //End of while loop
return count;
} //End of method
我希望这是有道理的。谢谢
因为这个:
s.substring(startCharacter, startCharacter + 2)
只有 returns 2 个字符,而不是 3 个。所以你的 if-statement
永远不会是真的。让它+3
if (s.substring(startCharacter, startCharacter + 3).equals("bar"))
您应该使用 equals
(或 equalsIgnoreCase
,如果值有可能有大写)而不是 ==
有了这个,您还应该更改 while-loop
的条件
如果您可以选择重写函数,我会使用类似下面的方法。这不是一个完美的解决方案,但如果数据不是太大,它是合适的。
public static int foobarCounter(String s) {
int count = 0;
String p1 = "foo";
String p2 = "bar";
int i = 0;
int j = 0;
while ((j = i = s.indexOf(p1, i)) != -1) {
while ((j = s.indexOf(p2, j + p1.length())) != -1) {
//the extended stepwidth of the previous line depends on p1, p2 and is not always possible like this
++count;
}
i = i + 1;
}
return count;
}
此函数的要点是搜索字符串以查找以 "foo" 开头并以 "bar".
结尾的子字符串例如foobar 应该 return 1. foobarafoobbbarabar 应该 return 5 因为第一个 foo 和它后面的 bar 算作 1,第一个 foo 和最后一个 bar 算作另一个 1,第二个 foo 和它后面的 bar算作另一个 1 后开始 3 个字符,最后第二个 foo 和最后一个 bar 也算作 1,总共 5.
目前,我已将我的函数设置为如下工作:
public static int foobarCounter(String s)
{
int count = 0;
while(s.length() >0)
{
int startCharacter = 4; //Start character to check for bar start right after the last character of foo
while(startCharacter + 2 < s.length()) //Prevent a string out of bounds exception
{
if(s.startsWith("foo"))
{
if(s.substring(startCharacter, startCharacter + 2) == "bar")
{
++count; //add one to the count of foobars
}
else
{
++startCharacter; //else check from the start one more character along
}
}
else
{
s.replace(s.substring(0,1), ""); //Doesn't start with foo, remove the first character and try again
}
} //End of inner while loop
} //End of while loop
return count;
} //End of method
我希望这是有道理的。谢谢
因为这个:
s.substring(startCharacter, startCharacter + 2)
只有 returns 2 个字符,而不是 3 个。所以你的 if-statement
永远不会是真的。让它+3
if (s.substring(startCharacter, startCharacter + 3).equals("bar"))
您应该使用 equals
(或 equalsIgnoreCase
,如果值有可能有大写)而不是 ==
有了这个,您还应该更改 while-loop
如果您可以选择重写函数,我会使用类似下面的方法。这不是一个完美的解决方案,但如果数据不是太大,它是合适的。
public static int foobarCounter(String s) {
int count = 0;
String p1 = "foo";
String p2 = "bar";
int i = 0;
int j = 0;
while ((j = i = s.indexOf(p1, i)) != -1) {
while ((j = s.indexOf(p2, j + p1.length())) != -1) {
//the extended stepwidth of the previous line depends on p1, p2 and is not always possible like this
++count;
}
i = i + 1;
}
return count;
}