Reputation: 3653
What's the easiest and most effective way to spit a string into different arrays of types? Example:
String[] textArr;
String[] numbersArr;
and if possible a String[] doubleArr
and a String[] dateArray
z
//the string I want to split
String splitMe = "Tinus has 99 issues and has to pay $2200.50 for 26 on 2016/10/10";
After it's split it should be
String[] textArr = ["Tinus","has","issues","and","to","pay","for","on"];
String[] numbersArr = ["99","26"];
String[] doubleArr = ["2200.50"];
String[] dateArr = ["2016/10/10"];
Upvotes: 0
Views: 51
Reputation: 6063
You can try something like this:
String splitMe = "Tinus has 99 issues and has to pay $2200.50 for 26 on 2016/10/10";
String[] splitArray = splitMe.split(" ");
System.out.println("splitArray: " + Arrays.toString(splitArray));
String[] tmp = new String[splitArray.length];
int i = 0;
for (String s : splitArray) {
if (s.matches("[A-Za-z]+")) {
tmp[i] = s;
i++;
}
}
String[] textArr = new String[i];
for (int j = 0; j < textArr.length; j++) {
textArr[j] = tmp[j];
}
tmp = new String[splitArray.length];
i = 0;
for (String s : splitArray) {
if (s.matches("[0-9]+")) {
tmp[i] = s;
i++;
}
}
String[] numbersArr = new String[i];
for (int j = 0; j < numbersArr.length; j++) {
numbersArr[j] = tmp[j];
}
tmp = new String[splitArray.length];
i = 0;
for (String s : splitArray) {
if (s.matches("\\$[0-9]+\\.[0-9]+")) {
tmp[i] = s;
i++;
}
}
String[] doubleArr = new String[i];
for (int j = 0; j < doubleArr.length; j++) {
doubleArr[j] = tmp[j];
}
tmp = new String[splitArray.length];
i = 0;
for (String s : splitArray) {
if (s.matches("[0-9]+/[0-9]+/[0-9]+")) {
tmp[i] = s;
i++;
}
}
String[] dateArr = new String[i];
for (int j = 0; j < dateArr.length; j++) {
dateArr[j] = tmp[j];
}
System.out.println("textArr: " + Arrays.toString(textArr));
System.out.println("numbersArr: " + Arrays.toString(numbersArr));
System.out.println("doubleArr: " + Arrays.toString(doubleArr));
System.out.println("dateArr: " + Arrays.toString(dateArr));
Please note that the regex used are not ideal but they work for your case. I used arrays because I thought it is a strict requirement. You can use lists as well which is better.
Upvotes: 0
Reputation: 521534
I might opt for just splitting the input string by space, and then using a pattern match to check each entry to determine where it belongs:
String splitMe = "Tinus has 99 issues and has to pay $2200.50 for 26 on 2016/10/10";
String[] parts = splitMe.split(" ");
List<String> textList = new ArrayList<>();
List<String> numbersList = new ArrayList<>();
List<String> currencyList = new ArrayList<>();
List<String> dateList = new ArrayList<>();
for (String part : parts) {
if (part.matches("\\d*")) {
numbersList.add(part);
}
else if (part.matches("\\$\\d*\\.\\d*")) {
currencyList.add(part);
}
else if (part.matches("\\d{4}/\\d{2}/\\d{2}")) {
dateList.add(part);
}
else {
textList.add(part);
}
}
I didn't attempt to formally extract a double from the currency. And I also chose to use lists rather than arrays to store the various terms, because this will scale better. I will leave it up to you to fill in the details.
Upvotes: 4