how to make multiple line string to single line string?












9















I have below string



String str="select * from m_menus;

select * from m_roles";


I want above string in one line
like



String str="select * from m_menus;select * from m_roles";


I have tried



str1=str.replace("[rn]+", " "); 


and also



str1=str.replace("n"," "); 


Both are not working.










share|improve this question




















  • 2





    Stupid question.. but why don't you just do this in the first place?: String str="select * from m_menus;select * from m_roles"; Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.

    – forsvarir
    Jun 27 '12 at 7:43













  • @forsvarir Probably he is getting string in that manner from user input?

    – Mukund Samant
    Jun 27 '12 at 7:44











  • I am taking string as input from user

    – happy
    Jun 27 '12 at 7:53
















9















I have below string



String str="select * from m_menus;

select * from m_roles";


I want above string in one line
like



String str="select * from m_menus;select * from m_roles";


I have tried



str1=str.replace("[rn]+", " "); 


and also



str1=str.replace("n"," "); 


Both are not working.










share|improve this question




















  • 2





    Stupid question.. but why don't you just do this in the first place?: String str="select * from m_menus;select * from m_roles"; Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.

    – forsvarir
    Jun 27 '12 at 7:43













  • @forsvarir Probably he is getting string in that manner from user input?

    – Mukund Samant
    Jun 27 '12 at 7:44











  • I am taking string as input from user

    – happy
    Jun 27 '12 at 7:53














9












9








9








I have below string



String str="select * from m_menus;

select * from m_roles";


I want above string in one line
like



String str="select * from m_menus;select * from m_roles";


I have tried



str1=str.replace("[rn]+", " "); 


and also



str1=str.replace("n"," "); 


Both are not working.










share|improve this question
















I have below string



String str="select * from m_menus;

select * from m_roles";


I want above string in one line
like



String str="select * from m_menus;select * from m_roles";


I have tried



str1=str.replace("[rn]+", " "); 


and also



str1=str.replace("n"," "); 


Both are not working.







java java-11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 6:05









Niraj Sonawane

2,42121943




2,42121943










asked Jun 27 '12 at 7:39









happyhappy

912154279




912154279








  • 2





    Stupid question.. but why don't you just do this in the first place?: String str="select * from m_menus;select * from m_roles"; Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.

    – forsvarir
    Jun 27 '12 at 7:43













  • @forsvarir Probably he is getting string in that manner from user input?

    – Mukund Samant
    Jun 27 '12 at 7:44











  • I am taking string as input from user

    – happy
    Jun 27 '12 at 7:53














  • 2





    Stupid question.. but why don't you just do this in the first place?: String str="select * from m_menus;select * from m_roles"; Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.

    – forsvarir
    Jun 27 '12 at 7:43













  • @forsvarir Probably he is getting string in that manner from user input?

    – Mukund Samant
    Jun 27 '12 at 7:44











  • I am taking string as input from user

    – happy
    Jun 27 '12 at 7:53








2




2





Stupid question.. but why don't you just do this in the first place?: String str="select * from m_menus;select * from m_roles"; Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.

– forsvarir
Jun 27 '12 at 7:43







Stupid question.. but why don't you just do this in the first place?: String str="select * from m_menus;select * from m_roles"; Is it really the string that's the problem or are you passing it to a SQL engine and getting results you're not happy with.

– forsvarir
Jun 27 '12 at 7:43















@forsvarir Probably he is getting string in that manner from user input?

– Mukund Samant
Jun 27 '12 at 7:44





@forsvarir Probably he is getting string in that manner from user input?

– Mukund Samant
Jun 27 '12 at 7:44













I am taking string as input from user

– happy
Jun 27 '12 at 7:53





I am taking string as input from user

– happy
Jun 27 '12 at 7:53












6 Answers
6






active

oldest

votes


















6














If you want to use regex, you should use the String.replaceAll() method.






share|improve this answer



















  • 2





    Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.

    – TZHX
    Jun 27 '12 at 7:47



















17














Use String.replaceAll instead.



str1=str.replaceAll("[rn]+", " ");





share|improve this answer































    3














    Why don't you use str.replaceAll("rn", " ") ?



    Should work and replace all occurences.






    share|improve this answer































      3














      No regular expressions and operating system independent:



      str1.replaceAll(System.lineSeparator(), " ");


      Windows uses rn as a line breaker, while *nix systems use only n.






      share|improve this answer































        0














        Using Java 11 lines()



        Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string



        yourMultilineString.lines().collect(Collectors.joining(";"));


        Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.






        share|improve this answer































          -1














          str1=str.replaceAll("[rn]+", " ");





          share|improve this answer


























          • try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer

            – Agilanbu
            Jan 2 at 6:37











          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%2f11221491%2fhow-to-make-multiple-line-string-to-single-line-string%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









          6














          If you want to use regex, you should use the String.replaceAll() method.






          share|improve this answer



















          • 2





            Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.

            – TZHX
            Jun 27 '12 at 7:47
















          6














          If you want to use regex, you should use the String.replaceAll() method.






          share|improve this answer



















          • 2





            Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.

            – TZHX
            Jun 27 '12 at 7:47














          6












          6








          6







          If you want to use regex, you should use the String.replaceAll() method.






          share|improve this answer













          If you want to use regex, you should use the String.replaceAll() method.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 27 '12 at 7:44









          TZHXTZHX

          3,814103850




          3,814103850








          • 2





            Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.

            – TZHX
            Jun 27 '12 at 7:47














          • 2





            Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.

            – TZHX
            Jun 27 '12 at 7:47








          2




          2





          Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.

          – TZHX
          Jun 27 '12 at 7:47





          Doesn't it? He's using what looks like a regex expression in a method that makes no claim to understand such.

          – TZHX
          Jun 27 '12 at 7:47













          17














          Use String.replaceAll instead.



          str1=str.replaceAll("[rn]+", " ");





          share|improve this answer




























            17














            Use String.replaceAll instead.



            str1=str.replaceAll("[rn]+", " ");





            share|improve this answer


























              17












              17








              17







              Use String.replaceAll instead.



              str1=str.replaceAll("[rn]+", " ");





              share|improve this answer













              Use String.replaceAll instead.



              str1=str.replaceAll("[rn]+", " ");






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 27 '12 at 7:44









              Mattias BuelensMattias Buelens

              15.4k42946




              15.4k42946























                  3














                  Why don't you use str.replaceAll("rn", " ") ?



                  Should work and replace all occurences.






                  share|improve this answer




























                    3














                    Why don't you use str.replaceAll("rn", " ") ?



                    Should work and replace all occurences.






                    share|improve this answer


























                      3












                      3








                      3







                      Why don't you use str.replaceAll("rn", " ") ?



                      Should work and replace all occurences.






                      share|improve this answer













                      Why don't you use str.replaceAll("rn", " ") ?



                      Should work and replace all occurences.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 27 '12 at 7:44









                      Michael LaffargueMichael Laffargue

                      8,63843264




                      8,63843264























                          3














                          No regular expressions and operating system independent:



                          str1.replaceAll(System.lineSeparator(), " ");


                          Windows uses rn as a line breaker, while *nix systems use only n.






                          share|improve this answer




























                            3














                            No regular expressions and operating system independent:



                            str1.replaceAll(System.lineSeparator(), " ");


                            Windows uses rn as a line breaker, while *nix systems use only n.






                            share|improve this answer


























                              3












                              3








                              3







                              No regular expressions and operating system independent:



                              str1.replaceAll(System.lineSeparator(), " ");


                              Windows uses rn as a line breaker, while *nix systems use only n.






                              share|improve this answer













                              No regular expressions and operating system independent:



                              str1.replaceAll(System.lineSeparator(), " ");


                              Windows uses rn as a line breaker, while *nix systems use only n.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 14 '18 at 7:50









                              dfinkidfinki

                              197111




                              197111























                                  0














                                  Using Java 11 lines()



                                  Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string



                                  yourMultilineString.lines().collect(Collectors.joining(";"));


                                  Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.






                                  share|improve this answer




























                                    0














                                    Using Java 11 lines()



                                    Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string



                                    yourMultilineString.lines().collect(Collectors.joining(";"));


                                    Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Using Java 11 lines()



                                      Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string



                                      yourMultilineString.lines().collect(Collectors.joining(";"));


                                      Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.






                                      share|improve this answer













                                      Using Java 11 lines()



                                      Java 11 has added new method String::lines, lines method uses specialized Spliterator to lazily provide lines from the source string



                                      yourMultilineString.lines().collect(Collectors.joining(";"));


                                      Note The string uses Windows’ rn and even though I’m on Linux, lines() still splits it. That’s because regardless of the operating system, the method treats r, n, and rn as line terminators and splits there – even if they are mixed in the same string.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 2 at 6:04









                                      Niraj SonawaneNiraj Sonawane

                                      2,42121943




                                      2,42121943























                                          -1














                                          str1=str.replaceAll("[rn]+", " ");





                                          share|improve this answer


























                                          • try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer

                                            – Agilanbu
                                            Jan 2 at 6:37
















                                          -1














                                          str1=str.replaceAll("[rn]+", " ");





                                          share|improve this answer


























                                          • try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer

                                            – Agilanbu
                                            Jan 2 at 6:37














                                          -1












                                          -1








                                          -1







                                          str1=str.replaceAll("[rn]+", " ");





                                          share|improve this answer















                                          str1=str.replaceAll("[rn]+", " ");






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jan 2 at 6:40









                                          iOS

                                          2,79722048




                                          2,79722048










                                          answered Jan 2 at 6:15









                                          joslin selva cjoslin selva c

                                          113




                                          113













                                          • try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer

                                            – Agilanbu
                                            Jan 2 at 6:37



















                                          • try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer

                                            – Agilanbu
                                            Jan 2 at 6:37

















                                          try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer

                                          – Agilanbu
                                          Jan 2 at 6:37





                                          try to highlight the keywords and be clear with the format it will help to reach out your answer for others and do write and explain your answer

                                          – Agilanbu
                                          Jan 2 at 6:37


















                                          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%2f11221491%2fhow-to-make-multiple-line-string-to-single-line-string%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

                                          Mossoró

                                          Error while reading .h5 file using the rhdf5 package in R

                                          Pushsharp Apns notification error: 'InvalidToken'