Reputation: 143
I have string "1000:2000,3000"
and I need to divide it into 3 strings so something like that:
"1000"
"2000"
"3000"
My code looks like:
int datas = 0;
String item = "";
String data = "";
String pocet = "";
List<String> items = "1000:2000,3000";
for (int ii = 0; ii < items.size(); ii++) {
System.out.println(items.get(ii));
int delkaid = items.get(ii).indexOf(":");
int delkadat = items.get(ii).indexOf(",") - items.get(ii).indexOf(":");
int delkapoctu = items.get(ii).length() - items.get(ii).indexOf(",");
for (int ite = 0; ite < delkaid; i++) {
item = item + items.get(ii).charAt(ite) + "";
}
for (int ite = 0; ite < delkadat; i++) {
data = data + items.get(ii).charAt(items.get(ii).indexOf(":") + 1 + ite) + "";
}
for (int ite = 0; ite < delkapoctu; i++) {
pocet = pocet + items.get(ii).charAt(items.get(ii).indexOf(",") + 1 + ite) + "";
}
datas = Integer.valueOf(data);
}
But I have some mistake in the code.
Upvotes: 0
Views: 96
Reputation: 159844
Your code doesnt compile due to the assignment of a String
to your List
but you could split on an alternation
String[] array = str.split(":|,");
A corresponding integer array of equal size can be used to hold the integer values. These can be processed in a basic for
loop
Upvotes: 6
Reputation:
Here is a link to a running example:
public static void main (final String[] args)
{
final String s = "1000:2000,3000";
final String[] a = s.split("[:,]");
for (final String n : a)
{
System.out.println(n);
}
}
Outputs
1000
2000
3000
Upvotes: 4
Reputation: 136072
this will split numbers on any delimiters
String numbers[] = line.split("\\D+");
Upvotes: 0
Reputation: 3735
You can use split()
method:
String[] tokens = str.split(":|,");
Upvotes: 2