简体   繁体   English

如何检查字符串是否包含Java中的某些字符?

[英]How to check if string contains some character in Java?

I want to check for instance if string "ABCD" contains "DC" string in it in Java.例如,我想检查字符串“ABCD”是否在 Java 中包含“DC”字符串。 But this is not a substring example, because every time my string and checking characters will change.但这不是子字符串示例,因为每次我的字符串和检查字符都会改变。 and I store checking characters into an array, So substring failed, it only works if I have CD, or BC.并且我将检查字符存储到数组中,因此子字符串失败,只有在我有 CD 或 BC 时才有效。 and I couldn't do match since every time I call checking character from an array.并且我无法进行匹配,因为每次我从数组中调用检查字符时。 SO what should I do any suggestion所以我应该怎么做任何建议

I use this method for char and byte array.我将此方法用于charbyte数组。 After you got the index of the src array where the sub is matched with, you then can store it anywhere you want.在获得与sub匹配的src数组的索引后,您可以将其存储在任何您想要的位置。 Try this:尝试这个:

public static int indexOf(char[] src, char[] sub) {
    int limit = src.length - sub.length;
    int i, j;
    for(i = 0; i < limit +1; ++i) {
        for(j = 0; j < sub.length; ++j) {
           if (src[i+j] != sub[j]) {
               break;
           }
        }
        if (j == sub.length)
               return i;
     }
   return -1;  
} 

here's some pseudocode to get you started这是一些可以帮助您入门的伪代码

we'll call "ABCD" the source string and "DC" the target string我们将称“ABCD”为源字符串,“DC”为目标字符串

change source string to list of chars

change target string to list of chars

for each char in target list of char

    if source list does not contain target char
        return false;

return true

Get all the permutation of the source string till it contains the desired sub string获取源字符串的所有排列,直到它包含所需的子字符串

Permutation Logic based on基于的排列逻辑

public static void main(String[] args) {
    System.out.println(Test.checkIfContains("ABCD", "DC"));
}

public static Boolean checkIfContains(String main, String check) {
    return permutation("", main, check);
}

private static Boolean permutation(String prefix, String main, String check) {
    int n = main.length();
    if (n == 0) {
        if (checkFor(prefix, check)) {
            return true;
        }
    } else {
        for (int i = 0; i < n; i++) {
            if (permutation(prefix + main.charAt(i), main.substring(0, i) + main.substring(i + 1, n), check)) {
                return true;
            }
        }
    }
    return false;
}

private static boolean checkFor(String prefix, String check) {
    return prefix.contains(check);
}

As I understand it, you want to see if all of the characters of the second string (which I called checkString) are contained in the first string (refString).据我了解,您想查看第二个字符串(我称之为 checkString)的所有字符是否都包含在第一个字符串 (refString) 中。 I would proceed using a function like this我会继续使用这样的函数

private boolean checkString(String refString, String checkString) {   
    boolean a;
    for (int i; i < checkString.length(); i++) {
        for (int j; j < refString.length(); j++) {
            a |= checkString.charAt(i) == refString.charAt(j);
        }
        if (!a) return false;
    }
    return true;
}

Which return true only when all the characters in checkString are in the reference string.仅当 checkString 中的所有字符都在引用字符串中时才返回 true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM