Java中的子字符串搜索

8

我可以帮您进行字符串比较的翻译。例如,有这样一个字符串:

"hello world i am from heaven"

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

简而言之,我认为我正在寻找精确比较。Java中有没有任何好的函数?

另外,在Java中是否有任何可以计算以2为底的对数的函数?


6
请不要将两个完全无关的问题放到同一个问题中。 - Stephen C
那么在“w”情况下,您期望返回false,因为它不是一个完整的单词? - PSpeed
6个回答

27
我假设你使用了字符版本的indexOf(),所以你遇到了问题(否则你找world时为什么会搜索w?)。如果是这样,indexOf()可以接受一个字符串参数进行搜索:

我假设你使用了字符版本的indexOf(),所以你遇到了问题(否则你找world时为什么会搜索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);
}

我将每个字符串都分解开来进行某种比较,但有时也会得到单个字符,从而引起问题。如果我想自己传递参数,那么我知道我可以传递一个字符串。但这是在运行时。你能否提供另一种解决方案? - user238384
或者你也可以说我正在查找字符串中的组织,并在运行时获取参数“or”,然后它会再次返回YES EXIST。 - user238384
4
抱歉,我不太明白你想做什么。你可以解释一下吗? - cletus

9

要进行精确的字符串比较,您可以简单地执行以下操作:

boolean match = stringA.equals(stringB);

如果你想检查一个字符串是否包含某个子字符串,可以这样做:
boolean contains = string.contains(substring);

如需更多字符串方法,请参阅javadocs


2

您可以使用

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

0

你好,我的版本如下:

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

        }
    }
}

请告诉我它是否有用。

0

我终于找到了解决方案。

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

                }


            });

最好在新代码中解释您所做的更改以及为什么会有帮助。 - Psytho

0
假设您有以下字符串:
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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接