java 检查一个字符串是否包含另一个字符串的所有字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27668213/
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
Check if a string has all the characters of another string
提问by Toby Mellor
String one = "This is a test";
String two = "This is a simple test";
I want to check if two
contains all the characters that are in one
, and ignore the fact it has extra characters.
我想检查是否two
包含 中的所有字符one
,并忽略它有多余字符的事实。
回答by Mureinik
The fastest thing would probably be to break them up to HashSet
s and then apply containsAll
最快的方法可能是将它们分解为HashSet
s 然后应用containsAll
public static Set<Character> stringToCharacterSet(String s) {
Set<Character> set = new HashSet<>();
for (char c : s.toCharArray()) {
set.add(c);
}
return set;
}
public static boolean containsAllChars
(String container, String containee) {
return stringToCharacterSet(container).containsAll
(stringToCharacterSet(containee));
}
public static void main(String[] args) {
String one = "This is a test";
String two = "This is a simple test";
System.out.println (containsAllChars(one, two));
}
回答by Paul Boddington
static boolean stringContains(String longer, String shorter) {
int i = 0;
for (char c : shorter.toCharArray()) {
i = longer.indexOf(c, i) + 1;
if (i <= 0) { return false; }
}
return true;
}
回答by M Anouti
Using a simple loop over the set of the characters in the first string:
在第一个字符串中的字符集上使用一个简单的循环:
String s1 = "This is a test";
String s2 = "This is a simple test";
Set<Character> chars = new HashSet<Character>();
for(int i = 0; i < s1.length(); i++) {
chars.add(s1.charAt(i));
}
for (Iterator<Character> iterator = chars.iterator(); iterator.hasNext();) {
Character character = iterator.next();
if(!s2.contains(character.toString())) {
// break and mark as not contained
break;
}
}
If words are meant to be checked, then you could split the string around whitespaces into a list of words:
如果要检查单词,则可以将空格周围的字符串拆分为单词列表:
String[] words1 = s1.split("\s");
String[] words2 = s2.split("\s");
List<String> wordList1 = Arrays.asList(words1);
List<String> wordList2 = Arrays.asList(words2);
System.out.println(wordList2.containsAll(wordList1));
回答by harold_finch
Try this. I know it's long but it works
试试这个。我知道它很长但它有效
public static void main(String[] args)
{
String String1,String2;
int i, j, count1, count2;
count1 = 0;
count2 = 0;
String1 = "This is a test";
String2 = "This is a simple test";
char[] list1 = new char[String2.length()];
char[] list2 = new char[String2.length()];
char[] list3 = new char[String2.length()];
for (i = 0; i <= String1.length() - 1; i++)
{
list1[i] = String1.charAt(i);
for (j = 0; j <= String2.length() - 1; j++)
{
list2[j] = String2.charAt(j);
if (list1[i] == list2[j])
{
i++;
count1++;
}
}
}
for (i = 0; i <= String1.length() - 1; i++)
{
list1[i] = String1.charAt(i);
for (j = 0; j <= String1.length() - 1; j++)
{
list3[j] = String1.charAt(j);
if (list1[i] == list3[j])
{
i++;
count2++;
}
}
}
if (count1 >= count2)
System.out.println(true);
else
System.out.println(false);
}
回答by justanother
two.startsWith(one)
If you aren't sure about the start position (the above assumes 0), try using the following API
如果您不确定起始位置(以上假设为 0),请尝试使用以下 API
startsWith(String, offset)