有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的字符串子字符串搜索

我对字符串比较有问题。例如,有以下字符串:

"hello world i am from heaven"

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

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

Java中是否有任何函数可以计算log base 2


共 (6) 个答案

  1. # 1 楼答案

    我终于找到了解决办法

    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);
    
                    }
    
    
                });
    
  2. # 2 楼答案

    要进行精确的字符串比较,只需执行以下操作:

    boolean match = stringA.equals(stringB);
    

    如果要检查字符串是否包含子字符串,可以执行以下操作:

    boolean contains = string.contains(substring);
    

    有关更多字符串方法,请参见javadocs

  3. # 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. # 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. # 5 楼答案

    假设您有以下字符串:

    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);
    
  6. # 6 楼答案

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

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

    至于log base 2,这很简单:

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