Substring search in Java

前端 未结 6 399

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 strin

6条回答
  •  离开以前
    2021-01-18 07:05

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

提交回复
热议问题