split an empty string gives an element












-1















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>












share|improve this question


















  • 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 returns 1, because of 0: "".

    – 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















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>












share|improve this question


















  • 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 returns 1, because of 0: "".

    – 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








-1








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>












share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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 returns 1, because of 0: "".

    – 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





    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 returns 1, because of 0: "".

    – 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














3 Answers
3






active

oldest

votes


















1














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);








share|improve this answer































    1














       let tags = "";
    let arr = tags.split(',').filter(x => x);
    console.log(arr.length);
    else
    {

    var result=console.log(arr.length);
    }





    share|improve this answer

































      1














      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(',');





      share|improve this answer

























        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        1














        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);








        share|improve this answer




























          1














          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);








          share|improve this answer


























            1












            1








            1







            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);








            share|improve this answer













            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);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 11:44









            Ahsan AliAhsan Ali

            2,5401823




            2,5401823

























                1














                   let tags = "";
                let arr = tags.split(',').filter(x => x);
                console.log(arr.length);
                else
                {

                var result=console.log(arr.length);
                }





                share|improve this answer






























                  1














                     let tags = "";
                  let arr = tags.split(',').filter(x => x);
                  console.log(arr.length);
                  else
                  {

                  var result=console.log(arr.length);
                  }





                  share|improve this answer




























                    1












                    1








                    1







                       let tags = "";
                    let arr = tags.split(',').filter(x => x);
                    console.log(arr.length);
                    else
                    {

                    var result=console.log(arr.length);
                    }





                    share|improve this answer















                       let tags = "";
                    let arr = tags.split(',').filter(x => x);
                    console.log(arr.length);
                    else
                    {

                    var result=console.log(arr.length);
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 2 at 11:46

























                    answered Jan 2 at 11:41









                    joslin selva cjoslin selva c

                    113




                    113























                        1














                        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(',');





                        share|improve this answer






























                          1














                          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(',');





                          share|improve this answer




























                            1












                            1








                            1







                            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(',');





                            share|improve this answer















                            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(',');






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 2 at 11:46

























                            answered Jan 2 at 11:38









                            codycody

                            8,23631226




                            8,23631226






























                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                'app-layout' is not a known element: how to share Component with different Modules

                                android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                WPF add header to Image with URL pettitions [duplicate]