Case insensitive String split() method












13















When I perform



String test="23x34 ";
String array=test.split("x"); //splitting using simple letter


I got two items in array as 23 and 34



but when I did



String test="23x34 ";
String array=test.split("X"); //splitting using capitalletter


I got one item in array 23x34



So is there any way I can use the split method as case insensitive or whether there is any other method that can help?










share|improve this question



























    13















    When I perform



    String test="23x34 ";
    String array=test.split("x"); //splitting using simple letter


    I got two items in array as 23 and 34



    but when I did



    String test="23x34 ";
    String array=test.split("X"); //splitting using capitalletter


    I got one item in array 23x34



    So is there any way I can use the split method as case insensitive or whether there is any other method that can help?










    share|improve this question

























      13












      13








      13


      3






      When I perform



      String test="23x34 ";
      String array=test.split("x"); //splitting using simple letter


      I got two items in array as 23 and 34



      but when I did



      String test="23x34 ";
      String array=test.split("X"); //splitting using capitalletter


      I got one item in array 23x34



      So is there any way I can use the split method as case insensitive or whether there is any other method that can help?










      share|improve this question














      When I perform



      String test="23x34 ";
      String array=test.split("x"); //splitting using simple letter


      I got two items in array as 23 and 34



      but when I did



      String test="23x34 ";
      String array=test.split("X"); //splitting using capitalletter


      I got one item in array 23x34



      So is there any way I can use the split method as case insensitive or whether there is any other method that can help?







      java string split






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 16 '13 at 8:00









      Sanjaya LiyanageSanjaya Liyanage

      3,37172648




      3,37172648
























          6 Answers
          6






          active

          oldest

          votes


















          15














          Use regex pattern [xX] in split



          String x = "24X45";
          String res = x.split("[xX]");
          System.out.println(Arrays.toString(res));





          share|improve this answer
























          • thanks it works.

            – Sanjaya Liyanage
            May 16 '13 at 8:18



















          44














          split uses, as the documentation suggests, a regexp. a regexp for your example would be :



          "[xX]"


          Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :



          "(?i)x"


          In this case, x can be any litteral properly escaped.






          share|improve this answer
























          • downvoter please comment.

            – njzk2
            May 16 '13 at 9:35






          • 3





            +1 for (?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]

            – Fernando Leal
            Mar 28 '14 at 18:42






          • 1





            If you have unicode chars you should rather use (?iu) flag instead.

            – NikolaB
            Oct 14 '15 at 12:06






          • 1





            "24x45".split("(?i)X") not working in chrome 67

            – Chan Tzish
            Jun 18 '18 at 23:32








          • 3





            @ChanTzish this question is about Java

            – njzk2
            Jun 19 '18 at 3:06



















          7














          You can also use an embedded flag in your regex:



          String array = test.split("(?i)x"); // splits case insensitive





          share|improve this answer































            4














            I personally prefer using



            String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
            String parts = modified.split(splitterValue);


            In this way you can ensure any regex will work, as long as you have a unique splitter value






            share|improve this answer































              0














              Java's String class' split method also accepts regex.



              To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split






              share|improve this answer



















              • 1





                "also accepts regex" actually only accepts regex.

                – wchargin
                Jun 4 '13 at 5:15











              • @WChargin So true. And I realized it just yesterday.

                – zEro
                Jun 6 '13 at 14:11



















              -1














              You could use a regex as an argument to split, like this:



              "32x23".split("[xX]");


              Or you could use a StringTokenizer that lets you set its set of delimiters, like this:



              StringTokenizer st = new StringTokenizer("32x23","xX");
              // ^^ ^^
              // string delimiter


              This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.






              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%2f16581977%2fcase-insensitive-string-split-method%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









                15














                Use regex pattern [xX] in split



                String x = "24X45";
                String res = x.split("[xX]");
                System.out.println(Arrays.toString(res));





                share|improve this answer
























                • thanks it works.

                  – Sanjaya Liyanage
                  May 16 '13 at 8:18
















                15














                Use regex pattern [xX] in split



                String x = "24X45";
                String res = x.split("[xX]");
                System.out.println(Arrays.toString(res));





                share|improve this answer
























                • thanks it works.

                  – Sanjaya Liyanage
                  May 16 '13 at 8:18














                15












                15








                15







                Use regex pattern [xX] in split



                String x = "24X45";
                String res = x.split("[xX]");
                System.out.println(Arrays.toString(res));





                share|improve this answer













                Use regex pattern [xX] in split



                String x = "24X45";
                String res = x.split("[xX]");
                System.out.println(Arrays.toString(res));






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 16 '13 at 8:01









                harshharsh

                5,96412230




                5,96412230













                • thanks it works.

                  – Sanjaya Liyanage
                  May 16 '13 at 8:18



















                • thanks it works.

                  – Sanjaya Liyanage
                  May 16 '13 at 8:18

















                thanks it works.

                – Sanjaya Liyanage
                May 16 '13 at 8:18





                thanks it works.

                – Sanjaya Liyanage
                May 16 '13 at 8:18













                44














                split uses, as the documentation suggests, a regexp. a regexp for your example would be :



                "[xX]"


                Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :



                "(?i)x"


                In this case, x can be any litteral properly escaped.






                share|improve this answer
























                • downvoter please comment.

                  – njzk2
                  May 16 '13 at 9:35






                • 3





                  +1 for (?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]

                  – Fernando Leal
                  Mar 28 '14 at 18:42






                • 1





                  If you have unicode chars you should rather use (?iu) flag instead.

                  – NikolaB
                  Oct 14 '15 at 12:06






                • 1





                  "24x45".split("(?i)X") not working in chrome 67

                  – Chan Tzish
                  Jun 18 '18 at 23:32








                • 3





                  @ChanTzish this question is about Java

                  – njzk2
                  Jun 19 '18 at 3:06
















                44














                split uses, as the documentation suggests, a regexp. a regexp for your example would be :



                "[xX]"


                Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :



                "(?i)x"


                In this case, x can be any litteral properly escaped.






                share|improve this answer
























                • downvoter please comment.

                  – njzk2
                  May 16 '13 at 9:35






                • 3





                  +1 for (?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]

                  – Fernando Leal
                  Mar 28 '14 at 18:42






                • 1





                  If you have unicode chars you should rather use (?iu) flag instead.

                  – NikolaB
                  Oct 14 '15 at 12:06






                • 1





                  "24x45".split("(?i)X") not working in chrome 67

                  – Chan Tzish
                  Jun 18 '18 at 23:32








                • 3





                  @ChanTzish this question is about Java

                  – njzk2
                  Jun 19 '18 at 3:06














                44












                44








                44







                split uses, as the documentation suggests, a regexp. a regexp for your example would be :



                "[xX]"


                Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :



                "(?i)x"


                In this case, x can be any litteral properly escaped.






                share|improve this answer













                split uses, as the documentation suggests, a regexp. a regexp for your example would be :



                "[xX]"


                Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :



                "(?i)x"


                In this case, x can be any litteral properly escaped.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 16 '13 at 8:07









                njzk2njzk2

                32.8k44991




                32.8k44991













                • downvoter please comment.

                  – njzk2
                  May 16 '13 at 9:35






                • 3





                  +1 for (?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]

                  – Fernando Leal
                  Mar 28 '14 at 18:42






                • 1





                  If you have unicode chars you should rather use (?iu) flag instead.

                  – NikolaB
                  Oct 14 '15 at 12:06






                • 1





                  "24x45".split("(?i)X") not working in chrome 67

                  – Chan Tzish
                  Jun 18 '18 at 23:32








                • 3





                  @ChanTzish this question is about Java

                  – njzk2
                  Jun 19 '18 at 3:06



















                • downvoter please comment.

                  – njzk2
                  May 16 '13 at 9:35






                • 3





                  +1 for (?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]

                  – Fernando Leal
                  Mar 28 '14 at 18:42






                • 1





                  If you have unicode chars you should rather use (?iu) flag instead.

                  – NikolaB
                  Oct 14 '15 at 12:06






                • 1





                  "24x45".split("(?i)X") not working in chrome 67

                  – Chan Tzish
                  Jun 18 '18 at 23:32








                • 3





                  @ChanTzish this question is about Java

                  – njzk2
                  Jun 19 '18 at 3:06

















                downvoter please comment.

                – njzk2
                May 16 '13 at 9:35





                downvoter please comment.

                – njzk2
                May 16 '13 at 9:35




                3




                3





                +1 for (?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]

                – Fernando Leal
                Mar 28 '14 at 18:42





                +1 for (?i), it can be used for sentences. Ex: String array = "24xXx45".split("(?i)XXX"); // [24, 45]

                – Fernando Leal
                Mar 28 '14 at 18:42




                1




                1





                If you have unicode chars you should rather use (?iu) flag instead.

                – NikolaB
                Oct 14 '15 at 12:06





                If you have unicode chars you should rather use (?iu) flag instead.

                – NikolaB
                Oct 14 '15 at 12:06




                1




                1





                "24x45".split("(?i)X") not working in chrome 67

                – Chan Tzish
                Jun 18 '18 at 23:32







                "24x45".split("(?i)X") not working in chrome 67

                – Chan Tzish
                Jun 18 '18 at 23:32






                3




                3





                @ChanTzish this question is about Java

                – njzk2
                Jun 19 '18 at 3:06





                @ChanTzish this question is about Java

                – njzk2
                Jun 19 '18 at 3:06











                7














                You can also use an embedded flag in your regex:



                String array = test.split("(?i)x"); // splits case insensitive





                share|improve this answer




























                  7














                  You can also use an embedded flag in your regex:



                  String array = test.split("(?i)x"); // splits case insensitive





                  share|improve this answer


























                    7












                    7








                    7







                    You can also use an embedded flag in your regex:



                    String array = test.split("(?i)x"); // splits case insensitive





                    share|improve this answer













                    You can also use an embedded flag in your regex:



                    String array = test.split("(?i)x"); // splits case insensitive






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 16 '13 at 8:08









                    jlordojlordo

                    31.3k64371




                    31.3k64371























                        4














                        I personally prefer using



                        String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
                        String parts = modified.split(splitterValue);


                        In this way you can ensure any regex will work, as long as you have a unique splitter value






                        share|improve this answer




























                          4














                          I personally prefer using



                          String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
                          String parts = modified.split(splitterValue);


                          In this way you can ensure any regex will work, as long as you have a unique splitter value






                          share|improve this answer


























                            4












                            4








                            4







                            I personally prefer using



                            String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
                            String parts = modified.split(splitterValue);


                            In this way you can ensure any regex will work, as long as you have a unique splitter value






                            share|improve this answer













                            I personally prefer using



                            String modified = Pattern.compile("x", Pattern.CASE_INSENSITIVE).matcher(stringContents).replaceAll(splitterValue);
                            String parts = modified.split(splitterValue);


                            In this way you can ensure any regex will work, as long as you have a unique splitter value







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 23 '14 at 23:29









                            user2981810user2981810

                            149210




                            149210























                                0














                                Java's String class' split method also accepts regex.



                                To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split






                                share|improve this answer



















                                • 1





                                  "also accepts regex" actually only accepts regex.

                                  – wchargin
                                  Jun 4 '13 at 5:15











                                • @WChargin So true. And I realized it just yesterday.

                                  – zEro
                                  Jun 6 '13 at 14:11
















                                0














                                Java's String class' split method also accepts regex.



                                To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split






                                share|improve this answer



















                                • 1





                                  "also accepts regex" actually only accepts regex.

                                  – wchargin
                                  Jun 4 '13 at 5:15











                                • @WChargin So true. And I realized it just yesterday.

                                  – zEro
                                  Jun 6 '13 at 14:11














                                0












                                0








                                0







                                Java's String class' split method also accepts regex.



                                To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split






                                share|improve this answer













                                Java's String class' split method also accepts regex.



                                To keep things short, this should help you: http://www.coderanch.com/t/480781/java/java/String-split







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 16 '13 at 8:04









                                zErozEro

                                1,1281223




                                1,1281223








                                • 1





                                  "also accepts regex" actually only accepts regex.

                                  – wchargin
                                  Jun 4 '13 at 5:15











                                • @WChargin So true. And I realized it just yesterday.

                                  – zEro
                                  Jun 6 '13 at 14:11














                                • 1





                                  "also accepts regex" actually only accepts regex.

                                  – wchargin
                                  Jun 4 '13 at 5:15











                                • @WChargin So true. And I realized it just yesterday.

                                  – zEro
                                  Jun 6 '13 at 14:11








                                1




                                1





                                "also accepts regex" actually only accepts regex.

                                – wchargin
                                Jun 4 '13 at 5:15





                                "also accepts regex" actually only accepts regex.

                                – wchargin
                                Jun 4 '13 at 5:15













                                @WChargin So true. And I realized it just yesterday.

                                – zEro
                                Jun 6 '13 at 14:11





                                @WChargin So true. And I realized it just yesterday.

                                – zEro
                                Jun 6 '13 at 14:11











                                -1














                                You could use a regex as an argument to split, like this:



                                "32x23".split("[xX]");


                                Or you could use a StringTokenizer that lets you set its set of delimiters, like this:



                                StringTokenizer st = new StringTokenizer("32x23","xX");
                                // ^^ ^^
                                // string delimiter


                                This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.






                                share|improve this answer




























                                  -1














                                  You could use a regex as an argument to split, like this:



                                  "32x23".split("[xX]");


                                  Or you could use a StringTokenizer that lets you set its set of delimiters, like this:



                                  StringTokenizer st = new StringTokenizer("32x23","xX");
                                  // ^^ ^^
                                  // string delimiter


                                  This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.






                                  share|improve this answer


























                                    -1












                                    -1








                                    -1







                                    You could use a regex as an argument to split, like this:



                                    "32x23".split("[xX]");


                                    Or you could use a StringTokenizer that lets you set its set of delimiters, like this:



                                    StringTokenizer st = new StringTokenizer("32x23","xX");
                                    // ^^ ^^
                                    // string delimiter


                                    This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.






                                    share|improve this answer













                                    You could use a regex as an argument to split, like this:



                                    "32x23".split("[xX]");


                                    Or you could use a StringTokenizer that lets you set its set of delimiters, like this:



                                    StringTokenizer st = new StringTokenizer("32x23","xX");
                                    // ^^ ^^
                                    // string delimiter


                                    This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered May 16 '13 at 8:13









                                    ananaanana

                                    1,251811




                                    1,251811






























                                        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%2f16581977%2fcase-insensitive-string-split-method%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

                                        Lallio

                                        Futebolista

                                        Jornalista