我可以使用txt文件或通過Scanner類(我可能會使用)輸入,但我需要比較字符串並檢查它們是否包含相同的字符。如果有重複的字符,它仍然是真實的,例如,abc
和aaabbbccc
將是相同的。檢查一對字符串是否包含相同的字符?
回答
你可以做這樣的事情:
//just call the checkChars method with 2 strings and it will check both strings to make sure
//that all the characters are contained in both strings.
public static void main(String args[]) {
String test = "abc";
String test1 = "abcaaabbc";
System.out.println("" + checkChars(test, test1));
}
public static String checkChars(String string1, String string2)
{
boolean isContained = false;
char[] char1 = string1.toCharArray();
char[] char2 = string2.toCharArray();
for(int i = 0; i < char1.length; i++)
{
isContained = false;
for(int j = 0; j < char2.length; j++)
{
if(char1[i] == char2[j])
{
isContained = true;
break;
}
}
if(isContained == false)
{
break;
}
}
if(isContained == true)
{
for(int i = 0; i < char2.length; i++)
{
isContained = false;
for(int j = 0; j < char1.length; j++)
{
if(char2[i] == char1[j])
{
isContained = true;
break;
}
}
if(isContained == false)
{
break;
}
}
}
if(isContained)
{
return "Both strings contain the same characters.";
}
else
{
return "The strings have different characters.";
}
}
可以使用checkChars方法用於比較兩個字符串爲相同的字符。
我在我的主要方法中調用它,但我想要返回像「兩個字符串是相同的」而不是隻是true或false,我是否需要將返回類型設置爲一個字符串? – user3247712 2014-11-21 20:12:16
是的,我會編輯,所以你可以看到如何做到這一點... – brso05 2014-11-21 20:18:18
@ user3247712你去了我編輯它返回一個字符串消息... – brso05 2014-11-21 20:20:12
既然你不擔心字符數,你的問題比一個更常見的問題要簡單得多:anagrams。作爲Pshemo的評論指出,使用Set
S:
boolean hasSameChars(String s, String t) {
Set<Character> sChars = new HashSet<Character>(Chars.asList(s.toCharArray());
Set<Character> tChars = new HashSet<Character>(Chars.asList(t.toCharArray());
return sChars.equals(tChars);
}
如果你想檢查的字符數也是兩個字符串相同,然後用Map<String, Integer>
或Multiset<String>
跟蹤計數。
爲了使答案簡明扼要,我使用了番石榴,但這個想法顯然獨立於番石榴的方法。
隨着堆棧溢出快速搜索,我得到這個 刪除重複的字符 - 僅>檢查它們是否相同
公共靜態無效的主要(字串[] args){
String s1 = "abc";
String s2 = "aaabbbccc";
System.out.println(check(dupRemove(s1), dupRemove(s2)));
}
static boolean check(String a, String b){
char[] c1 = a.toCharArray();
char[] c2 = b.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
return Arrays.equals(c1, c2);
}
static String dupRemove(String s){
StringBuilder clean = new StringBuilder();
for (int i = 0; i<s.length(); i++){
String j = s.substring(i, i + 1);
if(clean.indexOf(j)==-1){
clean.append(j);
}
}
return clean.toString();
}
- 1. 檢查2個字符串是否包含相同的字符?
- 2. 如何檢查一個字符串是否包含兩個相同的字符?
- 3. 檢查字符串是否包含字(不是子字符串!)
- 4. Applescript:檢查一個字符串是否包含空字符串?
- 5. 檢查是否字符串包含「HTTP://」
- 6. 檢查行是否包含字符串
- 7. 檢查一個字符串是否只包含特殊字符
- 8. React-Native:檢查字符串是否包含字符串
- 9. 檢查字符串是否不包含其他字符串
- 10. 檢查字符串是否包含子字符串
- 11. 檢查一個字符串是否包含數字和字母
- 12. PHP檢查字符串是否包含一些字母/數字
- 13. 如何檢查字符串是否包含C#中的字符?
- 14. 檢查字符串是否包含python中的特殊字符
- 15. 檢查字符串是否包含for循環的字符?
- 16. 檢查字符串是否只包含指定的字符
- 17. 檢查字符串包含的字符
- 18. 檢查一個字符串是否不包含一些文字
- 19. 熊貓 - 檢查字符串列包含一對字符串
- 20. 查找字符串是否包含字
- 21. 檢查字符串是否只包含拉丁字符?
- 22. 檢查字符串是否包含數字和符號
- 23. 如何檢查字符串是否包含某個字符?
- 24. 如何檢查是否字符串包含數字符號
- 25. Java:檢查字符串是否包含多個字符
- 26. 檢查字符串是否包含新行字符
- 27. 如何檢查一個字符串是否包含任何一些字符串
- 28. 如何檢查一個字符串是否包含一個子字符串 - mysql
- 29. 有效檢查一個字符串是否包含另一個字符串
- 30. 檢查一個字符串是否包含另一個字符串
- >排序相同的字符或可能abc = aabbbcccdddeee? – brso05 2014-11-21 19:31:11
將兩個字符串中的字符分開,並將其設置爲'Set'並比較它們的內容。 –
Pshemo
2014-11-21 19:31:26
只有相同的字符。 abc = aaabbbcccddee會被認爲是不同的。 – user3247712 2014-11-21 19:36:30