Explode String into Array With No Empty Elements

Explode string into array with no empty elements?

Try preg_split.

$exploded = preg_split('@/@', '1/2//3/', -1, PREG_SPLIT_NO_EMPTY);

How to split string without getting empty values to the output array

You can use match with regex.

str.match(/[^\/]+/g);

The regex [^\/]+ will match any character that is not forward slash.





function getValues(str) {

return str.match(/[^\/]+/g);

}


document.write('<pre>');

document.write('<b>/value/1/</b><br />' + JSON.stringify(getValues('/value/1/'), 0, 4));

document.write('<br /><br /><b>/value/1</b><br />' + JSON.stringify(getValues('/value/1'), 0, 4));

document.write('<br /><br /><b>/value/</b><br />' + JSON.stringify(getValues('/value/'), 0, 4));

document.write('<br /><br /><b>/value</b><br />' + JSON.stringify(getValues('/value'), 0, 4));

document.write('</pre>');

How to split String without leaving behind empty strings?

Add the "one or more times" greediness quantifier to your character class:

String[] inputTokens = input.split("[(),\\s]+");

This will result in one leading empty String, which is unavoidable when using the split() method and splitting away the immediate start of the String and otherwise no empty Strings.

How can I remove all empty values when I explode a string using PHP?

E.g. via array_filter() or by using the PREG_SPLIT_NO_EMPTY option on preg_split()

<?php
// only for testing purposes ...
$_POST['tag'] = ",jay,john,,,bill,glenn,,0,,";

echo "--- version 1: array_filter ----\n";
// note that this also filters "0" out, since (bool)"0" is FALSE in php
// array_filter() called with only one parameter tests each element as a boolean value
// see http://docs.php.net/language.types.type-juggling
$tags = array_filter( explode(",", $_POST['tag']) );
var_dump($tags);

echo "--- version 2: array_filter/strlen ----\n";
// this one keeps the "0" element
// array_filter() calls strlen() for each element of the array and tests the result as a boolean value
$tags = array_filter( explode(",", $_POST['tag']), 'strlen' );
var_dump($tags);

echo "--- version 3: PREG_SPLIT_NO_EMPTY ----\n";
$tags = preg_split('/,/', $_POST['tag'], -1, PREG_SPLIT_NO_EMPTY);
var_dump($tags);

prints

--- version 1: array_filter ----
array(4) {
[1]=>
string(3) "jay"
[2]=>
string(4) "john"
[5]=>
string(4) "bill"
[6]=>
string(5) "glenn"
}
--- version 2: array_filter/strlen ----
array(5) {
[1]=>
string(3) "jay"
[2]=>
string(4) "john"
[5]=>
string(4) "bill"
[6]=>
string(5) "glenn"
[8]=>
string(1) "0"
}
--- version 3: PREG_SPLIT_NO_EMPTY ----
array(5) {
[0]=>
string(3) "jay"
[1]=>
string(4) "john"
[2]=>
string(4) "bill"
[3]=>
string(5) "glenn"
[4]=>
string(1) "0"
}

Splitting string by whitespace, without empty elements?

You could simply match all non-space character sequences:

str.match(/[^ ]+/g)

Java String split removed empty values

split(delimiter) by default removes trailing empty strings from result array. To turn this mechanism off we need to use overloaded version of split(delimiter, limit) with limit set to negative value like

String[] split = data.split("\\|", -1);

Little more details:

split(regex) internally returns result of split(regex, 0) and in documentation of this method you can find (emphasis mine)

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.

If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Exception:

It is worth mentioning that removing trailing empty string makes sense only if such empty strings were created by the split mechanism. So for "".split(anything) since we can't split "" farther we will get as result [""] array.

It happens because split didn't happen here, so "" despite being empty and trailing represents original string, not empty string which was created by splitting process.

String split returns an array with more elements than expected (empty elements)

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

bash - split string into array WITH empty values

No field separator can be longer than 1 character, unfortunately, so '::' → ':'.

Aside of that, globbing should be explicitly turned off to prevent potential filename expansion in an unquoted variable.

set -f # disable globbing
pattern=":a:b c:"
oldIFS=$IFS
IFS=":"
extractees=($pattern)
IFS=$oldIFS

echo "'${extractees[0]}'"
echo "'${extractees[1]}'"
echo "'${extractees[2]}'"
echo "'${extractees[3]}'"


Related Topics



Leave a reply



Submit