Asked  6 Months ago    Answers:  5   Viewed   31 times

See Related .NET question

I'm looking for a quick and easy way to do exactly the opposite of split so that it will cause ["a","b","c"] to become "a,b,c"

Iterating through an array requires either adding a condition (if this is not the last element, add the seperator) or using substring to remove the last separator.

I'm sure there is a certified, efficient way to do it (Apache Commons?)

How do you prefer doing it in your projects?

 Answers

62

Using Java 8 you can do this in a very clean way:

String.join(delimiter, elements);

This works in three ways:

1) directly specifying the elements

String joined1 = String.join(",", "a", "b", "c");

2) using arrays

String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);

3) using iterables

List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
Tuesday, June 1, 2021
 
letrollpoilu
answered 6 Months ago
36

To start you off on your assignment, String.split splits strings on a regular expression and this expression may be an empty string:

String[] ary = "abc".split("");

Yields the array:

(java.lang.String[]) [, a, b, c]

Getting rid of the empty 1st entry is left as an exercise for the reader :-)

Note: In Java 8, the empty first element is no longer included.

Saturday, June 5, 2021
 
sassy_geekette
answered 6 Months ago
41

readdir is asynchronous. It won't get the result right away. You should log the filePaths inside the callback. The only reason why the console show the value is because the console evaluate the array when you unfold it.

When you press the little arrow on the left, put the mouse on the i box on the right. What happen is that the console keeps a reference to the array, so when the user unfold the array it then show what its current value is. But when you log filePaths.length the array is empty because readdir didn't finish reading yet that's why you get 0. But by the time you open the console and press that arrow, readdir will already be done reading and the console will print the current value of the array (after it been filled).

enter image description here

Example to demonstrate the problem: (not a solution, it's just to understand what is really happening)

Try and run this code and see what happen:

var arr = [];
setTimeout(function() {
  arr.push(1, 2, 3);
}, 5000);
console.log(arr.length);
console.log(arr);

Here the array and it's length are both logged before the array is filled. The array will be filled after 5 seconds. So the output will be 0 and a string array[]. Now because arrays could have tons of data, the console won't show that data untill the user unfold the array. So what the console does is keep a reference to the array untill the user press unfold arrow. If you unfold the array before 5 seconds you see that the array is empty (not filled yet). If you wait untill the 5 seconds pass then unfold it, then you'll see that it's filled.

Note: Also, the line that get logged to the console (something like > Array(0)) is just a string representation of the object/array at the moment the log happens. It won't get updated if the object/array changes. So that also may seem confusing sometimes.

I hope it's clear now.

Monday, June 7, 2021
 
sunshinejr
answered 6 Months ago
91

This is the correct and expected behavior. Given that you've included the separator in the string, the split function (simplified) takes the part to the left of the separator ("a,b,c,d,e:10") as the first element and the part to the rest of the separator (an empty string) as the second element.

If you're really curious about how split() works, you can check out pages 148 and 149 of the ECMA spec (ECMA 262) at http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

Friday, June 18, 2021
 
elias
answered 6 Months ago
82

If your just using the google maps (or any other embed such as youtube, soundcloud etc.) IFRAME embed you can make it responsive by simply adding this to your CSS.

iframe, object, embed{max-width: 100%;}

If your container/theme is responsive this will work great. Essentially this will work with ANY iframe and not just limited to google maps. But keep in mind if you have other iframes on your site they will as well become responsive.

Sunday, August 8, 2021
 
michele
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share