Hey everyone just wondering how I would go about splitting a number such as:
"1100000000000001"
to:
"11.00.00.00.00.00.00.01"
Possibly using regex?
Hey everyone just wondering how I would go about splitting a number such as:
"1100000000000001"
to:
"11.00.00.00.00.00.00.01"
Possibly using regex?
Using a regex is not the best solution for this problem. Constructing the string yourself offers a simple way to do this:
String str = "1234567890";
String newStr = "";
for(int i = 0; i < str.length(); i++) {
newStr += str.charAt(i);
if(i % 2 != 0 && i != str.length() - 1) {
newStr += ".";
}
}
System.out.println(newStr);
Split at every second char and join the elements of the resulting array using .
as a delimeter:
String reg = "(?<=\\G..)";
String str = "1100000000000001";
String res = Pattern.compile(reg).splitAsStream(str).collect(Collectors.joining("."));
System.out.println(res);
using regex pattern :
String str = "1100000000000001";
String pattern = "(\\d\\d)(?!$)";
System.out.println(str.replaceAll(pattern, "$1."));