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

后端 未结 16 2395
余生分开走
余生分开走 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:34

    Here is a solution in plain and simple C++ code with no fancy function, use DMA to allocate a dynamic string array, and put data in array till you find a open space. please refer code below with comments. I hope it helps.

    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
    
    string data="hello there how are you"; // a_size=5, char count =23
    //getline(cin,data); 
    int count=0; // initialize a count to count total number of spaces in string.
    int len=data.length();
    for (int i = 0; i < (int)data.length(); ++i)
    {
        if(data[i]==' ')
        {
            ++count;
        }
    }
    //declare a string array +1 greater than the size 
    // num of space in string.
    string* str = new string[count+1];
    
    int i, start=0;
    for (int index=0; index<count+1; ++index) // index array to increment index of string array and feed data.
    {   string temp="";
        for ( i = start; i <len; ++i)
        {   
            if(data[i]!=' ') //increment temp stored word till you find a space.
            {
                temp=temp+data[i];
            }else{
                start=i+1; // increment i counter to next to the space
                break;
            }
        }str[index]=temp;
    }
    
    
    //print data 
    for (int i = 0; i < count+1; ++i)
    {
        cout<<str[i]<<" ";
    }
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-01 00:35

    You can use BreakIterator.getWordInstance to find all words in a string.

    public static List<String> getWords(String text) {
        List<String> words = new ArrayList<String>();
        BreakIterator breakIterator = BreakIterator.getWordInstance();
        breakIterator.setText(text);
        int lastIndex = breakIterator.first();
        while (BreakIterator.DONE != lastIndex) {
            int firstIndex = lastIndex;
            lastIndex = breakIterator.next();
            if (lastIndex != BreakIterator.DONE && Character.isLetterOrDigit(text.charAt(firstIndex))) {
                words.add(text.substring(firstIndex, lastIndex));
            }
        }
    
        return words;
    }
    

    Test:

    public static void main(String[] args) {
        System.out.println(getWords("A PT CR M0RT BOUSG SABN NTE TR/GB/(G) = RAND(MIN(XXX, YY + ABC))"));
    }
    

    Ouput:

    [A, PT, CR, M0RT, BOUSG, SABN, NTE, TR, GB, G, RAND, MIN, XXX, YY, ABC]
    
    0 讨论(0)
  • 2020-12-01 00:39

    You can just split your string like that using this regular expression

    String l = "sofia, malgré tout aimait : la laitue et le choux !" <br/>
    l.split("[[ ]*|[,]*|[\\.]*|[:]*|[/]*|[!]*|[?]*|[+]*]+");
    
    0 讨论(0)
  • 2020-12-01 00:39

    Use string.replace(".", "").replace(",", "").replace("?", "").replace("!","").split(' ') to split your code into an array with no periods, commas, question marks, or exclamation marks. You can add/remove as many replace calls as you want.

    0 讨论(0)
  • 2020-12-01 00:41

    The easiest and best answer I can think of is to use the following method defined on the java string -

    String[] split(String regex)
    

    And just do "This is a sample sentence".split(" "). Because it takes a regex, you can do more complicated splits as well, which can include removing unwanted punctuation and other such characters.

    0 讨论(0)
  • 2020-12-01 00:42

    I already did post this answer somewhere, i will do it here again. This version doesn't use any major inbuilt method. You got the char array, convert it into a String. Hope it helps!

    import java.util.Scanner;
    
    public class SentenceToWord 
    {
        public static int getNumberOfWords(String sentence)
        {
            int counter=0;
            for(int i=0;i<sentence.length();i++)
            {
                if(sentence.charAt(i)==' ')
                counter++;
            }
            return counter+1;
        }
    
        public static char[] getSubString(String sentence,int start,int end) //method to give substring, replacement of String.substring() 
        {
            int counter=0;
            char charArrayToReturn[]=new char[end-start];
            for(int i=start;i<end;i++)
            {
                charArrayToReturn[counter++]=sentence.charAt(i);
            }
            return charArrayToReturn;
        }
    
        public static char[][] getWordsFromString(String sentence)
        {
            int wordsCounter=0;
            int spaceIndex=0;
            int length=sentence.length();
            char wordsArray[][]=new char[getNumberOfWords(sentence)][]; 
            for(int i=0;i<length;i++)
            {
                if(sentence.charAt(i)==' ' || i+1==length)
                {
                wordsArray[wordsCounter++]=getSubString(sentence, spaceIndex,i+1); //get each word as substring
                spaceIndex=i+1; //increment space index
                }
            }
            return  wordsArray; //return the 2 dimensional char array
        }
    
    
        public static void main(String[] args) 
        {
        System.out.println("Please enter the String");
        Scanner input=new Scanner(System.in);
        String userInput=input.nextLine().trim();
        int numOfWords=getNumberOfWords(userInput);
        char words[][]=new char[numOfWords+1][];
        words=getWordsFromString(userInput);
        System.out.println("Total number of words found in the String is "+(numOfWords));
        for(int i=0;i<numOfWords;i++)
        {
            System.out.println(" ");
            for(int j=0;j<words[i].length;j++)
            {
            System.out.print(words[i][j]);//print out each char one by one
            }
        }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题