I want to convert an long array values to a specific format of string.
for e.g. longArray = {0,1,2,3} to be converted as string 0.1.2.3
I can do Arrays.toString(longArray) which will return [0, 1, 2, 3].
Now this string [0,1,2,3] has to be converted into 0.1.2.3
I have used this code which works but would like to see if this code can be improved
String convertedString = Arrays.toString(longArray).replaceAll(",",".").replaceAll("[\\[,\\],\\s]", "");
I have to mention that i am on Java 7 so can't use any Java 8 features like streams Best Regards,
Saurav
.as the character. If java doesn't, just loop through the array.. make your own string.[]doesn't use comma to separate characters. A character class matchingaandbwould be[ab], not[a,b]. Also, super minor improvement, the secondreplaceAllcall shortens the text, so flipping the order will leave fewer characters for the otherreplaceAllcall to process:.replaceAll("[\\[\\]\\s]+", "").replaceAll(",",".")(see this comment thread about the+I added).Arrays.toString(longArray).replace(", ", ".").replaceFirst(".(.*).", "$1");