我有字符串比较问题。例如,有这样的字符串:Java中的子串搜索
"hello world i am from heaven"
我想搜索该字符串是否包含“world”。我使用了以下功能,但它们有一些问题。我使用String.indexof()
,但如果我尝试搜索“w”,它会说它存在。
总之,我认为我在寻找确切的比较。 Java中有没有好的功能?
在Java中还有什么函数可以计算log base 2吗?
我有字符串比较问题。例如,有这样的字符串:Java中的子串搜索
"hello world i am from heaven"
我想搜索该字符串是否包含“world”。我使用了以下功能,但它们有一些问题。我使用String.indexof()
,但如果我尝试搜索“w”,它会说它存在。
总之,我认为我在寻找确切的比较。 Java中有没有好的功能?
在Java中还有什么函数可以计算log base 2吗?
我假设你正在使用indexOf()
使用字符版本与您有问题(否则为什么你会成为搜索用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);
}
我爆炸每个字符串做我的应用程序的某种比较。有一段时间它也会得到单一的CHAR,所以它会产生这些问题。如果我想通过自己传递参数,那么我知道我可以传递一个字符串。但那是在运行时间。你可以请另一种解决方案吗 – user238384 2010-01-01 01:50:26
或者你也可以说我是在一个字符串,并在运行时它得到arugment“或”再次,它会说YES EXIST – user238384 2010-01-01 01:52:25
对不起,我真的不明白你想要做什么找组织。你可以解释吗? – cletus 2010-01-01 02:17:31
对于一个确切的字符串对比,可以简单地做:
boolean match = stringA.equals(stringB);
如果你想检查一个字符串是否包含子字符串,你可以这样做:
boolean contains = string.contains(substring);
更多字符串的方法,请参阅javadocs
你好我的版本是如下:
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(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 2015-12-18 09:00:23
@agazerboy - 不要把两个完全不相关的问题成为一个问题。 – 2010-01-01 02:25:39
因此,在“w”情况下,您期望返回错误的东西,因为它不是一个完整的单词? – PSpeed 2010-01-01 03:10:10