how to convert an array to a string after split function












0















I'm trying to convert the return value of the .split() function into a string.



There is a string of characters that is being parsed from a text file, and I need to change it from an array to a string.



After calling the split function, the result is an array, however, I need it to be a string to perform character operations and functions.



example = "--NAME: John Doe"
print example
value = example.split("--NAME: ")
print value.class
value.to_s.strip
print value
print value.class









share|improve this question




















  • 2





    It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g., split) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".

    – Cary Swoveland
    Jan 2 at 4:48













  • Is "--NAME: " actually a delimiter? To me it looks more like a prefix / label.

    – Stefan
    Jan 2 at 13:26
















0















I'm trying to convert the return value of the .split() function into a string.



There is a string of characters that is being parsed from a text file, and I need to change it from an array to a string.



After calling the split function, the result is an array, however, I need it to be a string to perform character operations and functions.



example = "--NAME: John Doe"
print example
value = example.split("--NAME: ")
print value.class
value.to_s.strip
print value
print value.class









share|improve this question




















  • 2





    It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g., split) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".

    – Cary Swoveland
    Jan 2 at 4:48













  • Is "--NAME: " actually a delimiter? To me it looks more like a prefix / label.

    – Stefan
    Jan 2 at 13:26














0












0








0


1






I'm trying to convert the return value of the .split() function into a string.



There is a string of characters that is being parsed from a text file, and I need to change it from an array to a string.



After calling the split function, the result is an array, however, I need it to be a string to perform character operations and functions.



example = "--NAME: John Doe"
print example
value = example.split("--NAME: ")
print value.class
value.to_s.strip
print value
print value.class









share|improve this question
















I'm trying to convert the return value of the .split() function into a string.



There is a string of characters that is being parsed from a text file, and I need to change it from an array to a string.



After calling the split function, the result is an array, however, I need it to be a string to perform character operations and functions.



example = "--NAME: John Doe"
print example
value = example.split("--NAME: ")
print value.class
value.to_s.strip
print value
print value.class






ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 6:00









Lahiru

1,35342527




1,35342527










asked Jan 2 at 1:35









philly_mcvphilly_mcv

41




41








  • 2





    It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g., split) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".

    – Cary Swoveland
    Jan 2 at 4:48













  • Is "--NAME: " actually a delimiter? To me it looks more like a prefix / label.

    – Stefan
    Jan 2 at 13:26














  • 2





    It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g., split) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".

    – Cary Swoveland
    Jan 2 at 4:48













  • Is "--NAME: " actually a delimiter? To me it looks more like a prefix / label.

    – Stefan
    Jan 2 at 13:26








2




2





It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g., split) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".

– Cary Swoveland
Jan 2 at 4:48







It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g., split) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".

– Cary Swoveland
Jan 2 at 4:48















Is "--NAME: " actually a delimiter? To me it looks more like a prefix / label.

– Stefan
Jan 2 at 13:26





Is "--NAME: " actually a delimiter? To me it looks more like a prefix / label.

– Stefan
Jan 2 at 13:26












6 Answers
6






active

oldest

votes


















1














you can use 'join' to convert the array to string.



With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.



example = "--NAME: John Doe"
value = example.split("--NAME: ")
puts value



["", "John Doe"]




puts value.reject(&:blank?).join(",")



"John Doe"







share|improve this answer































    1














    (?<=--NAME:) Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text



    Strip! to remove leading or trailing spaces in place.



    irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!


    Step by step:



    irb(main):026:0> exampel = "--NAME: John Doe"
    => "--NAME: John Doe"
    irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
    => "John Doe"
    irb(main):028:0> example
    => "John Doe"
    irb(main):029:0>





    share|improve this answer

































      0














      Perhaps split() isn't the right tool for your needs?



      If you just want to remove the prompt you could try gsub() (https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub() stands for "global substitution" and in other languages it is often known as replace().



      example.gsub("--NAME: ", "")



      Will replace the prompt with an empty string and give you:




      "John Doe"




      (a String)






      share|improve this answer































        0














        Simply sub-string your string:



        example = "--NAME: John Doe"

        example[8..-1]

        #=> "John Doe"


        While 8 indicates the start position and -1 indicates that the substring should be ended with the last character of the example string since the length of the NAME is a variable.






        share|improve this answer

































          0














          If you are getting your example string in same formats everytime,



          example.split(' ', 2).last





          share|improve this answer































            0














            > value = example.split("--NAME: ")
            #=> ["", "John Doe"]
            > value.join.strip
            #=> "John Doe"





            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%2f54000302%2fhow-to-convert-an-array-to-a-string-after-split-function%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              you can use 'join' to convert the array to string.



              With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.



              example = "--NAME: John Doe"
              value = example.split("--NAME: ")
              puts value



              ["", "John Doe"]




              puts value.reject(&:blank?).join(",")



              "John Doe"







              share|improve this answer




























                1














                you can use 'join' to convert the array to string.



                With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.



                example = "--NAME: John Doe"
                value = example.split("--NAME: ")
                puts value



                ["", "John Doe"]




                puts value.reject(&:blank?).join(",")



                "John Doe"







                share|improve this answer


























                  1












                  1








                  1







                  you can use 'join' to convert the array to string.



                  With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.



                  example = "--NAME: John Doe"
                  value = example.split("--NAME: ")
                  puts value



                  ["", "John Doe"]




                  puts value.reject(&:blank?).join(",")



                  "John Doe"







                  share|improve this answer













                  you can use 'join' to convert the array to string.



                  With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.



                  example = "--NAME: John Doe"
                  value = example.split("--NAME: ")
                  puts value



                  ["", "John Doe"]




                  puts value.reject(&:blank?).join(",")



                  "John Doe"








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 2:44









                  Saraswathy RenugaSaraswathy Renuga

                  137110




                  137110

























                      1














                      (?<=--NAME:) Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text



                      Strip! to remove leading or trailing spaces in place.



                      irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!


                      Step by step:



                      irb(main):026:0> exampel = "--NAME: John Doe"
                      => "--NAME: John Doe"
                      irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
                      => "John Doe"
                      irb(main):028:0> example
                      => "John Doe"
                      irb(main):029:0>





                      share|improve this answer






























                        1














                        (?<=--NAME:) Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text



                        Strip! to remove leading or trailing spaces in place.



                        irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!


                        Step by step:



                        irb(main):026:0> exampel = "--NAME: John Doe"
                        => "--NAME: John Doe"
                        irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
                        => "John Doe"
                        irb(main):028:0> example
                        => "John Doe"
                        irb(main):029:0>





                        share|improve this answer




























                          1












                          1








                          1







                          (?<=--NAME:) Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text



                          Strip! to remove leading or trailing spaces in place.



                          irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!


                          Step by step:



                          irb(main):026:0> exampel = "--NAME: John Doe"
                          => "--NAME: John Doe"
                          irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
                          => "John Doe"
                          irb(main):028:0> example
                          => "John Doe"
                          irb(main):029:0>





                          share|improve this answer















                          (?<=--NAME:) Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text



                          Strip! to remove leading or trailing spaces in place.



                          irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!


                          Step by step:



                          irb(main):026:0> exampel = "--NAME: John Doe"
                          => "--NAME: John Doe"
                          irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
                          => "John Doe"
                          irb(main):028:0> example
                          => "John Doe"
                          irb(main):029:0>






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 2 at 4:27

























                          answered Jan 2 at 4:21









                          zeezee

                          3,29712630




                          3,29712630























                              0














                              Perhaps split() isn't the right tool for your needs?



                              If you just want to remove the prompt you could try gsub() (https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub() stands for "global substitution" and in other languages it is often known as replace().



                              example.gsub("--NAME: ", "")



                              Will replace the prompt with an empty string and give you:




                              "John Doe"




                              (a String)






                              share|improve this answer




























                                0














                                Perhaps split() isn't the right tool for your needs?



                                If you just want to remove the prompt you could try gsub() (https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub() stands for "global substitution" and in other languages it is often known as replace().



                                example.gsub("--NAME: ", "")



                                Will replace the prompt with an empty string and give you:




                                "John Doe"




                                (a String)






                                share|improve this answer


























                                  0












                                  0








                                  0







                                  Perhaps split() isn't the right tool for your needs?



                                  If you just want to remove the prompt you could try gsub() (https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub() stands for "global substitution" and in other languages it is often known as replace().



                                  example.gsub("--NAME: ", "")



                                  Will replace the prompt with an empty string and give you:




                                  "John Doe"




                                  (a String)






                                  share|improve this answer













                                  Perhaps split() isn't the right tool for your needs?



                                  If you just want to remove the prompt you could try gsub() (https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub() stands for "global substitution" and in other languages it is often known as replace().



                                  example.gsub("--NAME: ", "")



                                  Will replace the prompt with an empty string and give you:




                                  "John Doe"




                                  (a String)







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jan 2 at 3:45









                                  DavidDavid

                                  1,51031428




                                  1,51031428























                                      0














                                      Simply sub-string your string:



                                      example = "--NAME: John Doe"

                                      example[8..-1]

                                      #=> "John Doe"


                                      While 8 indicates the start position and -1 indicates that the substring should be ended with the last character of the example string since the length of the NAME is a variable.






                                      share|improve this answer






























                                        0














                                        Simply sub-string your string:



                                        example = "--NAME: John Doe"

                                        example[8..-1]

                                        #=> "John Doe"


                                        While 8 indicates the start position and -1 indicates that the substring should be ended with the last character of the example string since the length of the NAME is a variable.






                                        share|improve this answer




























                                          0












                                          0








                                          0







                                          Simply sub-string your string:



                                          example = "--NAME: John Doe"

                                          example[8..-1]

                                          #=> "John Doe"


                                          While 8 indicates the start position and -1 indicates that the substring should be ended with the last character of the example string since the length of the NAME is a variable.






                                          share|improve this answer















                                          Simply sub-string your string:



                                          example = "--NAME: John Doe"

                                          example[8..-1]

                                          #=> "John Doe"


                                          While 8 indicates the start position and -1 indicates that the substring should be ended with the last character of the example string since the length of the NAME is a variable.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jan 2 at 4:12

























                                          answered Jan 2 at 4:05









                                          LahiruLahiru

                                          1,35342527




                                          1,35342527























                                              0














                                              If you are getting your example string in same formats everytime,



                                              example.split(' ', 2).last





                                              share|improve this answer




























                                                0














                                                If you are getting your example string in same formats everytime,



                                                example.split(' ', 2).last





                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  If you are getting your example string in same formats everytime,



                                                  example.split(' ', 2).last





                                                  share|improve this answer













                                                  If you are getting your example string in same formats everytime,



                                                  example.split(' ', 2).last






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jan 2 at 5:39









                                                  rayray

                                                  3,3561829




                                                  3,3561829























                                                      0














                                                      > value = example.split("--NAME: ")
                                                      #=> ["", "John Doe"]
                                                      > value.join.strip
                                                      #=> "John Doe"





                                                      share|improve this answer




























                                                        0














                                                        > value = example.split("--NAME: ")
                                                        #=> ["", "John Doe"]
                                                        > value.join.strip
                                                        #=> "John Doe"





                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          > value = example.split("--NAME: ")
                                                          #=> ["", "John Doe"]
                                                          > value.join.strip
                                                          #=> "John Doe"





                                                          share|improve this answer













                                                          > value = example.split("--NAME: ")
                                                          #=> ["", "John Doe"]
                                                          > value.join.strip
                                                          #=> "John Doe"






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jan 2 at 6:04









                                                          Gagan GamiGagan Gami

                                                          8,46911643




                                                          8,46911643






























                                                              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%2f54000302%2fhow-to-convert-an-array-to-a-string-after-split-function%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

                                                              MongoDB - Not Authorized To Execute Command

                                                              How to fix TextFormField cause rebuild widget in Flutter

                                                              Npm cannot find a required file even through it is in the searched directory