Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在字符串所在的另一个字符串中查找该字符串的出现处';它没有嵌入到另一个词中?_Java - Fatal编程技术网

Java 如何在字符串所在的另一个字符串中查找该字符串的出现处';它没有嵌入到另一个词中?

Java 如何在字符串所在的另一个字符串中查找该字符串的出现处';它没有嵌入到另一个词中?,java,Java,我试图创建一个静态方法“indexOfKeyword”,并返回一个字符串的indexOf,该字符串没有嵌入到另一个单词中。如果没有这种情况,它将返回-1 比如说, String s = "In Florida, snowshoes generate no interest."; String keyword = "no"; 这将返回31 我认为唯一的问题不是找到字符串关键字的下一个匹配项。到目前为止,我得到的是: public static int indexOfKeyword( String

我试图创建一个静态方法“indexOfKeyword”,并返回一个字符串的indexOf,该字符串没有嵌入到另一个单词中。如果没有这种情况,它将返回-1

比如说,

String s = "In Florida, snowshoes generate no interest.";
String keyword = "no";
这将返回31

我认为唯一的问题不是找到字符串关键字的下一个匹配项。到目前为止,我得到的是:

public static int indexOfKeyword( String s, String keyword )
{

s = s.toLowerCase();
keyword = keyword.toLowerCase();


int startIdx = s.indexOf( keyword );



while ( startIdx >= 0 )
{

String before = " ";
String after = " ";

if ( startIdx > 0 ){

     before = s.substring( startIdx - 1 , startIdx);
 }


int endIdx = startIdx;


 if ( endIdx < s.length() ){

     after = s.substring( startIdx + keyword.length() , startIdx + keyword.length() + 1);
 }


if ( !(before.compareTo("a") >= 0 && before.compareTo("z") <= 0 && after.compareTo("a") >= 0 &&     after.compareTo("z") <= 0)){
  return startIdx;
}




startIdx = 
   /* expression using 2-input indexOf for the start of the next occurrence */

}


  return -1;
}


 public static void main( String[] args )  
 { 
 // ... and test it here 
String s = ""; 
String keyword = ""; 

System.out.println( indexOfKeyword( s, keyword ) ); 
} 
public static int indexOfKeyword(字符串s,字符串关键字)
{
s=s.toLowerCase();
关键字=关键字.toLowerCase();
int startIdx=s.indexOf(关键字);
而(startIdx>=0)
{
字符串在“”之前;
在“”后面加上字符串;
如果(startIdx>0){
before=s.substring(startIdx-1,startIdx);
}
int endIdx=startIdx;
如果(endIdx=0&&before.compareTo(“z”)=0&&before.compareTo(“z”)类似以下内容:

String input = "In Florida, snowshoes generate no interest.";
String pattern = "\\bno\\b";
Matcher matcher = Pattern.compile(pattern).matcher(input);

return matcher.find() ? matcher.start() : -1;
public static int indexOfKeyword(String s, String keyword) {
    String pattern = "\\b" + Pattern.quote(keyword) + "\\b";
    Matcher matcher = Pattern.compile(pattern).matcher(s);

    return matcher.find() ? matcher.start() : -1;
}
未嵌入另一个单词的字符串不一定用空格分隔。它可以是逗号、句点、字符串的开头等

上述解决方案使用正则表达式单词边界(
\b
)给出正确的解决方案


如果在正则表达式中使用关键字时存在包含具有特殊含义的字符的风险,则可能需要首先对其进行转义:

String pattern = "\\b" + Pattern.quote(keyword) + "\\b";
因此,完整的方法实现可能如下所示:

String input = "In Florida, snowshoes generate no interest.";
String pattern = "\\bno\\b";
Matcher matcher = Pattern.compile(pattern).matcher(input);

return matcher.find() ? matcher.start() : -1;
public static int indexOfKeyword(String s, String keyword) {
    String pattern = "\\b" + Pattern.quote(keyword) + "\\b";
    Matcher matcher = Pattern.compile(pattern).matcher(s);

    return matcher.find() ? matcher.start() : -1;
}

感谢您回答大家的问题,但我忘了提到,我正在尝试使用Florida示例创建一个静态方法“indexOfKeyword”,向他展示如何将关键字转义到正则表达式中是不值得的pattern@KeithNicholas感谢您的帮助!+1我必须说我特别喜欢return语句,它巧妙地调用了
find()
,这也会变异匹配器,允许立即在线调用
start()