Related
I want the Java code for converting an array of strings into an string.
Java 8+
Use String.join():
String str = String.join(",", arr);
Note that arr can also be any Iterable (such as a list), not just an array.
If you have a Stream, you can use the joining collector:
Stream.of("a", "b", "c")
.collect(Collectors.joining(","))
Legacy (Java 7 and earlier)
StringBuilder builder = new StringBuilder();
for(String s : arr) {
builder.append(s);
}
String str = builder.toString();
Alternatively, if you just want a "debug-style" dump of an array:
String str = Arrays.toString(arr);
Note that if you're really legacy (Java 1.4 and earlier) you'll need to replace StringBuilder there with StringBuffer.
Android
Use TextUtils.join():
String str = TextUtils.join(",", arr);
General notes
You can modify all the above examples depending on what characters, if any, you want in between strings.
DON'T use a string and just append to it with += in a loop like some of the answers show here. This sends the GC through the roof because you're creating and throwing away as many string objects as you have items in your array. For small arrays you might not really notice the difference, but for large ones it can be orders of magnitude slower.
Use Apache commons StringUtils.join(). It takes an array, as a parameter (and also has overloads for Iterable and Iterator parameters) and calls toString() on each element (if it is not null) to get each elements string representation. Each elements string representation is then joined into one string with a separator in between if one is specified:
String joinedString = StringUtils.join(new Object[]{"a", "b", 1}, "-");
System.out.println(joinedString);
Produces:
a-b-1
I like using Google's Guava Joiner for this, e.g.:
Joiner.on(", ").skipNulls().join("Harry", null, "Ron", "Hermione");
would produce the same String as:
new String("Harry, Ron, Hermione");
ETA: Java 8 has similar support now:
String.join(", ", "Harry", "Ron", "Hermione");
Can't see support for skipping null values, but that's easily worked around.
From Java 8, the simplest way I think is:
String[] array = { "cat", "mouse" };
String delimiter = "";
String result = String.join(delimiter, array);
This way you can choose an arbitrary delimiter.
You could do this, given an array a of primitive type:
StringBuffer result = new StringBuffer();
for (int i = 0; i < a.length; i++) {
result.append( a[i] );
//result.append( optional separator );
}
String mynewstring = result.toString();
Try the Arrays.deepToString method.
Returns a string representation of the "deep contents" of the specified
array. If the array contains other arrays as elements, the string
representation contains their contents and so on. This method is
designed for converting multidimensional arrays to strings
Try the Arrays.toString overloaded methods.
Or else, try this below generic implementation:
public static void main(String... args) throws Exception {
String[] array = {"ABC", "XYZ", "PQR"};
System.out.println(new Test().join(array, ", "));
}
public <T> String join(T[] array, String cement) {
StringBuilder builder = new StringBuilder();
if(array == null || array.length == 0) {
return null;
}
for (T t : array) {
builder.append(t).append(cement);
}
builder.delete(builder.length() - cement.length(), builder.length());
return builder.toString();
}
public class ArrayToString
{
public static void main(String[] args)
{
String[] strArray = new String[]{"Java", "PHP", ".NET", "PERL", "C", "COBOL"};
String newString = Arrays.toString(strArray);
newString = newString.substring(1, newString.length()-1);
System.out.println("New New String: " + newString);
}
}
You want code which produce string from arrayList,
Iterate through all elements in list and add it to your String result
you can do this in 2 ways: using String as result or StringBuffer/StringBuilder.
Example:
String result = "";
for (String s : list) {
result += s;
}
...but this isn't good practice because of performance reason. Better is using StringBuffer (threads safe) or StringBuilder which are more appropriate to adding Strings
String[] strings = new String[25000];
for (int i = 0; i < 25000; i++) strings[i] = '1234567';
String result;
result = "";
for (String s : strings) result += s;
//linear +: 5s
result = "";
for (String s : strings) result = result.concat(s);
//linear .concat: 2.5s
result = String.join("", strings);
//Java 8 .join: 3ms
Public String join(String delimiter, String[] s)
{
int ls = s.length;
switch (ls)
{
case 0: return "";
case 1: return s[0];
case 2: return s[0].concat(delimiter).concat(s[1]);
default:
int l1 = ls / 2;
String[] s1 = Arrays.copyOfRange(s, 0, l1);
String[] s2 = Arrays.copyOfRange(s, l1, ls);
return join(delimiter, s1).concat(delimiter).concat(join(delimiter, s2));
}
}
result = join("", strings);
// Divide&Conquer join: 7ms
If you don't have the choise but to use Java 6 or 7 then you should use Divide&Conquer join.
String array[]={"one","two"};
String s="";
for(int i=0;i<array.length;i++)
{
s=s+array[i];
}
System.out.print(s);
Use Apache Commons' StringUtils library's join method.
String[] stringArray = {"a","b","c"};
StringUtils.join(stringArray, ",");
When we use stream we do have more flexibility, like
map --> convert any array object to toString
filter --> remove when it is empty
join --> Adding joining character
//Deduplicate the comma character in the input string
String[] splits = input.split("\\s*,\\s*");
return Arrays.stream(splits).filter(StringUtils::isNotBlank).collect(Collectors.joining(", "));
If you know how much elements the array has, a simple way is doing this:
String appendedString = "" + array[0] + "" + array[1] + "" + array[2] + "" + array[3];
Given a list
List<String> l = new ArrayList<String>();
l.add("one");
l.add("two");
l.add("three");
I have a method
String join(List<String> messages) {
if (messages.isEmpty()) return "";
if (messages.size() == 1) return messages.get(0);
String message = "";
message = StringUtils.join(messages.subList(0, messages.size() -2), ", ");
message = message + (messages.size() > 2 ? ", " : "") + StringUtils.join(messages.subList(messages.size() -2, messages.size()), ", and ");
return message;
}
which, for l, produces "one, two, and three".
My question is, is there a standard (apache-commons) method that does the same?, eg
WhatEverUtils.join(l, ", ", ", and ");
To clarify. My problem is not getting this method to work. It works just as I want it to, it's tested and all is well. My problem is that I could not find some apache-commons-like module which implements such functionality. Which surprises me, since I cannot be the first one to need this.
But then maybe everyone else has just done
StringUtils.join(l, ", ").replaceAll(lastCommaRegex, ", and");
In Java 8 you can use String.join() like following:
Collection<String> elements = ....;
String result = String.join(", ", elements);
What about join from:
org.apache.commons.lang.StringUtils
Example:
StringUtils.join(new String[] { "one", "two", "three" }, ", "); // one, two, three
To have "and" or ", and" you can simple replace the last comma.
With Java 8, you can use streams with joiners.
Collection<String> strings;
...
String commaDelimited = strings.stream().collect(Collectors.joining(","));
// use strings.parallelStream() instead, if you think
// there are gains to be had by doing fork/join
I like using Guava for this purpose. Neat and very useful:
Joiner.on(",").join(myList)
This kind of code has been written time and time again and you should rather be freed implementing your specific implementation logic.
If you use maven, herewith the dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
It has a bunch of other wonderful cool features too!
This will produce the string "one, two, and three".
List<String> originalList = Arrays.asList("one", "two", "three");
Joiner.on(", ")
.join(originalList.subList(0, originalList.size() - 1))
.concat(", and ")
.concat(originalList.get(originalList.size() - 1));
To produce grammatical output in English there are 3 cases to consider when concatenating a list of strings:
"A"
"A and B"
"A, B, and C.
This can be accomplished using standard Java or Guava like below. The solutions are basically the same and just up to preference what you want to use.
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class JoinListTest {
#Test
public void test_join() {
// create cases (don't need to use ImmutableList builder from guava)
final List<String> case1 = new ImmutableList.Builder<String>().add("A").build();
final List<String> case2 = new ImmutableList.Builder<String>().add("A", "B").build();
final List<String> case3 = new ImmutableList.Builder<String>().add("A", "B", "C").build();
// test with standard java
assertEquals("A", joinListGrammaticallyWithJava(case1));
assertEquals("A and B", joinListGrammaticallyWithJava(case2));
assertEquals("A, B, and C", joinListGrammaticallyWithJava(case3));
// test with guava
assertEquals("A", joinListGrammaticallyWithGuava(case1));
assertEquals("A and B", joinListGrammaticallyWithGuava(case2));
assertEquals("A, B, and C", joinListGrammaticallyWithGuava(case3));
}
private String joinListGrammaticallyWithJava(final List<String> list) {
return list.size() > 1
? String.join(", ", list.subList(0, list.size() - 1))
.concat(String.format("%s and ", list.size() > 2 ? "," : ""))
.concat(list.get(list.size() - 1))
: list.get(0);
}
private String joinListGrammaticallyWithGuava(final List<String> list) {
return list.size() > 1
? Joiner.on(", ").join(list.subList(0, list.size() - 1))
.concat(String.format("%s and ", list.size() > 2 ? "," : ""))
.concat(list.get(list.size() - 1))
: list.get(0);
}
}
Other answers talk about "replacing the last comma", which isn't safe in case the last term itself contains a comma.
Rather than use a library, you can just use one (albeit long) line of JDK code:
public static String join(List<String> msgs) {
return msgs == null || msgs.size() == 0 ? "" : msgs.size() == 1 ? msgs.get(0) : msgs.subList(0, msgs.size() - 1).toString().replaceAll("^.|.$", "") + " and " + msgs.get(msgs.size() - 1);
}
See a live demo of this code handling all edge cases.
FYI, here's a more readable two-liner:
public static String join(List<String> msgs) {
int size = msgs == null ? 0 : msgs.size();
return size == 0 ? "" : size == 1 ? msgs.get(0) : msgs.subList(0, --size).toString().replaceAll("^.|.$", "") + " and " + msgs.get(size);
}
I don't know any Apache String joiner that can support adding and in the joined String.
Here's an untested code that will do what you asked:
public static String join(String separator, List<String> mList, boolean includeAndInText) {
StringBuilder sb = new StringBuilder();
int count = 0;
for (String m: mList) {
if (includeAndInText && (count + 1 != mList.size())) {
sb.append (" and ");
}
sb.append(m);
count++;
if (count < mList.size()) {
sp.append(separator);
}
}
return sb.toString();
}
If you want a more comprehensive solution, there is a brilliant NLG library for that - SimpleNLG
//initialize
NLGFactory nlgFactory = new NLGFactory(Lexicon.getDefaultLexicon());
Realiser realiser = new Realiser(lexicon);
CoordinatedPhraseElement cp = nlgFactory.createCoordinatedPhrase();
cp.setConjunction("and");
//code begins here
List<String> l = new ArrayList<String>();
l.add("one");
l.add("two");
l.add("three");
l.forEach(cp::addCoordinate);
//output
String output = realiser.realise(cp).toString();
This can support any number of array elements without needing to do ugly hacks like "remove last comma".
Improved version from Bohemian♦'s answer. You can choose to remove the nulled items check on personal preferences.
/** Auto Concat Wrapper
* Wraps a list of string with comma and concat the last element with "and" string.
* E.g: List["A", "B", "C", "D"] -> Output: "A, B, C and D"
* #param elements
*/
public static String join(List<String> elements){
if(elements==null){return "";}
List<String> tmp = new ArrayList<>(elements);
tmp.removeAll(Collections.singleton(null)); //Remove all nulled items
int size = tmp.size();
return size == 0 ? "" : size == 1 ? tmp.get(0) : String.join(", ", tmp.subList(0, --size)).concat(" and ").concat(tmp.get(size));
}
Test results:
List<String> w = Arrays.asList("A");
List<String> x = Arrays.asList("A", "B");
List<String> y = Arrays.asList("A", "B", null, "C");
List<String> z = Arrays.asList("A", "B", "C", "D");
System.out.println(join(w));//A
System.out.println(join(x));//A and B
System.out.println(join(y));//A, B and C
System.out.println(join(z));//A, B, C and D
In this way we can join
List<Long> ids = new ArrayList<>();
String idsAsString = String.join(",", ids);
System.out.println(idsAsString);
I have some strings defined in my Java application, like so:
m3 = "T, V, E";
m2 = "T, W, E";
as an example.
Now I need to check, which parts of the strings match. So in this case, I would want a string m4, containing T, E, as a result.
In that case for example:
m1 = "A, S";
m3 = "T, V, E";
i would want an empty (but declared) string.
Or is there a better way, to do it with another method then with strings? I'm actually declaring those strings by hand. Would an array be better? If yes, how could I do it with arrays?
In Java 8 you can proceed as below :
String s1 = "T,V,E";
String s2 = "T,W,E";
List<String> l1 = Arrays.asList(s1.split(","));
List<String> l2 = Arrays.asList(s2.split(","));
List<String> result = l1.stream().filter(l2::contains).collect(Collectors.toList());
System.out.println(String.join(",", result));
The result is "T,E" as expected.
You can achieve this in many ways. One of the ways is using Set.
First, split m1 the characters by (comma) and add it to HashSet. Then split the m2 characters and add it to ArrayList. Now by the for loop try to add the ArrayList characters to HashSet. You will get false from the add method (Set.add()) if it is not added (because the character is already there). If you get false print the character or add it to another ArrayList.
String m3 = "T, V, E";
String m2 = "T, W, E";
Set<String> set = new HashSet<>(Arrays.asList(m3.split(",")));
List<String> list = Arrays.asList(m2.split(","));
for (String s : list) {
if (!set.add(s)) {
System.out.println(s);
}
}
Result will be T and E
The appropriate data structure is Set.
Set<String> m3 = new TreeSet<>();
Collections.addAll(m3, "T", "V", "E");
Collections.addAll(m3, "T, V, E".split(",\\s*")); // Alternatively.
Set<String> m2 = new HashSet<>();
Collections.addAll(m2, "T", "W", "E");
Set<String> m5 = new TreeSet<>(m2);
m5.retainAll(m3);
Java 9:
Set<String> m3 = Set.of("T", "V", "E");
Set<String> m2 = Set.of("T", "W", "E");
you can use the split() function as the following
String a="A, B, C, D";
String b="B, C, D";
String[] a_chars =a.split(", "); //returns array with A B C D
String[] b_chars =b.split(", "); //returns array with B C D
this whay you have 2 arrays of strings now you can compare them using 2 (for) loops
String result="";
for(int c=0;c<a_chars.length;c++)
{
for(int d=0;d<b_chars.length;d++)
{
if(a_chars[c].equals(b_chars[d]))
{
result+=a_chars[c]+", ";
}
}
}
now you have the result string like this result="B, C, D, "
check of the length of result is greater than zero
if so erase the lase 2 characters which are ,
if(result.length()>0)result=result.substring(0,result.length()-2);
if the length of the result string is zero that means there is no matching letters so no need to modify it
If all your String follows this pattern : ..., ..., .... , you could split them and filter only each String that is contained in the two arrays.
You can then collect them into a List and join them with , :
List<String> commonStrings = Arrays.stream(m2.split(",\\s"))
.flatMap(s-> Arrays.stream(m3.split(",\\s"))
.filter(s2.equals(s)))
.collect(Collectors.toList());
String joinedString = String.join(",", commonStrings);
Note that this code doesn't return the exact number of equals occurrences in the two Strings.
So if one String contains two A and the other one A, you will get two A in the result.
If it doesn' matter and you want to get only distinct String, you can invoke distinct() before collect().
Otherwise, to return the exact number of equals occurrences, during the processing, as soon as a String part is consumed (A for example) as the two parts are equal in the two Strings, you could create new Strings from the actual Strings but by removing this String part .
String s1 = "T,V,E";
String s2 = "T,W,E";
List<String> l1 = Arrays.asList(s1.split(","));
List<String> l2 = Arrays.asList(s2.split(","));
List<String> result = l1.stream().filter(l2::contains).collect(Collectors.toList());
System.out.println(String.join(",", result));
result = T&E
This is good answer I will tell you too this
Given this function signature:
public static void test(final String... args) {}
I would like to fit a string and an array of strings into the args:
test("a", new String[]{"b", "c"});
But it is not possible because the second argument is not expanded.
So is it possible to expand an array to fit into varargs?
If that is not possible, what is the shortest way to construct a concatenated string array given one string and a string array? E.g:
String a = "a";
String[] b = new String[]{"b", "c"};
String[] c = // To get: "a", "b", "c"
Thank you.
You can use Guava's ObjectArrays.concat(T, T[]) method:
String a = "a";
String[] b = new String[]{"b", "c"};
String[] c = ObjectArrays.concat(a, b);
Notice the order of arguments. Invoking with (b, a) will also work, but that will append the element to the array rather than prepend (which is what you seem to want). This internally uses System.arraycopy() method.
I don't know any other shorter ways but this is pretty clean to me. With plain java (without ant libraries)
String a = "a";
String[] b = new String[] { "b", "c" };
String[] c = new String[b.length + 1];
c[0]=a;
System.arraycopy(b, 0, c, 1, b.length);
That will work no matter what is the size of b
You could just create a separate array, and put the String "a" first into that array, and then copy the array b[] into the array using System.arraycopy(), and then pass the new array to the method.
You can use StringBuilder:
StringBuilder sb = new StringBuilder();
sb.append(a);
for(String s : b) {
sb.append(s);
}
And then you can convert it back to ArrayList or array of Strings...
Shortest way which comes to my mind (BUT not clean code):
test((a + "," + Arrays.toString(b)).replaceAll("[\\[\\] ]", "").split(","));
Test:
public static void main(String[] args) {
String a = "a";
String[] b = new String[] {"b", "c"};
System.out.println(Arrays.toString(
(a + "," + Arrays.toString(b)).replaceAll("[\\[\\] ]", "").split(",")));
}
Output:
[a, b, c]
Given a list
List<String> l = new ArrayList<String>();
l.add("one");
l.add("two");
l.add("three");
I have a method
String join(List<String> messages) {
if (messages.isEmpty()) return "";
if (messages.size() == 1) return messages.get(0);
String message = "";
message = StringUtils.join(messages.subList(0, messages.size() -2), ", ");
message = message + (messages.size() > 2 ? ", " : "") + StringUtils.join(messages.subList(messages.size() -2, messages.size()), ", and ");
return message;
}
which, for l, produces "one, two, and three".
My question is, is there a standard (apache-commons) method that does the same?, eg
WhatEverUtils.join(l, ", ", ", and ");
To clarify. My problem is not getting this method to work. It works just as I want it to, it's tested and all is well. My problem is that I could not find some apache-commons-like module which implements such functionality. Which surprises me, since I cannot be the first one to need this.
But then maybe everyone else has just done
StringUtils.join(l, ", ").replaceAll(lastCommaRegex, ", and");
In Java 8 you can use String.join() like following:
Collection<String> elements = ....;
String result = String.join(", ", elements);
What about join from:
org.apache.commons.lang.StringUtils
Example:
StringUtils.join(new String[] { "one", "two", "three" }, ", "); // one, two, three
To have "and" or ", and" you can simple replace the last comma.
With Java 8, you can use streams with joiners.
Collection<String> strings;
...
String commaDelimited = strings.stream().collect(Collectors.joining(","));
// use strings.parallelStream() instead, if you think
// there are gains to be had by doing fork/join
I like using Guava for this purpose. Neat and very useful:
Joiner.on(",").join(myList)
This kind of code has been written time and time again and you should rather be freed implementing your specific implementation logic.
If you use maven, herewith the dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>
It has a bunch of other wonderful cool features too!
This will produce the string "one, two, and three".
List<String> originalList = Arrays.asList("one", "two", "three");
Joiner.on(", ")
.join(originalList.subList(0, originalList.size() - 1))
.concat(", and ")
.concat(originalList.get(originalList.size() - 1));
To produce grammatical output in English there are 3 cases to consider when concatenating a list of strings:
"A"
"A and B"
"A, B, and C.
This can be accomplished using standard Java or Guava like below. The solutions are basically the same and just up to preference what you want to use.
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class JoinListTest {
#Test
public void test_join() {
// create cases (don't need to use ImmutableList builder from guava)
final List<String> case1 = new ImmutableList.Builder<String>().add("A").build();
final List<String> case2 = new ImmutableList.Builder<String>().add("A", "B").build();
final List<String> case3 = new ImmutableList.Builder<String>().add("A", "B", "C").build();
// test with standard java
assertEquals("A", joinListGrammaticallyWithJava(case1));
assertEquals("A and B", joinListGrammaticallyWithJava(case2));
assertEquals("A, B, and C", joinListGrammaticallyWithJava(case3));
// test with guava
assertEquals("A", joinListGrammaticallyWithGuava(case1));
assertEquals("A and B", joinListGrammaticallyWithGuava(case2));
assertEquals("A, B, and C", joinListGrammaticallyWithGuava(case3));
}
private String joinListGrammaticallyWithJava(final List<String> list) {
return list.size() > 1
? String.join(", ", list.subList(0, list.size() - 1))
.concat(String.format("%s and ", list.size() > 2 ? "," : ""))
.concat(list.get(list.size() - 1))
: list.get(0);
}
private String joinListGrammaticallyWithGuava(final List<String> list) {
return list.size() > 1
? Joiner.on(", ").join(list.subList(0, list.size() - 1))
.concat(String.format("%s and ", list.size() > 2 ? "," : ""))
.concat(list.get(list.size() - 1))
: list.get(0);
}
}
Other answers talk about "replacing the last comma", which isn't safe in case the last term itself contains a comma.
Rather than use a library, you can just use one (albeit long) line of JDK code:
public static String join(List<String> msgs) {
return msgs == null || msgs.size() == 0 ? "" : msgs.size() == 1 ? msgs.get(0) : msgs.subList(0, msgs.size() - 1).toString().replaceAll("^.|.$", "") + " and " + msgs.get(msgs.size() - 1);
}
See a live demo of this code handling all edge cases.
FYI, here's a more readable two-liner:
public static String join(List<String> msgs) {
int size = msgs == null ? 0 : msgs.size();
return size == 0 ? "" : size == 1 ? msgs.get(0) : msgs.subList(0, --size).toString().replaceAll("^.|.$", "") + " and " + msgs.get(size);
}
I don't know any Apache String joiner that can support adding and in the joined String.
Here's an untested code that will do what you asked:
public static String join(String separator, List<String> mList, boolean includeAndInText) {
StringBuilder sb = new StringBuilder();
int count = 0;
for (String m: mList) {
if (includeAndInText && (count + 1 != mList.size())) {
sb.append (" and ");
}
sb.append(m);
count++;
if (count < mList.size()) {
sp.append(separator);
}
}
return sb.toString();
}
If you want a more comprehensive solution, there is a brilliant NLG library for that - SimpleNLG
//initialize
NLGFactory nlgFactory = new NLGFactory(Lexicon.getDefaultLexicon());
Realiser realiser = new Realiser(lexicon);
CoordinatedPhraseElement cp = nlgFactory.createCoordinatedPhrase();
cp.setConjunction("and");
//code begins here
List<String> l = new ArrayList<String>();
l.add("one");
l.add("two");
l.add("three");
l.forEach(cp::addCoordinate);
//output
String output = realiser.realise(cp).toString();
This can support any number of array elements without needing to do ugly hacks like "remove last comma".
Improved version from Bohemian♦'s answer. You can choose to remove the nulled items check on personal preferences.
/** Auto Concat Wrapper
* Wraps a list of string with comma and concat the last element with "and" string.
* E.g: List["A", "B", "C", "D"] -> Output: "A, B, C and D"
* #param elements
*/
public static String join(List<String> elements){
if(elements==null){return "";}
List<String> tmp = new ArrayList<>(elements);
tmp.removeAll(Collections.singleton(null)); //Remove all nulled items
int size = tmp.size();
return size == 0 ? "" : size == 1 ? tmp.get(0) : String.join(", ", tmp.subList(0, --size)).concat(" and ").concat(tmp.get(size));
}
Test results:
List<String> w = Arrays.asList("A");
List<String> x = Arrays.asList("A", "B");
List<String> y = Arrays.asList("A", "B", null, "C");
List<String> z = Arrays.asList("A", "B", "C", "D");
System.out.println(join(w));//A
System.out.println(join(x));//A and B
System.out.println(join(y));//A, B and C
System.out.println(join(z));//A, B, C and D
In this way we can join
List<Long> ids = new ArrayList<>();
String idsAsString = String.join(",", ids);
System.out.println(idsAsString);