Java中的子字符串搜索 [英] Substring search in Java

查看:115
本文介绍了Java中的子字符串搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到字符串比较问题。例如,有这个字符串:

 我来自天堂的世界

我想搜索此字符串是否包含world。我使用了以下功能,但它们有一些问题。我使用 String.indexof(),但如果我尝试搜索w,它会说它存在。



<总之,我认为我正在寻找确切的比较。 Java中是否有任何好的功能?



Java中是否还有可以计算log base 2的函数?

解决方案

我假设您使用角色版本与<* c $ c> indexOf()有关的问题(否则为什么会你在寻找世界时寻找w?)。如果是这样, indexOf()可以使用字符串参数来搜索:

  String s =你好天堂我来自天堂; 
if(s.indexOf(world)!= -1){
//它包含world
}

对于log base 2,这很容易:

  public static double log2 (双d){
返回Math.log(d)/ Math.log(2.0d);
}


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

这篇关于Java中的子字符串搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆