Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
join list of strings
lajos kamocsay
Ranch Hand
Posts: 37
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Is there an easy way to join a list of
string
in
java
using a delimiter?
For example:
ArrayList<String> pathList = new ArrayList<String>(); pathList.add("home"); pathList.add("joe"); pathList.add("tmp");
I would like to make that into a path string, "/home/joe/tmp/". I can do:
StringBuilder path = new StringBuilder(File.separator); for (String s : pathList) { path.append(s); path.append(File.separator); }
But it seems there should be something in the API to do this ain one step. Python has:
String.join(["home","joe","tmp"],"/");
Anything like that in Java?
Thanks.
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
I like...
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
No.
[Jess in Action]
[AskingGoodQuestions]
Ken Blair
Ranch Hand
Posts: 1078
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
No, but you could write one fairly easily using varargs in 1.5:
public String join(final String delimeter, final String... strings) { final StringBuilder buff = new StringBuilder(delimeter); for (String s : strings) { buff.append(s).append(delimeter); } return buff.toString(); }
Usage: join("/", "home", "joe", "tmp)
Curse your sudden but inevitable betrayal! And this tiny ad too!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
large system.in to object concurrent operations
NullPointerException Just Does Not Make Sense
How to pass a dynamic parameter in struts-config.xml?
Listing all directories from a given root in batch file
Different reults of execution of jar from terminal and by double clicking
More...