I have a string separated by semicolon like this:
"11;21;12;22;13;;14;24;15;25"
.
Look one value is missing after 13
.
I Want to populate that missing number with 13
(previous value) and store it into another string.
I have a string separated by semicolon like this:
"11;21;12;22;13;;14;24;15;25"
.
Look one value is missing after 13
.
I Want to populate that missing number with 13
(previous value) and store it into another string.
Try this.
static final Pattern PAT = Pattern.compile("([^;]+)(;;+)");
public static void main(String[] args) {
String input = "11;21;12;22;13;;;14;24;15;25";
String output = PAT.matcher(input)
.replaceAll(m -> (m.group(1) + ";").repeat(m.group(2).length()));
System.out.println(output);
}
output:
11;21;12;22;13;13;13;14;24;15;25
What have you tried so far?
You could solve this problem in many ways i think. If you like to use regular expression, you could do it that way: ([^;]*);;+
.
Or you could use a simple String.split(";");
and iterate over the resulting array. Every empty string indicates that there was this ;;
String str = "11;21;12;22;13;;14;24;15;25";
String[] a = str.split(";");
for(int i=0; i<a.length; i++){
if(a[i].length()==0){
System.out.println(a[i-1]);
}
}