【问题标题】:Substring search in JavaJava中的子字符串搜索
【发布时间】:2010-01-01 01:35:26
【问题描述】:

字符串比较有问题。比如有这个字符串:

"hello world i am from heaven"

我想搜索这个字符串是否包含“world”。我使用了以下功能,但它们有一些问题。我使用了String.indexof(),但如果我尝试搜索“w”,它会说它存在。

简而言之,我想我正在寻找精确的比较。 Java有什么好的功能吗?

还有Java中可以计算log base 2的函数吗?

【问题讨论】:

  • @agazerboy - 不要将两个完全不相关的问题放在一个问题中。
  • 所以在 "w" 的情况下,您期望返回 false 的内容,因为它不是一个完整的单词?

标签: java string


【解决方案1】:

我假设您在使用角色版本时遇到的indexOf() 问题与您使用角色版本有关(否则为什么您在寻找世界时要搜索 w?)。如果是这样,indexOf() 可以带一个字符串参数来搜索:

String s = "hello world i am from heaven";
if (s.indexOf("world") != -1) {
  // it contains world
}

至于对数基数 2,这很简单:

public static double log2(double d) {
  return Math.log(d) / Math.log(2.0d);
}

【讨论】:

  • 我分解每个字符串以对我的应用程序进行某种比较。有时它也会获得单个 CHAR,因此会产生这些问题。如果我想自己传递参数,那么我知道我可以传递一个字符串。但那是在运行时。你能请其他解决方案吗?
  • 或者你也可以说我正在寻找字符串中的组织,并且在运行时它会得到参数“或”再次它会说是存在
  • 抱歉,我不太明白您要做什么。你能解释一下吗?
【解决方案2】:

对于精确的字符串比较,您可以这样做:

boolean match = stringA.equals(stringB);

如果要检查字符串是否包含子字符串,可以这样做:

boolean contains = string.contains(substring);

更多String方法,见javadocs

【讨论】:

    【解决方案3】:

    你可以使用

    String s = "hello world i am from heaven";
    if (s.contains("world")) {
    // This string contains "world"
    }
    

    这是一个简单易用的功能,适用于单线:

    String s = "hello world i am from heaven";
    if (s.contains("world")) System.out.prinln("It exists!!!!!!!");
    

    【讨论】:

      【解决方案4】:

      嗨,我的版本如下:

      package com.example.basic;
      
      public class FindSubString {
      
      
          public String findTheSubString(String searchString, String inputString){
      
      
              StringBuilder builder = new StringBuilder(inputString);
      
              System.out.println("The capacity of the String " + builder.capacity());
      
              System.out.println("pos of" + builder.indexOf(searchString));
      
              return builder.substring(builder.indexOf(searchString),builder.indexOf(searchString)+searchString.length());
          }
      
          public static void main(String[] args) {
      
              String myString = "Please find if I am in this String and will I be found";
              String searchString = "I am in this String";
      
              FindSubString subString = new FindSubString();
      
              boolean isPresent = myString.contains(searchString);
      
              if(isPresent){
      
              System.out.println("The substring is present " + isPresent + myString.length());
      
              System.out.println(subString.findTheSubString(searchString,myString));
              }
              else 
              {
                  System.out.println("The substring is ! present " + isPresent);
      
              }
          }
      }
      

      如果有用请告诉我。

      【讨论】:

        【解决方案5】:

        我终于找到了解决办法。

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        
                                ArrayList<HashMap<String, String>> arrayTemplist = new ArrayList<HashMap<String, String>>();
                                String searchString = mEditText.getText().toString();
                                if(searchString.equals("")){new DownloadJSON().execute();}
        
                            else{
        
        
                            for (int i = 0; i < arraylist.size(); i++) {
                                String currentString = arraylist.get(i).get(MainActivity.COUNTRY);
                                if (searchString.toLowerCase().contains(currentString.toLowerCase())) {
            //pass the character-sequence instead of currentstring
        
                                    arrayTemplist.add(arraylist.get(i));
                                }
                            }
                                }
                                adapter = new ListViewAdapter(MainActivity.this, arrayTemplist);
                                listview.setAdapter(adapter);
        
                        }
        
        
                    });
            }
        

        把上面的代码替换成这个:

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        
                                ArrayList<HashMap<String, String>> arrayTemplist = new ArrayList<HashMap<String, String>>();
                                String searchString = mEditText.getText().toString();
                                if(searchString.equals("")){new DownloadJSON().execute();}
        
                            else{
        
        
                            for (int i = 0; i < arraylist.size(); i++) {
                                String currentString = arraylist.get(i).get(MainActivity.COUNTRY);
                                if (currentString.toLowerCase().contains(searchString.toLowerCase())) {
            //pass the character-sequence instead of currentstring
        
                                    arrayTemplist.add(arraylist.get(i));
                                }
                            }
                                }
                                adapter = new ListViewAdapter(MainActivity.this, arrayTemplist);
                                listview.setAdapter(adapter);
        
                        }
        
        
                    });
        

        【讨论】:

        • 最好同时说明您在新代码中所做的更改以及为什么会有所帮助。
        【解决方案6】:

        假设您有以下字符串:

        String sampleS = "hello world i am from heaven";
        

        使用下面的代码进行字符串到字符串的比较:

        boolean is_Equal = sampleS.equals("<any string you want to compare>");
        

        使用以下任一代码检查您的字符串是否包含子字符串:

        boolean is_Contains = sampleS.contains("world");
        // OR
        boolean is_Contains = (sampleS.indexOf("world") != -1);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-10
          • 2013-05-25
          • 2021-03-22
          • 1970-01-01
          • 2018-12-01
          • 2019-10-06
          • 1970-01-01
          相关资源
          最近更新 更多