如何找到仅包含/包含给定char序列的单词 [英] How do I find words that only contain/consist of a given char sequence
本文介绍了如何找到仅包含/包含给定char序列的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望遍历字典文件并查找仅包含给定字符的字词
I wish to loop through a dictionary file and find words that contain only the given characters
示例dgo
期望的结果:狗,上帝
不包含(在其中)给定字符的单词
NOT words that contain (within them) the given characters
I我正在使用以下代码:
I am working with the following code:
while((dictionaryWord = br_.readLine()) != null)
{
if(dictionaryWord.contains(getWord()))
System.out.println(dictionaryWord);
}
但是这给了我包含给定字符的所有单词 - - 不希望
But this gives me all words which contain the given characters -- NOT DESIRED
推荐答案
没有正则表达式:
public static boolean sameCharacters(String left, String right) {
return sortCharacters(left).equals(sortCharacters(right));
}
private static String sortCharacters(String s) {
final char[] chars = s.toCharArray();
Arrays.sort(chars);
return String.valueOf(chars);
}
更新:性能更佳的版本(感谢 user384706 ):
UPDATE: better performing version (thanks to user384706):
public static boolean sameCharacters(String left, String right) {
return Arrays.equals(sortCharacters(left), sortCharacters(right));
}
private static char[] sortCharacters(String s) {
final char[] chars = s.toCharArray();
Arrays.sort(chars);
return chars;
}
这篇关于如何找到仅包含/包含给定char序列的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文