To insert dots (.
) into a String (not a number), at every 3 positions, starting at the end, you could do this:
private static String format(String s) {
if (s.length() <= 3)
return s;
int first = (s.length() - 1) % 3 + 1;
StringBuilder buf = new StringBuilder(s.substring(0, first));
for (int i = first; i < s.length(); i += 3)
buf.append('.').append(s.substring(i, i + 3));
return buf.toString();
}
Test
for (String s = "1"; s.length() <= 30; s += (s.length() + 1) % 10)
System.out.printf("%-30s -> %s%n", s, format(s));
Output
1 -> 1
12 -> 12
123 -> 123
1234 -> 1.234
12345 -> 12.345
123456 -> 123.456
1234567 -> 1.234.567
12345678 -> 12.345.678
123456789 -> 123.456.789
1234567890 -> 1.234.567.890
12345678901 -> 12.345.678.901
123456789012 -> 123.456.789.012
1234567890123 -> 1.234.567.890.123
12345678901234 -> 12.345.678.901.234
123456789012345 -> 123.456.789.012.345
1234567890123456 -> 1.234.567.890.123.456
12345678901234567 -> 12.345.678.901.234.567
123456789012345678 -> 123.456.789.012.345.678
1234567890123456789 -> 1.234.567.890.123.456.789
12345678901234567890 -> 12.345.678.901.234.567.890
123456789012345678901 -> 123.456.789.012.345.678.901
1234567890123456789012 -> 1.234.567.890.123.456.789.012
12345678901234567890123 -> 12.345.678.901.234.567.890.123
123456789012345678901234 -> 123.456.789.012.345.678.901.234
1234567890123456789012345 -> 1.234.567.890.123.456.789.012.345
12345678901234567890123456 -> 12.345.678.901.234.567.890.123.456
123456789012345678901234567 -> 123.456.789.012.345.678.901.234.567
1234567890123456789012345678 -> 1.234.567.890.123.456.789.012.345.678
12345678901234567890123456789 -> 12.345.678.901.234.567.890.123.456.789
123456789012345678901234567890 -> 123.456.789.012.345.678.901.234.567.890
Since you specifically asked for doing it on a string, it can do it on any string, e.g.
Hello World! -> Hel.lo .Wor.ld!