简体   繁体   中英

How can I split a string into words in Java without using String.split()?

My teacher specifically requested that we split a sentence into words without using String.split() . I've done it using a Vector (which we haven't learned), a while -loop, and substrings. What are other ways of accomplishing this? (preferably without using Vectors / ArrayLists ).

I believe that your teacher is asking you to process the string yourself (without using any other libraries to do it for you). Check to see if this is the case - if you can use them, there are things such as StringTokenizer, Pattern, and Scanner to facilitate string processing.

Otherwise...

You will need a list of word separators (such as space, tab, period, etc...) and then walk the array, building a string a character at a time until you hit the word separator. After finding a complete word (you have encountered a word separator character), save it the variable out into your structure (or whatever is required), reset the variable you are building the word in and continue.

Parsing the string character by character, copying each character into a new String, and stopping when you reach a white space character. Then start a new string and continue until you reach the end of the original string.

您可以使用java.util.Scanner。

You can use java.util.StringTokenizer to split a text using desired delimiter. Default delimiter is SPACE/TAB/NEW_LINE.

String myTextToBeSplit = "This is the text to be split into words.";  
StringTokenizer tokenizer = new StringTokenizer( myTextToBeSplit );  
while ( tokinizer.hasMoreTokens()) {  
    String word = tokinizer.nextToken();  
    System.out.println( word ); // word you are looking in  
}  

As an alternate you can also use java.util.Scanner

Scanner s = new Scanner(myTextToBeSplit).useDelimiter("\\s");  
while( s.hasNext() ) {  
System.out.println(s.next());  
}  
s.close();  
import java.util.Arrays;
public class ReverseTheWords {

    public static void main(String[] args) {
        String s = "hello java how do you do";
        System.out.println(Arrays.toString(ReverseTheWords.split(s)));
    }

    public static String[] split(String s) {
        int count = 0;
        char[] c = s.toCharArray();

        for (int i = 0; i < c.length; i++) {
            if (c[i] == ' ') {
                count++;
            }
        }
        String temp = "";
        int k = 0;
        String[] rev = new String[count + 1];
        for (int i = 0; i < c.length; i++) {
            if (c[i] == ' ') {
                rev[k++] = temp;
                temp = "";
            } else
                temp = temp + c[i];
        }
        rev[k] = temp;
        return rev;
    }

}

或者使用Pattern (也称为正则表达式)来尝试匹配单词。

  • Use a Scanner with ctor (String)
  • regular expressions and match
  • StringTokenizer
  • iterating yourself char by char
  • recursive iteration

Without using a Vector / List (and without manually re-implementing their ability to re-size themselves for your function), you can take advantage of the simple observation that a string of length N cannot have more than (N+1)/2 words (in integer division). You can declare an array of strings of that size, populate it the same way you populated that Vector , and then copy the results to an array of the size of the number of words you found.

So:

String[] mySplit( String in ){
    String[] bigArray = new String[ (in.length()+1)/2 ];

    int numWords = 0;
    // Populate bigArray with your while loop and keep
    // track of the number of words

    String[] result = new String[numWords];
    // Copy results from bigArray to result

    return result;
}
public class MySplit {

public static String[] mySplit(String text,String delemeter){
    java.util.List<String> parts = new java.util.ArrayList<String>();
    text+=delemeter;        

    for (int i = text.indexOf(delemeter), j=0; i != -1;) {
        parts.add(text.substring(j,i));
        j=i+delemeter.length();
        i = text.indexOf(delemeter,j);
    }


    return parts.toArray(new String[0]);
}

public static void main(String[] args) {
    String str="012ab567ab0123ab";
    String delemeter="ab";
    String result[]=mySplit(str,delemeter);
    for(String s:result)
        System.out.println(s);
}

}
public class sha1 {
public static void main(String[] args) {
    String s = "hello java how do you do";
    System.out.println(Arrays.toString(sha1.split(s)));
}
public static String[] split(String s) {
    int count = 0;
    char[] c = s.toCharArray();

    for (int i = 0; i < c.length; i++) {
        if (c[i] == ' ') {
            count++;
        }
    }
    String temp = "";
    int k = 0;
    String[] rev = new String[count + 1];
    for (int i = c.length-1; i >= 0; i--) {
        if (c[i] == ' ') {
            rev[k++] = temp;
            temp = "";
        } else
            temp = temp + c[i];
    }
    rev[k] = temp;
    return rev;
}

}

Simple touch.! Improve if you want to.

package com.asif.test;

public class SplitWithoutSplitMethod {

public static void main(String[] args) {

    split('@',"asif@is@handsome");
}

static void split(char delimeter, String line){

    String word = "";
    String wordsArr[] = new String[3];

    int k = 0;
    for(int i = 0; i <line.length(); i++){

        if(line.charAt(i) != delimeter){
            word+= line.charAt(i);
        }else{
            wordsArr[k] = word;
            word = "";
            k++;                    
        }
    }
    wordsArr[k] = word;
    for(int j = 0; j <wordsArr.length; j++)
    System.out.println(wordsArr[j]);

}

}

Please try this .

  public static String[] mysplit(String mystring) {

    String string=mystring+" ";               //append " " bcz java string does not hava any ending character
    int[] spacetracker=new int[string.length()];// to count no. of spaces in string
    char[] array=new char[string.length()];     //store all non space character
    String[] tokenArray=new String[string.length()];//to return token of words

    int spaceIndex=0;
    int parseIndex=0;
    int arrayIndex=0;
    int k=0;
    while(parseIndex<string.length())
    {
        if(string.charAt(parseIndex)==' '||string.charAt(parseIndex)==' ')
        {
            spacetracker[spaceIndex]=parseIndex;
            spaceIndex++;
            parseIndex++;
        }else
        {
        array[arrayIndex]=string.charAt(parseIndex);
        arrayIndex++;
        parseIndex++;
        }
    }


    for(int i=0;i<spacetracker.length;i++)
    {
        String token="";
        for(int j=k;j<(spacetracker[i])-i;j++)
        {
            token=token+array[j];
            k++;

        }
        tokenArray[i]=token;
        //System.out.println(token);
        token="";

    }
    return tokenArray;
}

Hope this helps

import java.util.*;
class StringSplit {
    public static void main(String[] args) 
    {
        String s="splitting a string without using split()";
        ArrayList<Integer> al=new ArrayList<Integer>();     //Instead you can also use a String 
        ArrayList<String> splitResult=new ArrayList<String>();
        for(int i=0;i<s.length();i++)
            if(s.charAt(i)==' ')
                al.add(i);
        al.add(0, 0);
        al.add(al.size(),s.length());
        String[] words=new String[al.size()];
        for(int j=0;j<=words.length-2;j++)
                splitResult.add(s.substring(al.get(j),al.get(j+1)).trim());
        System.out.println(splitResult);
    }
}

Time complexity: O(n)

You can use java Pattern to do it in easy way.

package com.company;

import java.util.regex.Pattern;

public class umeshtest {

    public static void main(String a[]) {
        String ss = "I'm Testing and testing the new feature";
        Pattern.compile(" ").splitAsStream(ss).forEach(s -> System.out.println(s));
    }
}

您还可以使用String.substringcharAt[]

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