Java中的子串搜索
我对字符串比较有问题。例如,有以下字符串:Java中的子串搜索,java,string,Java,String,我对字符串比较有问题。例如,有以下字符串: "hello world i am from heaven" 我想搜索这个字符串是否包含“world”。我使用了以下函数,但它们有一些问题。我使用了String.indexof(),但如果我尝试搜索“w”,它会说它存在 简言之,我想我在寻找精确的比较。Java中有好的函数吗 Java中是否有任何函数可以计算log base 2?我假设您在indexOf()中遇到的问题与您使用字符版本有关(否则,在查找world时为什么要搜索w?)。如果是这样,ind
"hello world i am from heaven"
我想搜索这个字符串是否包含“world”。我使用了以下函数,但它们有一些问题。我使用了String.indexof()
,但如果我尝试搜索“w”,它会说它存在
简言之,我想我在寻找精确的比较。Java中有好的函数吗
Java中是否有任何函数可以计算log base 2?我假设您在
indexOf()
中遇到的问题与您使用字符版本有关(否则,在查找world时为什么要搜索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);
}
要进行精确的字符串比较,只需执行以下操作:
boolean match = stringA.equals(stringB);
如果要检查字符串是否包含子字符串,可以执行以下操作:
boolean contains = string.contains(substring);
有关更多字符串方法,请参见Hi我的版本如下:
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);
}
}
}
请告诉我它是否有用。我终于找到了解决方案
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(字符序列、int start、int before、int count){
ArrayList arrayTemplist=新建ArrayList();
字符串searchString=mEditText.getText().toString();
if(searchString.equals(“”){new DownloadJSON().execute();}
否则{
对于(int i=0;i
将上述代码替换为以下代码:
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);
}
});
public void onTextChanged(字符序列、int start、int before、int count){
ArrayList arrayTemplist=新建ArrayList();
字符串searchString=mEditText.getText().toString();
if(searchString.equals(“”){new DownloadJSON().execute();}
否则{
对于(int i=0;i
您可以使用
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!!!!!!!");
假设您有以下字符串:
String sampleS = "hello world i am from heaven";
使用以下代码进行字符串到字符串的比较:
boolean is_Equal = sampleS.equals("<any string you want to compare>");
我分解每个字符串,为我的应用程序进行某种比较。有时它也会得到单字符,因此会产生这些问题。若我想自己传递参数,那个么我知道我可以传递字符串。但这是在运行时。你能不能请另一个解决方案?或者你也可以说,我正在寻找一个字符串中的组织,在运行时,它会得到一个字符串“或”再次它会说是存在对不起,我真的不明白你想做什么。你能解释一下吗?@agazerboy-不要把两个完全不相关的问题放在一个问题中。所以在“w”的情况下,你期望返回false的东西,因为它不是一个完整的单词?如果你也解释一下你在新代码中所做的更改以及为什么会有帮助,那就更好了。
boolean is_Contains = sampleS.contains("world");
// OR
boolean is_Contains = (sampleS.indexOf("world") != -1);