The issue that I have is, I have a string input (s), which I then need to split by anything that is not (a-z or A-Z). The solution I've found so far is to split the String by empty spaces, but I've realised it will be more efficient to split by any non-alphabetical characters. This would then allow me to instantly output the array.
Question:
My question is, how do I split a string by any character in a string.
I basically need a version of this:
String[] inputSplit = input.split(" ", "!", ",", "?", ".", "_", "'", "@");
but it actually works without errors.
The code I've written so far isn't really relevant to the question, but I'll put it here anyway in case it helps:
import java.util.Arrays;
import java.util.Scanner;
public class StringTokens {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
// Splits by " "
String[] inputSplit = input.split(" ");
// Counts the amount of tokens
int n = inputSplit.length;
System.out.println(n);
for (int i = 0; i < inputSplit.length; i++) {
// Prints each of the items in the array
System.out.println(inputSplit[i]);
}
}
}