Split string into individual words Java












35















I would like to know how to split up a large string into a series of smaller strings or words.
For example:




I want to walk my dog.




I want to have a string: "I",
another string:"want", etc.



How would I do this?










share|improve this question




















  • 2





    Please show what you've tried (did you look for the word "split" in the docs for String, for example?)

    – Jon Skeet
    Jul 30 '12 at 16:53






  • 9





    Yes, String#split() is named very ambiguously :-P

    – maksimov
    Jul 30 '12 at 16:53
















35















I would like to know how to split up a large string into a series of smaller strings or words.
For example:




I want to walk my dog.




I want to have a string: "I",
another string:"want", etc.



How would I do this?










share|improve this question




















  • 2





    Please show what you've tried (did you look for the word "split" in the docs for String, for example?)

    – Jon Skeet
    Jul 30 '12 at 16:53






  • 9





    Yes, String#split() is named very ambiguously :-P

    – maksimov
    Jul 30 '12 at 16:53














35












35








35


12






I would like to know how to split up a large string into a series of smaller strings or words.
For example:




I want to walk my dog.




I want to have a string: "I",
another string:"want", etc.



How would I do this?










share|improve this question
















I would like to know how to split up a large string into a series of smaller strings or words.
For example:




I want to walk my dog.




I want to have a string: "I",
another string:"want", etc.



How would I do this?







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 30 '16 at 20:40









Eric Leschinski

86.4k37318272




86.4k37318272










asked Jul 30 '12 at 16:52









foshofosho

67421020




67421020








  • 2





    Please show what you've tried (did you look for the word "split" in the docs for String, for example?)

    – Jon Skeet
    Jul 30 '12 at 16:53






  • 9





    Yes, String#split() is named very ambiguously :-P

    – maksimov
    Jul 30 '12 at 16:53














  • 2





    Please show what you've tried (did you look for the word "split" in the docs for String, for example?)

    – Jon Skeet
    Jul 30 '12 at 16:53






  • 9





    Yes, String#split() is named very ambiguously :-P

    – maksimov
    Jul 30 '12 at 16:53








2




2





Please show what you've tried (did you look for the word "split" in the docs for String, for example?)

– Jon Skeet
Jul 30 '12 at 16:53





Please show what you've tried (did you look for the word "split" in the docs for String, for example?)

– Jon Skeet
Jul 30 '12 at 16:53




9




9





Yes, String#split() is named very ambiguously :-P

– maksimov
Jul 30 '12 at 16:53





Yes, String#split() is named very ambiguously :-P

– maksimov
Jul 30 '12 at 16:53












9 Answers
9






active

oldest

votes


















64














Use split() method



Eg:



String s = "I want to walk my dog";
String arr = s.split(" ");

for ( String ss : arr) {
System.out.println(ss);
}





share|improve this answer


























  • String s already defined?

    – fosho
    Jul 30 '12 at 16:58











  • @fosho thanks... it was a typo

    – Kumar Vivek Mitra
    Jul 30 '12 at 17:00






  • 18





    This method will not remove commas, dots, and so on from the words.

    – kazy
    Mar 27 '15 at 13:20



















46














As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:



String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
String words = s.split("\W+");


The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.






share|improve this answer





















  • 5





    \W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.

    – rghome
    May 19 '17 at 13:56











  • Thank you for pointing that out! Changed answer accordingly.

    – Anton Teodor
    May 21 '17 at 14:03



















24














A regex can also be used to split words.



w can be used to match word characters ([A-Za-z0-9_]), so that punctuation is removed from the results:



String s = "I want to walk my dog, and why not?";
Pattern pattern = Pattern.compile("\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group());
}


Outputs:



I
want
to
walk
my
dog
and
why
not


See Java API documentation for Pattern






share|improve this answer


























  • Does this produce empty words?

    – Joshua Oliphant
    Apr 27 '16 at 22:10



















7














See my other answer if your phrase contains accentuated characters :



String listeMots = phrase.split("\P{L}+");





share|improve this answer





















  • 1





    This is the best answer.

    – rghome
    May 19 '17 at 14:06



















3














Yet another method, using StringTokenizer :



String s = "I want to walk my dog";
StringTokenizer tokenizer = new StringTokenizer(s);

while(tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}





share|improve this answer
























  • ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though

    – Coding Enthusiast
    Jan 20 '17 at 21:42











  • No, there isn't any array being produced . StringTokenizer looks for the consecutive tokens in the string and returns them one by one.

    – Kao
    Jan 21 '17 at 12:55






  • 1





    Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

    – Tomor
    Jan 6 '18 at 19:24





















2














You can use split(" ") method of the String class and can get each word as code given below:



String s = "I want to walk my dog";
String strArray=s.split(" ");
for(int i=0; i<strArray.length;i++) {
System.out.println(strArray[i]);
}





share|improve this answer

































    1














    Use split()



    String words = stringInstance.split(" ");





    share|improve this answer
























    • what must i import?

      – fosho
      Jul 30 '12 at 16:55






    • 2





      nothing<!------------->

      – Jigar Joshi
      Jul 30 '12 at 16:56






    • 1





      Please go through the link from answer

      – Jigar Joshi
      Jul 30 '12 at 16:59



















    1














    To include any separators between words (like everything except all lower case and upper case letters), we can do:



    String mystring = "hi, there,hi Leo";
    String arr = mystring.split("[^a-zA-Z]+");
    for(int i = 0; i < arr.length; i += 1)
    {
    System.out.println(arr[i]);
    }


    Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z], in groups of at least one [+].






    share|improve this answer

































      0














      you can use Apache commons' StringUtils class



          String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)





      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%2f11726023%2fsplit-string-into-individual-words-java%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        9 Answers
        9






        active

        oldest

        votes








        9 Answers
        9






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        64














        Use split() method



        Eg:



        String s = "I want to walk my dog";
        String arr = s.split(" ");

        for ( String ss : arr) {
        System.out.println(ss);
        }





        share|improve this answer


























        • String s already defined?

          – fosho
          Jul 30 '12 at 16:58











        • @fosho thanks... it was a typo

          – Kumar Vivek Mitra
          Jul 30 '12 at 17:00






        • 18





          This method will not remove commas, dots, and so on from the words.

          – kazy
          Mar 27 '15 at 13:20
















        64














        Use split() method



        Eg:



        String s = "I want to walk my dog";
        String arr = s.split(" ");

        for ( String ss : arr) {
        System.out.println(ss);
        }





        share|improve this answer


























        • String s already defined?

          – fosho
          Jul 30 '12 at 16:58











        • @fosho thanks... it was a typo

          – Kumar Vivek Mitra
          Jul 30 '12 at 17:00






        • 18





          This method will not remove commas, dots, and so on from the words.

          – kazy
          Mar 27 '15 at 13:20














        64












        64








        64







        Use split() method



        Eg:



        String s = "I want to walk my dog";
        String arr = s.split(" ");

        for ( String ss : arr) {
        System.out.println(ss);
        }





        share|improve this answer















        Use split() method



        Eg:



        String s = "I want to walk my dog";
        String arr = s.split(" ");

        for ( String ss : arr) {
        System.out.println(ss);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 24 '18 at 12:26









        Abdullah Khan

        5,36122843




        5,36122843










        answered Jul 30 '12 at 16:54









        Kumar Vivek MitraKumar Vivek Mitra

        29.3k63663




        29.3k63663













        • String s already defined?

          – fosho
          Jul 30 '12 at 16:58











        • @fosho thanks... it was a typo

          – Kumar Vivek Mitra
          Jul 30 '12 at 17:00






        • 18





          This method will not remove commas, dots, and so on from the words.

          – kazy
          Mar 27 '15 at 13:20



















        • String s already defined?

          – fosho
          Jul 30 '12 at 16:58











        • @fosho thanks... it was a typo

          – Kumar Vivek Mitra
          Jul 30 '12 at 17:00






        • 18





          This method will not remove commas, dots, and so on from the words.

          – kazy
          Mar 27 '15 at 13:20

















        String s already defined?

        – fosho
        Jul 30 '12 at 16:58





        String s already defined?

        – fosho
        Jul 30 '12 at 16:58













        @fosho thanks... it was a typo

        – Kumar Vivek Mitra
        Jul 30 '12 at 17:00





        @fosho thanks... it was a typo

        – Kumar Vivek Mitra
        Jul 30 '12 at 17:00




        18




        18





        This method will not remove commas, dots, and so on from the words.

        – kazy
        Mar 27 '15 at 13:20





        This method will not remove commas, dots, and so on from the words.

        – kazy
        Mar 27 '15 at 13:20













        46














        As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:



        String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
        String words = s.split("\W+");


        The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.






        share|improve this answer





















        • 5





          \W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.

          – rghome
          May 19 '17 at 13:56











        • Thank you for pointing that out! Changed answer accordingly.

          – Anton Teodor
          May 21 '17 at 14:03
















        46














        As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:



        String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
        String words = s.split("\W+");


        The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.






        share|improve this answer





















        • 5





          \W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.

          – rghome
          May 19 '17 at 13:56











        • Thank you for pointing that out! Changed answer accordingly.

          – Anton Teodor
          May 21 '17 at 14:03














        46












        46








        46







        As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:



        String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
        String words = s.split("\W+");


        The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.






        share|improve this answer















        As a more general solution (but ASCII only!), to include any other separators between words (like commas and semicolons), I suggest:



        String s = "I want to walk my dog, cat, and tarantula; maybe even my tortoise.";
        String words = s.split("\W+");


        The regex means that the delimiters will be anything that is not a word [W], in groups of at least one [+]. Because [+] is greedy, it will take for instance ';' and ' ' together as one delimiter.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 21 '17 at 13:50

























        answered Jul 28 '15 at 11:26









        Anton TeodorAnton Teodor

        56946




        56946








        • 5





          \W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.

          – rghome
          May 19 '17 at 13:56











        • Thank you for pointing that out! Changed answer accordingly.

          – Anton Teodor
          May 21 '17 at 14:03














        • 5





          \W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.

          – rghome
          May 19 '17 at 13:56











        • Thank you for pointing that out! Changed answer accordingly.

          – Anton Teodor
          May 21 '17 at 14:03








        5




        5





        \W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.

        – rghome
        May 19 '17 at 13:56





        \W only seems to consider ASCII alphabetic characters. It isn't suitable for languages with accents.

        – rghome
        May 19 '17 at 13:56













        Thank you for pointing that out! Changed answer accordingly.

        – Anton Teodor
        May 21 '17 at 14:03





        Thank you for pointing that out! Changed answer accordingly.

        – Anton Teodor
        May 21 '17 at 14:03











        24














        A regex can also be used to split words.



        w can be used to match word characters ([A-Za-z0-9_]), so that punctuation is removed from the results:



        String s = "I want to walk my dog, and why not?";
        Pattern pattern = Pattern.compile("\w+");
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()) {
        System.out.println(matcher.group());
        }


        Outputs:



        I
        want
        to
        walk
        my
        dog
        and
        why
        not


        See Java API documentation for Pattern






        share|improve this answer


























        • Does this produce empty words?

          – Joshua Oliphant
          Apr 27 '16 at 22:10
















        24














        A regex can also be used to split words.



        w can be used to match word characters ([A-Za-z0-9_]), so that punctuation is removed from the results:



        String s = "I want to walk my dog, and why not?";
        Pattern pattern = Pattern.compile("\w+");
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()) {
        System.out.println(matcher.group());
        }


        Outputs:



        I
        want
        to
        walk
        my
        dog
        and
        why
        not


        See Java API documentation for Pattern






        share|improve this answer


























        • Does this produce empty words?

          – Joshua Oliphant
          Apr 27 '16 at 22:10














        24












        24








        24







        A regex can also be used to split words.



        w can be used to match word characters ([A-Za-z0-9_]), so that punctuation is removed from the results:



        String s = "I want to walk my dog, and why not?";
        Pattern pattern = Pattern.compile("\w+");
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()) {
        System.out.println(matcher.group());
        }


        Outputs:



        I
        want
        to
        walk
        my
        dog
        and
        why
        not


        See Java API documentation for Pattern






        share|improve this answer















        A regex can also be used to split words.



        w can be used to match word characters ([A-Za-z0-9_]), so that punctuation is removed from the results:



        String s = "I want to walk my dog, and why not?";
        Pattern pattern = Pattern.compile("\w+");
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()) {
        System.out.println(matcher.group());
        }


        Outputs:



        I
        want
        to
        walk
        my
        dog
        and
        why
        not


        See Java API documentation for Pattern







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 24 '18 at 13:12









        Abdullah Khan

        5,36122843




        5,36122843










        answered Jan 31 '15 at 22:12









        PetePete

        55049




        55049













        • Does this produce empty words?

          – Joshua Oliphant
          Apr 27 '16 at 22:10



















        • Does this produce empty words?

          – Joshua Oliphant
          Apr 27 '16 at 22:10

















        Does this produce empty words?

        – Joshua Oliphant
        Apr 27 '16 at 22:10





        Does this produce empty words?

        – Joshua Oliphant
        Apr 27 '16 at 22:10











        7














        See my other answer if your phrase contains accentuated characters :



        String listeMots = phrase.split("\P{L}+");





        share|improve this answer





















        • 1





          This is the best answer.

          – rghome
          May 19 '17 at 14:06
















        7














        See my other answer if your phrase contains accentuated characters :



        String listeMots = phrase.split("\P{L}+");





        share|improve this answer





















        • 1





          This is the best answer.

          – rghome
          May 19 '17 at 14:06














        7












        7








        7







        See my other answer if your phrase contains accentuated characters :



        String listeMots = phrase.split("\P{L}+");





        share|improve this answer















        See my other answer if your phrase contains accentuated characters :



        String listeMots = phrase.split("\P{L}+");






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 12:10









        Community

        11




        11










        answered Nov 4 '16 at 2:37









        Pierre CPierre C

        550613




        550613








        • 1





          This is the best answer.

          – rghome
          May 19 '17 at 14:06














        • 1





          This is the best answer.

          – rghome
          May 19 '17 at 14:06








        1




        1





        This is the best answer.

        – rghome
        May 19 '17 at 14:06





        This is the best answer.

        – rghome
        May 19 '17 at 14:06











        3














        Yet another method, using StringTokenizer :



        String s = "I want to walk my dog";
        StringTokenizer tokenizer = new StringTokenizer(s);

        while(tokenizer.hasMoreTokens()) {
        System.out.println(tokenizer.nextToken());
        }





        share|improve this answer
























        • ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though

          – Coding Enthusiast
          Jan 20 '17 at 21:42











        • No, there isn't any array being produced . StringTokenizer looks for the consecutive tokens in the string and returns them one by one.

          – Kao
          Jan 21 '17 at 12:55






        • 1





          Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

          – Tomor
          Jan 6 '18 at 19:24


















        3














        Yet another method, using StringTokenizer :



        String s = "I want to walk my dog";
        StringTokenizer tokenizer = new StringTokenizer(s);

        while(tokenizer.hasMoreTokens()) {
        System.out.println(tokenizer.nextToken());
        }





        share|improve this answer
























        • ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though

          – Coding Enthusiast
          Jan 20 '17 at 21:42











        • No, there isn't any array being produced . StringTokenizer looks for the consecutive tokens in the string and returns them one by one.

          – Kao
          Jan 21 '17 at 12:55






        • 1





          Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

          – Tomor
          Jan 6 '18 at 19:24
















        3












        3








        3







        Yet another method, using StringTokenizer :



        String s = "I want to walk my dog";
        StringTokenizer tokenizer = new StringTokenizer(s);

        while(tokenizer.hasMoreTokens()) {
        System.out.println(tokenizer.nextToken());
        }





        share|improve this answer













        Yet another method, using StringTokenizer :



        String s = "I want to walk my dog";
        StringTokenizer tokenizer = new StringTokenizer(s);

        while(tokenizer.hasMoreTokens()) {
        System.out.println(tokenizer.nextToken());
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 10 '14 at 8:57









        KaoKao

        3,94232857




        3,94232857













        • ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though

          – Coding Enthusiast
          Jan 20 '17 at 21:42











        • No, there isn't any array being produced . StringTokenizer looks for the consecutive tokens in the string and returns them one by one.

          – Kao
          Jan 21 '17 at 12:55






        • 1





          Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

          – Tomor
          Jan 6 '18 at 19:24





















        • ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though

          – Coding Enthusiast
          Jan 20 '17 at 21:42











        • No, there isn't any array being produced . StringTokenizer looks for the consecutive tokens in the string and returns them one by one.

          – Kao
          Jan 21 '17 at 12:55






        • 1





          Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

          – Tomor
          Jan 6 '18 at 19:24



















        ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though

        – Coding Enthusiast
        Jan 20 '17 at 21:42





        ah! this is good in case where i dont need an array but isn't tokenizer returning an array of token? nice idea though

        – Coding Enthusiast
        Jan 20 '17 at 21:42













        No, there isn't any array being produced . StringTokenizer looks for the consecutive tokens in the string and returns them one by one.

        – Kao
        Jan 21 '17 at 12:55





        No, there isn't any array being produced . StringTokenizer looks for the consecutive tokens in the string and returns them one by one.

        – Kao
        Jan 21 '17 at 12:55




        1




        1





        Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

        – Tomor
        Jan 6 '18 at 19:24







        Nice solution, unfortunately, StringTokenizer should not be used anymore. From the Docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

        – Tomor
        Jan 6 '18 at 19:24













        2














        You can use split(" ") method of the String class and can get each word as code given below:



        String s = "I want to walk my dog";
        String strArray=s.split(" ");
        for(int i=0; i<strArray.length;i++) {
        System.out.println(strArray[i]);
        }





        share|improve this answer






























          2














          You can use split(" ") method of the String class and can get each word as code given below:



          String s = "I want to walk my dog";
          String strArray=s.split(" ");
          for(int i=0; i<strArray.length;i++) {
          System.out.println(strArray[i]);
          }





          share|improve this answer




























            2












            2








            2







            You can use split(" ") method of the String class and can get each word as code given below:



            String s = "I want to walk my dog";
            String strArray=s.split(" ");
            for(int i=0; i<strArray.length;i++) {
            System.out.println(strArray[i]);
            }





            share|improve this answer















            You can use split(" ") method of the String class and can get each word as code given below:



            String s = "I want to walk my dog";
            String strArray=s.split(" ");
            for(int i=0; i<strArray.length;i++) {
            System.out.println(strArray[i]);
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 19 '14 at 18:18









            rwisch45

            3,18821733




            3,18821733










            answered Jul 19 '14 at 17:54









            AKTAKT

            1334




            1334























                1














                Use split()



                String words = stringInstance.split(" ");





                share|improve this answer
























                • what must i import?

                  – fosho
                  Jul 30 '12 at 16:55






                • 2





                  nothing<!------------->

                  – Jigar Joshi
                  Jul 30 '12 at 16:56






                • 1





                  Please go through the link from answer

                  – Jigar Joshi
                  Jul 30 '12 at 16:59
















                1














                Use split()



                String words = stringInstance.split(" ");





                share|improve this answer
























                • what must i import?

                  – fosho
                  Jul 30 '12 at 16:55






                • 2





                  nothing<!------------->

                  – Jigar Joshi
                  Jul 30 '12 at 16:56






                • 1





                  Please go through the link from answer

                  – Jigar Joshi
                  Jul 30 '12 at 16:59














                1












                1








                1







                Use split()



                String words = stringInstance.split(" ");





                share|improve this answer













                Use split()



                String words = stringInstance.split(" ");






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 30 '12 at 16:53









                Jigar JoshiJigar Joshi

                199k35339390




                199k35339390













                • what must i import?

                  – fosho
                  Jul 30 '12 at 16:55






                • 2





                  nothing<!------------->

                  – Jigar Joshi
                  Jul 30 '12 at 16:56






                • 1





                  Please go through the link from answer

                  – Jigar Joshi
                  Jul 30 '12 at 16:59



















                • what must i import?

                  – fosho
                  Jul 30 '12 at 16:55






                • 2





                  nothing<!------------->

                  – Jigar Joshi
                  Jul 30 '12 at 16:56






                • 1





                  Please go through the link from answer

                  – Jigar Joshi
                  Jul 30 '12 at 16:59

















                what must i import?

                – fosho
                Jul 30 '12 at 16:55





                what must i import?

                – fosho
                Jul 30 '12 at 16:55




                2




                2





                nothing<!------------->

                – Jigar Joshi
                Jul 30 '12 at 16:56





                nothing<!------------->

                – Jigar Joshi
                Jul 30 '12 at 16:56




                1




                1





                Please go through the link from answer

                – Jigar Joshi
                Jul 30 '12 at 16:59





                Please go through the link from answer

                – Jigar Joshi
                Jul 30 '12 at 16:59











                1














                To include any separators between words (like everything except all lower case and upper case letters), we can do:



                String mystring = "hi, there,hi Leo";
                String arr = mystring.split("[^a-zA-Z]+");
                for(int i = 0; i < arr.length; i += 1)
                {
                System.out.println(arr[i]);
                }


                Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z], in groups of at least one [+].






                share|improve this answer






























                  1














                  To include any separators between words (like everything except all lower case and upper case letters), we can do:



                  String mystring = "hi, there,hi Leo";
                  String arr = mystring.split("[^a-zA-Z]+");
                  for(int i = 0; i < arr.length; i += 1)
                  {
                  System.out.println(arr[i]);
                  }


                  Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z], in groups of at least one [+].






                  share|improve this answer




























                    1












                    1








                    1







                    To include any separators between words (like everything except all lower case and upper case letters), we can do:



                    String mystring = "hi, there,hi Leo";
                    String arr = mystring.split("[^a-zA-Z]+");
                    for(int i = 0; i < arr.length; i += 1)
                    {
                    System.out.println(arr[i]);
                    }


                    Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z], in groups of at least one [+].






                    share|improve this answer















                    To include any separators between words (like everything except all lower case and upper case letters), we can do:



                    String mystring = "hi, there,hi Leo";
                    String arr = mystring.split("[^a-zA-Z]+");
                    for(int i = 0; i < arr.length; i += 1)
                    {
                    System.out.println(arr[i]);
                    }


                    Here the regex means that the separators will be anything that is not a upper or lower case letter [^a-zA-Z], in groups of at least one [+].







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 20 '16 at 6:15









                    Danh

                    4,96672335




                    4,96672335










                    answered Nov 20 '16 at 5:56









                    Akib ImtiazAkib Imtiaz

                    233




                    233























                        0














                        you can use Apache commons' StringUtils class



                            String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)





                        share|improve this answer




























                          0














                          you can use Apache commons' StringUtils class



                              String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)





                          share|improve this answer


























                            0












                            0








                            0







                            you can use Apache commons' StringUtils class



                                String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)





                            share|improve this answer













                            you can use Apache commons' StringUtils class



                                String partsOfString = StringUtils.split("I want to walk my dog",StringUtils.SPACE)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Apr 24 '18 at 12:32









                            Gagan ChouhanGagan Chouhan

                            925




                            925






























                                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%2f11726023%2fsplit-string-into-individual-words-java%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

                                To store a contact into the json file from server.js file using a class in NodeJS

                                Redirect URL with Chrome Remote Debugging Android Devices

                                Dieringhausen