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

saurav
  • 5,388
  • 10
  • 56
  • 101
  • Most languages have a join function. Use `.` as the character. If java doesn't, just loop through the array.. make your own string. –  Jan 17 '18 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 – DPM Jan 17 '18 at 16:08
  • 1
    Possible duplicate of [Java: join array of primitives with separator](https://stackoverflow.com/questions/38425623/java-join-array-of-primitives-with-separator) – ctwheels Jan 17 '18 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](https://stackoverflow.com/questions/43359325/removing-all-fraction-symbols-like-%c2%bc-and-%c2%bd-from-a-string/43359401#comment73794375_43359401) about the `+` I added). – Andreas Jan 17 '18 at 16:23
  • `Arrays.toString(longArray).replace(", ", ".").replaceFirst(".(.*).", "$1");` – Joop Eggen Jan 17 '18 at 16:34
  • @ctwheels i checked with the common lang's array utils and string utils...but that call is not performant..it is taking 40 ms in compared to my native solution which is taking around 10ms – saurav Jan 18 '18 at 05:01
  • @Andreas thanks switching the order has increased the performance from 10ms to 3ms – saurav Jan 18 '18 at 05:02

2 Answers2

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
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.
GalAbra
  • 5,048
  • 4
  • 23
  • 42
  • seems almost same as my proposed solution...any performance improvement with your solution by using the negate operator ? – saurav Jan 17 '18 at 16:29
  • You do know that `.` does match a period, right? It matches *any* character, so it would be safer to escape it, i.e. `"^\\.|\\.$"` – Andreas Jan 17 '18 at 16:30
  • Just the use of the `+`, to include all the strings, not only individual characters – GalAbra Jan 17 '18 at 16:30
  • @Andreas I intentionally want to remove the first and the last characters – GalAbra Jan 17 '18 at 16:38