Back
Can anyone help me how I can able to join the elements present in the array using a separator like "- ". So the output will be something like this:
"1 - 2 - 3 - 4 - 5 - 6 - 7"
Any help would be appreciated.
You can achieve this by using the StringUtils and ArrayUtils tool:
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");
int[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");
Also, it can be work in any Java version starting from 1.2.
I hope this will help.
Want to know more about Java? Prefer this tutorial on Java Tutorial.
Want to become a Java Expert? Join Java Certification now!!
31k questions
32.8k answers
501 comments
693 users