java - How to add characters to a string in specific indexes? -


i have string formed 6 letters eg: "abcdef". need add "." every 2 characters this: "ab.cd.ef". i'm working in java, tried this:

private string formataddress(string sourceaddress) {     char[] sourceaddressformatted = new char[8];     sourceaddress.getchars(0, 1, sourceaddressformatted, 0);     sourceaddress += ".";     sourceaddress.getchars(2, 3, sourceaddressformatted, 3);     sourceaddress += ".";     sourceaddress.getchars(4, 5, sourceaddressformatted, 6);     string s = new string(sourceaddressformatted);     return s; } 

but received strange values such [c@2723b6.

thanks in advance:)

you should fix

    string sourceaddress = "abcdef";     string s = sourceaddress.substring(0, 2);     s += ".";     s += sourceaddress.substring(2, 4);     s += ".";     s += sourceaddress.substring(4, 6);     system.out.println(s); 

you can same regex, it's 1 line solution

    string s = sourceaddress.replaceall("(\\w\\w)(?=\\w\\w)", "$1.");     system.out.println(s); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -