简体   繁体   English

如何使用将字符串拆分为不包含特殊字符的字符数组?

[英]How to use split a string into character array without special characters?

    Scanner _in = new Scanner(System.in);
    System.out.println("Enter an Equation of variables");
    String _string = _in.nextLine();


    char[] cArray = _string.toCharArray();

I want to remove the symbols "+,=" and I want to remove any repeating variables. 我想删除符号“ +,=”,并且我想删除任何重复的变量。

so far I have: 到目前为止,我有:

for(int i = 0; i < cArray.length; i++){
   if(cArray[i].equals(+)|| cArray[i].equals(=)){
           cArray[i] = null;
        }

}   

However, I dont know how to condence the array to remove any gaps and I don't know how to remove repeating characters, I think I am making this harder than it needs to be 但是,我不知道如何压缩数组以消除任何间隙,也不知道如何消除重复的字符,我想这使它比需要的难

您可以使用:

_string.replaceAll("[+,=]","");

This sounds like a good use for regular expressions : 这听起来像是正则表达式的好用法:

String result = _string.replaceAll("[+=]", "");

Here, the [+=] is a character class that consists of + and = . 在这里, [+=]是由+=组成的字符类 You can add other characters as required. 您可以根据需要添加其他字符。

Try the next: 尝试下一个:

public static void main(String[] args) {
    String input = "a+a+b=c+d-a";

    char[] cArray = input.replaceAll("[-+=]", "")        // gaps
                         .replaceAll("(.)(?=.*\\1)", "") // repeating
                         .toCharArray();

    System.out.println(Arrays.toString(cArray));
}

Output: 输出:

[b, c, d, a]

Or you can se another array, like this: 或者,您可以设置另一个数组,如下所示:

Scanner in = new Scanner(System.in);

String s = in.nextLine();
char [] cArray = s.toCharArray();

int count = 0;
char [] cArray2 = new char[cArray.length];

for (int i = 0; i < cArray.length; i++){
    if (cArray[i] != '+' || cArray[i] != '='){
        cArray2[count++] = cArray[i];
    }
}


for (int i = 0; i < count; i++){
    boolean repeated = false;

    for (int j = i + 1; j < count; j++){
        if (cArray2[i] == cArray2[j]){
            repeated = true;
            break;
        }
    }

    if (!repeated){
        //do what you want
    }
}

You can extend LinkedHashSet (Which enforces uniqueness and retains order). 您可以扩展LinkedHashSet(强制唯一性保留顺序)。 Override the add() function to not accept any characters that you don't want to use. 重写add()函数以不接受您不想使用的任何字符。 Then put the contents in a char array. 然后将内容放入char数组中。

public static char[] toCharArray(String str) {

    // Here I am anonymously extending LinkedHashSet
    Set<Character> characters = new LinkedHashSet<Character>() {

        // Overriding the add method
        public boolean add(Character character) {
            if (!character.toString().matches("[\\+=]")) {
                // character is not '+' or '='
                return super.add(character);
            }
            // character is '+' or '='
            return false;
        }
    };

    // Adding characters from str to the set.
    // Duplicates, '+'s, and '='s will not be added.
    for (int i = 0; i < str.length(); i++) {
        characters.add(str.charAt(i));
    }

// Put Characters from set into a char[] and return.
    char[] arrayToReturn = new char[characters.size()];
    int i = 0;
    for (Character c : characters) {
        arrayToReturn[i++] = c;
    }
    return arrayToReturn;
}

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

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