Converting a sentence string to a string array of words in Java

后端 未结 16 2436
余生分开走
余生分开走 2020-12-01 00:04

I need my Java program to take a string like:

\"This is a sample sentence.\"

and turn it into a string array like:

{\"this\         


        
16条回答
  •  眼角桃花
    2020-12-01 00:29

    Following is a code snippet which splits a sentense to word and give its count too.

     import java.util.HashMap;
     import java.util.Iterator;
     import java.util.Map;
    
     public class StringToword {
    public static void main(String[] args) {
        String s="a a a A A";
        String[] splitedString=s.split(" ");
        Map m=new HashMap();
        int count=1;
        for(String s1 :splitedString){
             count=m.containsKey(s1)?count+1:1;
              m.put(s1, count);
            }
        Iterator itr=m.entrySet().iterator();
        while(itr.hasNext()){
            System.out.println(itr.next());         
        }
        }
    
    }
    

提交回复
热议问题