This question already has answers here:
Best way to concatenate List of String objects? [duplicate]
(19 answers)
Java: convert List<String> to a join()d String
(23 answers)
Closed 2 months ago.
I have list with some values:
List<String> list = Arrays.asList("1","2","3");
I want convert it to String "1; 2; 3"
I have only idea with loop. It works but it doesn't looks elegant. Maybe somebody can advice more easy way to do it?
Try to use String.join("; ", list)
It should work
Related
This question already has answers here:
How do I decompose a URL into its component parts in Java?
(5 answers)
In java, what's the best way to read a url and split it into its parts?
(5 answers)
Closed 3 years ago.
I have couple of URL's like
http://toidsu.abc.tnd:9083/login/pages/selection.xhtml#
http://toifsmdu.abc.tnd:9081/login/pages/selection.xhtml#
I want to get string up to 'http://toidsu.abc.tnd:9083' and 'http://toifsmdu.abc.tnd:9081'
How to do it?
Use this class to parse it for you: java.net.URI
This question already has answers here:
Java Equivalent to .NET's String.Format
(6 answers)
String replacement in java, similar to a velocity template
(12 answers)
Closed 4 years ago.
I need to augment some string like it's done by logback/famous loggers or property accessors.
String str= "I can {0} give {1} back your horses or restore your {2} to life.";
String[] got=new String[]{"not","you","dead"};
Are they any utility functions provided by any library that can convert str to replace {i} with indices in array?
Its most probably a duplicate and please refer me to some similar question.
This question already has answers here:
How to convert a Java 8 Stream to an Array?
(9 answers)
Closed 5 years ago.
I have a List of 'Client' objects each one with a field "email".
I need something like:
List<String> listEmails = clients.stream().map(client->client.getEmail())
.collect(Collectors.toList());
...but returning directly a String[].
Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?
Sure :
String[] result = clients
.stream()
.map(client->client.getEmail())
.toArray(String[]::new)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I've used
String[] red = linija.split("(?!^)");
and now in this String array I want to know if red[0] is "/". Tried (red[0] == "/") and with "//", please help.
Have you tried red[0].equals("/") ?
This question already has answers here:
make arrayList.toArray() return more specific types
(6 answers)
Convert ArrayList<String> to String[] array [duplicate]
(6 answers)
Closed 8 years ago.
I have this code:
java.util.List<String> result = new java.util.LinkedList<>();
And have added some strings to it.
But I want to return a String[]. Ideally I'd like to write
return (String[])result.toArray();
But I get a casting error at runtime. (Cannot convert Object[] to String[]). Is there a way round this that doesn't involve manual element by element copy?
Try this:
return result.toArray(new String[result.size()]);
Exactly for this there is the toArray(T[] arr) method so you can pass it a String array