简体   繁体   中英

Split string into individual words Java

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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 String split() method example<\/strong><\/em>

 public class SplitExample{  
        public static void main(String args[]){  
            String str="java string split method";  
            String[] words=str.split("\\s");//splits the string based on whitespace  
     
            for(String word:words){  
                System.out.println(word);  
            }  
        }
    }
class test{
           
    public static void main(String[] args){
                StringTokenizer st= new StringTokenizer("I want to walk my dog.");
                
                while (st.hasMoreTokens())
                    System.out.println(st.nextToken());
         
            }
        }

Using Java Stream API:

String sentence = "I want to walk my dog.";

Arrays.stream(sentence.split(" ")).forEach(System.out::println);

Output:

I
want
to
walk
my
dog.

Or

String sentence2 = "I want to walk my dog.";

Arrays.stream(sentence2.split(" ")).map(str -> str.replace(".", "")).forEach(System.out::println);

Output:

I
want
to
walk
my
dog

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM