tags:

views:

85

answers:

2

i am having a problem with string comparison. For example, I have 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?

+5  A: 

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);
}
cletus
I explode every string to do some kind of comparison for my application. Some time it also get single CHAR so it create these problems. If i want to pass argument by myself then i know i can pass a string. But that is at run time. Can you please another solution ?
Or you can also say that I am looking for organization in a string and at run time it get the arugment "or" again it will say YES EXIST
Sorry, I don't really understand what you're trying to do. Can you explain?
cletus
A: 

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

Richard