Java中的子字符串搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1987673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:07:51  来源:igfitidea点击:

Substring search in Java

javastring

提问by user238384

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". 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.

我想搜索这个字符串是否包含“world”。我使用了以下功能,但它们有一些问题。我使用过,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的函数?

回答by cletus

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?). If so, indexOf()can take a string argument to search for:

我假设您遇到的问题indexOf()与您使用字符版本有关(否则为什么在寻找世界时要搜索 w?)。如果是这样,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);
}

回答by Richard H

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

回答by Nagarajan S R

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.

请让我知道它是否有用。

回答by Vinay Mishra

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

                }


            });

回答by TruVortex_07

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

回答by Zulkarnain_Shaikh

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