public class CharMatch {
private void matchString(String s1, String s2) {
int count = 0;
int len1 = s1.length();
int len2 = s2.length();
for (int i = 0; i < len2; i++) {
for (int j = 0; j < len1; j++) {
if (s2.charAt(i) == s1.charAt(j)) {
count++;
}
if (count == len2) {
System.out.print("True");}
}
}
}
public static void main(String args[]) throws IOException {
CharMatch cm = new CharMatch();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter First String");
String a = br.readLine();
System.out.println("Enter Second String");
String b= br.readLine();
cm.matchString(a,b);
}
}
我在寫這個代碼輸入的兩個字符串,例如輸入:s1="Hello world" s2:"woeh"
則輸出應該如果s2
比賽中的所有字符是真實的在s1
中至少一次。這是我寫的代碼,但我沒有達到理想的輸出。檢查的第二個字符串中的所有字符是否與存在於第一個字符串中的字符匹配至少一次
更多信息請參考這裏http://stackoverflow.com/questions/767759/occurrences-of-substring- in-a-string –
海報希望以任意順序查找字符,而不是按順序查找完整字符串。 – Gray
這就是我從上面的代碼中所理解的。 –