java 在java中搜索单词内的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5969755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:45:07  来源:igfitidea点击:

search string inside a word in java

javasearchstring

提问by Mohammad Adil

I want to search a word inside a string : For example let the String be "ThisIsFile.java"and let i want to search "File.java"or "IsFile"

我想在字符串中搜索一个单词:例如让字符串成为"ThisIsFile.java"并让我想要搜索"File.java""IsFile"

This is something like sql 'like'query but unfortunately i am not getting that string from database.

这类似于 sql'like'查询,但不幸的是我没有从数据库中获取该字符串。

Please suggest me any good solution for this. Thanks

请为此建议我任何好的解决方案。谢谢

回答by mmccomb

There's a variety of ways of achieving this and the method you choose will depend on the complexity of your queries. For a simple plain symbol/word match the String class provides a contains method that takes a single parameter, the String to search for - and returns true if it occurs within the search String.

有多种方法可以实现这一点,您选择的方法将取决于查询的复杂性。对于简单的简单符号/单词匹配,String 类提供了一个 contains 方法,该方法采用单个参数,即要搜索的字符串 - 如果它出现在搜索字符串中,则返回 true。

bool containsFile = myString.contains("file");

回答by Jon Skeet

Do you mean:

你的意思是:

if (haystack.contains(needle))

? Note that this won't respect word boundaries or anything like that - it just finds if one string (needle) is a substring in another (haystack).

? 请注意,这不会考虑单词边界或类似的东西 - 它只是查找一个字符串 ( needle) 是否是另一个字符串 ( ) 中的子字符串haystack

回答by Chris Thompson

Try using String.contains(). Also, check out the documentationfor more information about the method

尝试使用String.contains(). 另外,请查看文档以获取有关该方法的更多信息

回答by Arj

You should be able to use

你应该可以使用

String.contains("some string");