简体   繁体   English

php stristr函数是否等效于Java?

[英]php stristr function Equivalent in java?

I am trying to send back a list as a response based on the user input. 我试图根据用户输入将列表作为响应发送回去。 I am using Spring mvc and i have this controller method that does the processing 我正在使用Spring mvc,并且我有执行处理的控制器方法

    @RequestMapping(value = { "/hint" }, method = RequestMethod.GET,params={"word"})
public @ResponseBody String hint(ModelMap model,@RequestParam String word) {
    System.out.println("Inside Hint");
    String[] hints = { "Ram", "Ra","R","Shyam" };

    String returnedhints="";
    // lookup all hints from array if $q is different from ""
    if (word != null) {
        System.out.println("i am here");
        //word = word.toLowerCase();
        int length = hints.length;
        for (int j = 0; j < length; j++) {
            System.out.println(word+"contains"+hints[j]+"="+word.contains(hints[j]));
            if ( hints[j].regionMatches(0,word, 0, hints[j].length())) {
                returnedhints= returnedhints+","+hints[j];
            }
        }
    }

return returnedhints;

}

The server code in php can be easily written as php中的服务器代码可以很容易地编写为

 if (stristr($q, substr($name, 0, $len))) {
        if ($hint === "") {
            $hint = $name;
        } else {
            $hint .= ", $name";
        }

So i wanted to know if there is any equivalent to stristr function in java? 所以我想知道在Java中是否有任何等效于stristr函数的函数?

PS the UI part is as follows PS的UI部分如下

function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        console.log(str);

        var xmlhttp = new XMLHttpRequest();

        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var list=xmlhttp.responseText.split(',');
                console.log(list);
                document.getElementById("txtHint").innerHTML = list;
            }
        };
        xmlhttp.open("GET", "/hint?word=" + str, true);
        xmlhttp.send();
    }
}
<label>Name <input id="peak" type="text" name="peak_name" onkeyup="showHint(this.value)">
                            <p id="peak"></p>
                        <p>Suggestions: <span id="txtHint"></span></p>

There is an implementation of strstr there , adapting it to be case insensitive should be possible. 还有的的strstr实现还有 ,适应它是不区分大小写应该是可能的。

In case the linked page is removed, here is the code : 如果链接页面被删除,则代码如下:

public class Solution {
    public String strStr(String haystack, String needle) {
      if(haystack==null || needle==null) return null; 
      int hLength=haystack.length(); 
      int nLength=needle.length(); 
      if(hLength<nLength) return null; 
      if(nLength==0) return haystack;
      for(int i=0; i<=hLength-nLength; i++)
      {
        if(haystack.charAt(i)==needle.charAt(0))
        {
          int j=0; 
          for(; j<nLength; j++)
          {
            if(haystack.charAt(i+j)!=needle.charAt(j))
            {
              break; 
            }
          }
          if(j==nLength) return haystack.substring(i) ; 
        }  
      }
      return null; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM