How to convert a list of digits to a string in Mathematica

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite












I want to convert a list of digits to a string, for example from 1,2,3,4,5, I want to get the string 12345.



The following will work as long as the list does not begin with 0's:



1, 2, 3, 4, 5 // FromDigits // ToString 
Out[10]:= 12345


However if the list begins with 0's, as in



0, 0, 1, 2, 3, 4 // FromDigits // ToString
Out[11]:= 1234


this method chops off the 0's in the front.



I could write a program that tests to see if the list begins with a string of 0's and then append that many zeros to the chopped off output, but I'd like a less procedural way of getting the string. Is this possible?



Note: the lists I want to convert are going to come from some prior computation, so unlike the above examples, I won't know their contents unless I explicitely examine them.







share|improve this question






















  • Try StringRiffle[0, 0, 1, 2, 3, 4, ""] introduced in version 10.1
    – Hans
    Sep 2 at 1:47














up vote
4
down vote

favorite












I want to convert a list of digits to a string, for example from 1,2,3,4,5, I want to get the string 12345.



The following will work as long as the list does not begin with 0's:



1, 2, 3, 4, 5 // FromDigits // ToString 
Out[10]:= 12345


However if the list begins with 0's, as in



0, 0, 1, 2, 3, 4 // FromDigits // ToString
Out[11]:= 1234


this method chops off the 0's in the front.



I could write a program that tests to see if the list begins with a string of 0's and then append that many zeros to the chopped off output, but I'd like a less procedural way of getting the string. Is this possible?



Note: the lists I want to convert are going to come from some prior computation, so unlike the above examples, I won't know their contents unless I explicitely examine them.







share|improve this question






















  • Try StringRiffle[0, 0, 1, 2, 3, 4, ""] introduced in version 10.1
    – Hans
    Sep 2 at 1:47












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I want to convert a list of digits to a string, for example from 1,2,3,4,5, I want to get the string 12345.



The following will work as long as the list does not begin with 0's:



1, 2, 3, 4, 5 // FromDigits // ToString 
Out[10]:= 12345


However if the list begins with 0's, as in



0, 0, 1, 2, 3, 4 // FromDigits // ToString
Out[11]:= 1234


this method chops off the 0's in the front.



I could write a program that tests to see if the list begins with a string of 0's and then append that many zeros to the chopped off output, but I'd like a less procedural way of getting the string. Is this possible?



Note: the lists I want to convert are going to come from some prior computation, so unlike the above examples, I won't know their contents unless I explicitely examine them.







share|improve this question














I want to convert a list of digits to a string, for example from 1,2,3,4,5, I want to get the string 12345.



The following will work as long as the list does not begin with 0's:



1, 2, 3, 4, 5 // FromDigits // ToString 
Out[10]:= 12345


However if the list begins with 0's, as in



0, 0, 1, 2, 3, 4 // FromDigits // ToString
Out[11]:= 1234


this method chops off the 0's in the front.



I could write a program that tests to see if the list begins with a string of 0's and then append that many zeros to the chopped off output, but I'd like a less procedural way of getting the string. Is this possible?



Note: the lists I want to convert are going to come from some prior computation, so unlike the above examples, I won't know their contents unless I explicitely examine them.









share|improve this question













share|improve this question




share|improve this question








edited Sep 2 at 1:33









m0nhawk

2,47811431




2,47811431










asked Sep 2 at 0:04









hagbard7000

262




262











  • Try StringRiffle[0, 0, 1, 2, 3, 4, ""] introduced in version 10.1
    – Hans
    Sep 2 at 1:47
















  • Try StringRiffle[0, 0, 1, 2, 3, 4, ""] introduced in version 10.1
    – Hans
    Sep 2 at 1:47















Try StringRiffle[0, 0, 1, 2, 3, 4, ""] introduced in version 10.1
– Hans
Sep 2 at 1:47




Try StringRiffle[0, 0, 1, 2, 3, 4, ""] introduced in version 10.1
– Hans
Sep 2 at 1:47










4 Answers
4






active

oldest

votes

















up vote
9
down vote













Try



StringRiffle[0, 0, 1, 2, 3, 4, ""]
"001234"


Introduced in version 10.1 or in postfix as follows:



0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &





share|improve this answer



























    up vote
    7
    down vote













    While StringRiffle is nice and concise, but it has -- similar to many of the String-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString and StringJoin is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0 to 9 appear in the list, you can also use FromCharacterCode which is faster by almost two further orders of magnitude.



    a = RandomInteger[0, 9, 100000];

    s1 = StringRiffle[a, ""]; // RepeatedTiming // First
    s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
    s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
    s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
    s1 == s2 == s3 == s4



    0.311



    0.0649



    0.036



    0.000534



    True







    share|improve this answer





























      up vote
      4
      down vote













      It might be more convenient to convert your list to a string first, then remove unnecessary symbols.



      0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &



      "001234"







      share|improve this answer





























        up vote
        4
        down vote













        It can also be done in this way



        StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]





        share|improve this answer




















        • This feels like the most readable and syntactically correct to me.
          – Emilio Pisanty
          Sep 2 at 10:46










        • @EmilioPisanty Thx.
          – Î‘λέξανδρος Ζεγγ
          Sep 2 at 10:56










        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "387"
        ;
        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',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181078%2fhow-to-convert-a-list-of-digits-to-a-string-in-mathematica%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        9
        down vote













        Try



        StringRiffle[0, 0, 1, 2, 3, 4, ""]
        "001234"


        Introduced in version 10.1 or in postfix as follows:



        0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &





        share|improve this answer
























          up vote
          9
          down vote













          Try



          StringRiffle[0, 0, 1, 2, 3, 4, ""]
          "001234"


          Introduced in version 10.1 or in postfix as follows:



          0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &





          share|improve this answer






















            up vote
            9
            down vote










            up vote
            9
            down vote









            Try



            StringRiffle[0, 0, 1, 2, 3, 4, ""]
            "001234"


            Introduced in version 10.1 or in postfix as follows:



            0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &





            share|improve this answer












            Try



            StringRiffle[0, 0, 1, 2, 3, 4, ""]
            "001234"


            Introduced in version 10.1 or in postfix as follows:



            0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 2 at 2:01









            Hans

            76146




            76146




















                up vote
                7
                down vote













                While StringRiffle is nice and concise, but it has -- similar to many of the String-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString and StringJoin is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0 to 9 appear in the list, you can also use FromCharacterCode which is faster by almost two further orders of magnitude.



                a = RandomInteger[0, 9, 100000];

                s1 = StringRiffle[a, ""]; // RepeatedTiming // First
                s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
                s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
                s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
                s1 == s2 == s3 == s4



                0.311



                0.0649



                0.036



                0.000534



                True







                share|improve this answer


























                  up vote
                  7
                  down vote













                  While StringRiffle is nice and concise, but it has -- similar to many of the String-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString and StringJoin is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0 to 9 appear in the list, you can also use FromCharacterCode which is faster by almost two further orders of magnitude.



                  a = RandomInteger[0, 9, 100000];

                  s1 = StringRiffle[a, ""]; // RepeatedTiming // First
                  s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
                  s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
                  s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
                  s1 == s2 == s3 == s4



                  0.311



                  0.0649



                  0.036



                  0.000534



                  True







                  share|improve this answer
























                    up vote
                    7
                    down vote










                    up vote
                    7
                    down vote









                    While StringRiffle is nice and concise, but it has -- similar to many of the String-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString and StringJoin is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0 to 9 appear in the list, you can also use FromCharacterCode which is faster by almost two further orders of magnitude.



                    a = RandomInteger[0, 9, 100000];

                    s1 = StringRiffle[a, ""]; // RepeatedTiming // First
                    s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
                    s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
                    s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
                    s1 == s2 == s3 == s4



                    0.311



                    0.0649



                    0.036



                    0.000534



                    True







                    share|improve this answer














                    While StringRiffle is nice and concise, but it has -- similar to many of the String-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString and StringJoin is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0 to 9 appear in the list, you can also use FromCharacterCode which is faster by almost two further orders of magnitude.



                    a = RandomInteger[0, 9, 100000];

                    s1 = StringRiffle[a, ""]; // RepeatedTiming // First
                    s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
                    s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
                    s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
                    s1 == s2 == s3 == s4



                    0.311



                    0.0649



                    0.036



                    0.000534



                    True








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 2 at 22:08

























                    answered Sep 2 at 9:31









                    Henrik Schumacher

                    36.9k249105




                    36.9k249105




















                        up vote
                        4
                        down vote













                        It might be more convenient to convert your list to a string first, then remove unnecessary symbols.



                        0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &



                        "001234"







                        share|improve this answer


























                          up vote
                          4
                          down vote













                          It might be more convenient to convert your list to a string first, then remove unnecessary symbols.



                          0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &



                          "001234"







                          share|improve this answer
























                            up vote
                            4
                            down vote










                            up vote
                            4
                            down vote









                            It might be more convenient to convert your list to a string first, then remove unnecessary symbols.



                            0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &



                            "001234"







                            share|improve this answer














                            It might be more convenient to convert your list to a string first, then remove unnecessary symbols.



                            0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &



                            "001234"








                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Sep 2 at 9:31









                            Henrik Schumacher

                            36.9k249105




                            36.9k249105










                            answered Sep 2 at 0:25









                            roman465

                            739313




                            739313




















                                up vote
                                4
                                down vote













                                It can also be done in this way



                                StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]





                                share|improve this answer




















                                • This feels like the most readable and syntactically correct to me.
                                  – Emilio Pisanty
                                  Sep 2 at 10:46










                                • @EmilioPisanty Thx.
                                  – Î‘λέξανδρος Ζεγγ
                                  Sep 2 at 10:56














                                up vote
                                4
                                down vote













                                It can also be done in this way



                                StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]





                                share|improve this answer




















                                • This feels like the most readable and syntactically correct to me.
                                  – Emilio Pisanty
                                  Sep 2 at 10:46










                                • @EmilioPisanty Thx.
                                  – Î‘λέξανδρος Ζεγγ
                                  Sep 2 at 10:56












                                up vote
                                4
                                down vote










                                up vote
                                4
                                down vote









                                It can also be done in this way



                                StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]





                                share|improve this answer












                                It can also be done in this way



                                StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Sep 2 at 9:58









                                Αλέξανδρος Ζεγγ

                                1,523720




                                1,523720











                                • This feels like the most readable and syntactically correct to me.
                                  – Emilio Pisanty
                                  Sep 2 at 10:46










                                • @EmilioPisanty Thx.
                                  – Î‘λέξανδρος Ζεγγ
                                  Sep 2 at 10:56
















                                • This feels like the most readable and syntactically correct to me.
                                  – Emilio Pisanty
                                  Sep 2 at 10:46










                                • @EmilioPisanty Thx.
                                  – Î‘λέξανδρος Ζεγγ
                                  Sep 2 at 10:56















                                This feels like the most readable and syntactically correct to me.
                                – Emilio Pisanty
                                Sep 2 at 10:46




                                This feels like the most readable and syntactically correct to me.
                                – Emilio Pisanty
                                Sep 2 at 10:46












                                @EmilioPisanty Thx.
                                – Î‘λέξανδρος Ζεγγ
                                Sep 2 at 10:56




                                @EmilioPisanty Thx.
                                – Î‘λέξανδρος Ζεγγ
                                Sep 2 at 10:56

















                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181078%2fhow-to-convert-a-list-of-digits-to-a-string-in-mathematica%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                One-line joke