0

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

7
  • Most languages have a join function. Use . as the character. If java doesn't, just loop through the array.. make your own string. Commented Jan 17, 2018 at 16:08
  • concision-wise, given it's Java, that seems to be as good as it gets (if you don't want to include 3rd party libraries), though you could use a static import Commented Jan 17, 2018 at 16:08
  • 1
    Possible duplicate of Java: join array of primitives with separator Commented Jan 17, 2018 at 16:09
  • 1
    The regex character class [] doesn't use comma to separate characters. A character class matching a and b would be [ab], not [a,b]. Also, super minor improvement, the second replaceAll call shortens the text, so flipping the order will leave fewer characters for the other replaceAll call to process: .replaceAll("[\\[\\]\\s]+", "").replaceAll(",",".") (see this comment thread about the + I added). Commented Jan 17, 2018 at 16:23
  • Arrays.toString(longArray).replace(", ", ".").replaceFirst(".(.*).", "$1"); Commented Jan 17, 2018 at 16:34

2 Answers 2

1
    long[] longArray = {0,1,2,3};
    String s = LongStream.of(longArray)
            .mapToObj(Long::toString)
            .collect(Collectors.joining("."));

Array of longs to stream of longs, every long mapped to a String with Long.toString(long) and then joined with a delimiter ..

Originally I has String::valueOf instead of Long.toString. Thanks to @Andreas for a slightly better style.

Sign up to request clarification or add additional context in comments.

1 Comment

@Andreas Or Long::toString(long).
0

Not much shorter, but I think the regular expressions make it look more elegant:

String convertedString = Arrays.toString(longArray)
.replaceAll("^.|.$", "");
.replaceAll("[^0-9]+",".")
  1. Remove first and last character.
  2. Replace all non-numeric strings with dots.

4 Comments

seems almost same as my proposed solution...any performance improvement with your solution by using the negate operator ?
You do know that . does match a period, right? It matches any character, so it would be safer to escape it, i.e. "^\\.|\\.$"
Just the use of the +, to include all the strings, not only individual characters
@Andreas I intentionally want to remove the first and the last characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.