split an empty string gives an element
why split
on an empty string gives an element?
i.e. how to get 0
in the below case:
let tags = "";
let arr = tags.split(',');
console.log(arr.length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
jquery split
|
show 13 more comments
why split
on an empty string gives an element?
i.e. how to get 0
in the below case:
let tags = "";
let arr = tags.split(',');
console.log(arr.length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
jquery split
1
it's in the function specification to return an array with the whole string if the separator is not found. You can still try to find if it exists first withindexOf
and split only if it is found
– Kaddath
Jan 2 at 11:30
You need to check for empty elements if you want to do that
– Just code
Jan 2 at 11:30
@Kaddah and what logic is behind? Why result is not an empty array?
– qadenza
Jan 2 at 11:33
Tryconsole.log(arr);
and then you know why. It returns1
, because of0: ""
.
– Reza Saadati
Jan 2 at 11:35
1
sometimes precision in the terms, calling things by their names can make think that we are leaving common sense. But common sense is often generalisation. Think about what is really what. An empty string is not nothing. It's a string, it's actually something. And if you don't split a cake in 2, you still have a cake. A whole yummy cake for your own..
– Kaddath
Jan 2 at 12:35
|
show 13 more comments
why split
on an empty string gives an element?
i.e. how to get 0
in the below case:
let tags = "";
let arr = tags.split(',');
console.log(arr.length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
jquery split
why split
on an empty string gives an element?
i.e. how to get 0
in the below case:
let tags = "";
let arr = tags.split(',');
console.log(arr.length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
let tags = "";
let arr = tags.split(',');
console.log(arr.length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
let tags = "";
let arr = tags.split(',');
console.log(arr.length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
jquery split
jquery split
asked Jan 2 at 11:27
qadenzaqadenza
2,987103762
2,987103762
1
it's in the function specification to return an array with the whole string if the separator is not found. You can still try to find if it exists first withindexOf
and split only if it is found
– Kaddath
Jan 2 at 11:30
You need to check for empty elements if you want to do that
– Just code
Jan 2 at 11:30
@Kaddah and what logic is behind? Why result is not an empty array?
– qadenza
Jan 2 at 11:33
Tryconsole.log(arr);
and then you know why. It returns1
, because of0: ""
.
– Reza Saadati
Jan 2 at 11:35
1
sometimes precision in the terms, calling things by their names can make think that we are leaving common sense. But common sense is often generalisation. Think about what is really what. An empty string is not nothing. It's a string, it's actually something. And if you don't split a cake in 2, you still have a cake. A whole yummy cake for your own..
– Kaddath
Jan 2 at 12:35
|
show 13 more comments
1
it's in the function specification to return an array with the whole string if the separator is not found. You can still try to find if it exists first withindexOf
and split only if it is found
– Kaddath
Jan 2 at 11:30
You need to check for empty elements if you want to do that
– Just code
Jan 2 at 11:30
@Kaddah and what logic is behind? Why result is not an empty array?
– qadenza
Jan 2 at 11:33
Tryconsole.log(arr);
and then you know why. It returns1
, because of0: ""
.
– Reza Saadati
Jan 2 at 11:35
1
sometimes precision in the terms, calling things by their names can make think that we are leaving common sense. But common sense is often generalisation. Think about what is really what. An empty string is not nothing. It's a string, it's actually something. And if you don't split a cake in 2, you still have a cake. A whole yummy cake for your own..
– Kaddath
Jan 2 at 12:35
1
1
it's in the function specification to return an array with the whole string if the separator is not found. You can still try to find if it exists first with
indexOf
and split only if it is found– Kaddath
Jan 2 at 11:30
it's in the function specification to return an array with the whole string if the separator is not found. You can still try to find if it exists first with
indexOf
and split only if it is found– Kaddath
Jan 2 at 11:30
You need to check for empty elements if you want to do that
– Just code
Jan 2 at 11:30
You need to check for empty elements if you want to do that
– Just code
Jan 2 at 11:30
@Kaddah and what logic is behind? Why result is not an empty array?
– qadenza
Jan 2 at 11:33
@Kaddah and what logic is behind? Why result is not an empty array?
– qadenza
Jan 2 at 11:33
Try
console.log(arr);
and then you know why. It returns 1
, because of 0: ""
.– Reza Saadati
Jan 2 at 11:35
Try
console.log(arr);
and then you know why. It returns 1
, because of 0: ""
.– Reza Saadati
Jan 2 at 11:35
1
1
sometimes precision in the terms, calling things by their names can make think that we are leaving common sense. But common sense is often generalisation. Think about what is really what. An empty string is not nothing. It's a string, it's actually something. And if you don't split a cake in 2, you still have a cake. A whole yummy cake for your own..
– Kaddath
Jan 2 at 12:35
sometimes precision in the terms, calling things by their names can make think that we are leaving common sense. But common sense is often generalisation. Think about what is really what. An empty string is not nothing. It's a string, it's actually something. And if you don't split a cake in 2, you still have a cake. A whole yummy cake for your own..
– Kaddath
Jan 2 at 12:35
|
show 13 more comments
3 Answers
3
active
oldest
votes
You can simply remove the empty string
elements form arr
using filter
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
add a comment |
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
else
{
var result=console.log(arr.length);
}
add a comment |
This is just the nature of the split
function as per the ECMAScript specification. Here is the relevant excerpt from the spec:
If the this object is (or converts to) the empty String, the result
depends on whether separator can match the empty String. If it can,
the result array contains no elements. Otherwise, the result array
contains one element, which is the empty String.
A simple workaround for the behavior you're looking for would be something like:
let arr = !tags ? : tags.split(',');
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005511%2fsplit-an-empty-string-gives-an-element%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can simply remove the empty string
elements form arr
using filter
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
add a comment |
You can simply remove the empty string
elements form arr
using filter
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
add a comment |
You can simply remove the empty string
elements form arr
using filter
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
You can simply remove the empty string
elements form arr
using filter
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
answered Jan 2 at 11:44
Ahsan AliAhsan Ali
2,5401823
2,5401823
add a comment |
add a comment |
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
else
{
var result=console.log(arr.length);
}
add a comment |
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
else
{
var result=console.log(arr.length);
}
add a comment |
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
else
{
var result=console.log(arr.length);
}
let tags = "";
let arr = tags.split(',').filter(x => x);
console.log(arr.length);
else
{
var result=console.log(arr.length);
}
edited Jan 2 at 11:46
answered Jan 2 at 11:41
joslin selva cjoslin selva c
113
113
add a comment |
add a comment |
This is just the nature of the split
function as per the ECMAScript specification. Here is the relevant excerpt from the spec:
If the this object is (or converts to) the empty String, the result
depends on whether separator can match the empty String. If it can,
the result array contains no elements. Otherwise, the result array
contains one element, which is the empty String.
A simple workaround for the behavior you're looking for would be something like:
let arr = !tags ? : tags.split(',');
add a comment |
This is just the nature of the split
function as per the ECMAScript specification. Here is the relevant excerpt from the spec:
If the this object is (or converts to) the empty String, the result
depends on whether separator can match the empty String. If it can,
the result array contains no elements. Otherwise, the result array
contains one element, which is the empty String.
A simple workaround for the behavior you're looking for would be something like:
let arr = !tags ? : tags.split(',');
add a comment |
This is just the nature of the split
function as per the ECMAScript specification. Here is the relevant excerpt from the spec:
If the this object is (or converts to) the empty String, the result
depends on whether separator can match the empty String. If it can,
the result array contains no elements. Otherwise, the result array
contains one element, which is the empty String.
A simple workaround for the behavior you're looking for would be something like:
let arr = !tags ? : tags.split(',');
This is just the nature of the split
function as per the ECMAScript specification. Here is the relevant excerpt from the spec:
If the this object is (or converts to) the empty String, the result
depends on whether separator can match the empty String. If it can,
the result array contains no elements. Otherwise, the result array
contains one element, which is the empty String.
A simple workaround for the behavior you're looking for would be something like:
let arr = !tags ? : tags.split(',');
edited Jan 2 at 11:46
answered Jan 2 at 11:38
codycody
8,23631226
8,23631226
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005511%2fsplit-an-empty-string-gives-an-element%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
it's in the function specification to return an array with the whole string if the separator is not found. You can still try to find if it exists first with
indexOf
and split only if it is found– Kaddath
Jan 2 at 11:30
You need to check for empty elements if you want to do that
– Just code
Jan 2 at 11:30
@Kaddah and what logic is behind? Why result is not an empty array?
– qadenza
Jan 2 at 11:33
Try
console.log(arr);
and then you know why. It returns1
, because of0: ""
.– Reza Saadati
Jan 2 at 11:35
1
sometimes precision in the terms, calling things by their names can make think that we are leaving common sense. But common sense is often generalisation. Think about what is really what. An empty string is not nothing. It's a string, it's actually something. And if you don't split a cake in 2, you still have a cake. A whole yummy cake for your own..
– Kaddath
Jan 2 at 12:35