A quick and easy way to join array elements with a separator (the opposite of split) in Java
Using Java 8 you can do this in a very clean way:
String.join(delimiter, elements);
This works in three ways:
1) directly specifying the elements
String joined1 = String.join(",", "a", "b", "c");
2) using arrays
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);
3) using iterables
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
Java: join array of primitives with separator
Here what I came up with. There are several way to do this and they are depends on the tools you using.
Using StringUtils
and ArrayUtils
from Common Lang:
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");
You can't just use StringUtils.join(arr, " - ");
because StringUtils
doesn't have that overloaded version of method. Though, it has method StringUtils.join(int[], char)
.
Works at any Java version, from 1.2.
Using Java 8 streams:
Something like this:
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = Arrays.stream(arr)
.mapToObj(String::valueOf)
.collect(Collectors.joining(" - "));
In fact, there are lot of variations to achive the result using streams.
Java 8's method String.join()
works only with strings, so to use it you still have to convert int[]
to String[]
.
String[] sarr = Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new);
String result = String.join(" - ", sarr);
If you stuck using Java 7 or earlier with no libraries, you could write your own utility method:
public static String myJoin(int[] arr, String separator) {
if (null == arr || 0 == arr.length) return "";
StringBuilder sb = new StringBuilder(256);
sb.append(arr[0]);
//if (arr.length == 1) return sb.toString();
for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);
return sb.toString();
}
Than you can do:
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = myJoin(arr, " - ");
Opposite of String.Split with separators (.net)
Found the answer. It's called String.Join.
A quick and easy way to join array elements with a separator (the opposite of split) in Java
Using Java 8 you can do this in a very clean way:
String.join(delimiter, elements);
This works in three ways:
1) directly specifying the elements
String joined1 = String.join(",", "a", "b", "c");
2) using arrays
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);
3) using iterables
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
Alternative for String.join in Android?
You can use TextUtils.join
instead:
String result = TextUtils.join(", ", list);
(String.join
was added in Java 8, which is why you can't use it in Android.)
Convert string array to strings with a whitespace separating them
You can use this code to get String with whitespace.
String[] array = {"a", "b", "c"};
String output = "";
for (int i = 0; i < array.length; i++) {
output += array[i] + " ";
}
My reverse a string code includes a space at the end of the string
Avoid space at end by adding a if statement which skips last iteration
public class Reverse {
public static void main(String[] args){
for(int i = args.length - 1; i >= 0; i--){
for(int j = args[i].length() - 1; j >= 0; j--){
System.out.print(args[i].charAt(j));
}
if(i != 0){
System.out.print(" ");
}
}
}
}
Related Topics
Difference Between Dto, Vo, Pojo, Javabeans
Android Listview Selected Item Stay Highlighted
How to Set Addsnapshotlistener and Remove in Populateviewholder in Recyclerview Item
Using Singleton Design Pattern for SQLitedatabase
How to Set Ld_Library_Path for Java Process
Problems Installing Java Ee Sdk on Linux
Cassandra Startup Error 1.2.6 on Linux X86_64
Java Installation Issues on Ubuntu
Create a Java Executable with Eclipse
How to Mock Void Methods With Mockito
Maximum Length of Intent Putextra Method? (Force Close)
Recyclerview Horizontal Scrolling to Left
Tool for Creating a Java Daemon Service on Linux