Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

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.

1 Answer

0 votes
by (26.7k points)

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), " - ");

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!!

Related questions

0 votes
1 answer
asked Mar 7, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Feb 8, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
asked Feb 7, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...