首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >php stristr函数在java中是否等价?

php stristr函数在java中是否等价?
EN

Stack Overflow用户
提问于 2016-08-23 18:01:24
回答 1查看 229关注 0票数 0

我正在尝试根据用户输入发送回一个列表作为响应。我使用的是Spring mvc,并且我有这个控制器方法来进行处理

代码语言:javascript
运行
复制
    @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;

}

用php编写的服务器代码可以很容易地写成

代码语言:javascript
运行
复制
 if (stristr($q, substr($name, 0, $len))) {
        if ($hint === "") {
            $hint = $name;
        } else {
            $hint .= ", $name";
        }

所以我想知道java中是否有与stristr函数等效的函数?

附注: UI部分如下

代码语言:javascript
运行
复制
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();
    }
}
代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-23 18:52:04

有一个strstr there的实现,将其修改为不区分大小写应该是可能的。

如果链接的页面被删除,下面是代码:

代码语言:javascript
运行
复制
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; 
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39098069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档