简体   繁体   English

Java中的子字符串搜索

[英]Substring search in Java

I have a problem with string comparison.我有字符串比较问题。 For example, there is this string:例如,有这个字符串:

"hello world i am from heaven"

I want to search if this string contains "world".我想搜索这个字符串是否包含“world”。 I used following functions but they have some problems.我使用了以下功能,但它们有一些问题。 I used String.indexof() but if I will try to search for "w" it will say it exists.我使用了String.indexof()但如果我尝试搜索“w”,它会说它存在。

In short I think I am looking for exact comparison.简而言之,我想我正在寻找精确的比较。 Is there any good function in Java? Java有什么好的函数吗?

Also is there any function in Java that can calculate log base 2? Java中是否还有可以计算对数基数2的函数?

I'm assuming the problems you're having with indexOf() related to you using the character version (otherwise why would you be searching for w when looking for world?).我假设您在使用字符版本时遇到的与indexOf()相关的问题(否则为什么在寻找世界时要搜索 w ?)。 If so, indexOf() can take a string argument to search for:如果是这样, indexOf()可以使用字符串参数来搜索:

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

as for log base 2, that's easy:至于 log base 2,这很简单:

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

For an exact String comparison, you can simply do:对于精确的字符串比较,您可以简单地执行以下操作:

boolean match = stringA.equals(stringB);

If you want to check that a string contains a substring, you can do:如果要检查字符串是否包含子字符串,可以执行以下操作:

boolean contains = string.contains(substring);

For more String methods, see the javadocs有关更多 String 方法,请参阅javadocs

You can use你可以使用

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

This is a simple and easy-to-use function and for a one-liner:这是一个简单易用的函数,用于单行:

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

Hi My version is as below:你好我的版本如下:

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);

        }
    }
}

Please let me know if it was useful.请让我知道它是否有用。

I got the solution finally.我终于得到了解决方案。

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);

                }


            });
    }

Replace the above code with this one:用这个替换上面的代码:

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);

                }


            });

Assume you have a below string:假设您有以下字符串:

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

Use below code for String to String comparison:使用以下代码进行字符串到字符串的比较:

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

Use either of below code to check if your string contains a substring:使用以下任一代码检查您的字符串是否包含子字符串:

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

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

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