Android Split string
To split a string in Android, you can use the split()
method of the String
class. This method takes a regular expression as a parameter, and returns an array of strings, each of which is a substring of the original string, split at the points where the regular expression matches.
For example, to split a string on the space character, you can use the following code:
String input = "This is a test string";
String[] words = input.split(" ");
This would result in the words
array containing the following elements:
words[0] = "This"
words[1] = "is"
words[2] = "a"
words[3] = "test"
words[4] = "string"
You can also use the split()
method with a regular expression to split the string on multiple characters. For example, to split a string on both the space and the comma character, you can use the following code:
String input = "This, is a test string";
String[] words = input.split("[ ,]");
This would result in the words
array containing the following elements:
words[0] = "This"
words[1] = "is"
words[2] = "a"
words[3] = "test"
words[4] = "string"