简体   繁体   中英

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). Override the add() function to not accept any characters that you don't want to use. Then put the contents in a char array.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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