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". 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.
In short I think I am looking for exact comparison. Is there any good function in Java?
Also is there any function in Java that can calculate log base 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?). If so, indexOf()
can take a string argument to search for:
String s = "hello world i am from heaven";
if (s.indexOf("world") != -1) {
// it contains world
}
as for log base 2, that's easy:
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
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);
精彩评论