Leo Jiang
Leo Jiang

Reputation: 26075

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).

Upvotes: 5

Views: 25963

Answers (16)

Umesh Sulakude
Umesh Sulakude

Reputation: 300

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));
    }
}

Upvotes: 0

SAM Jr
SAM Jr

Reputation: 32

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)

Upvotes: 0

Asif Khan
Asif Khan

Reputation: 71

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]);

}

}

Upvotes: 1

Santhosh Rajkumar
Santhosh Rajkumar

Reputation: 121

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;
}

}

Upvotes: 1

vinay j
vinay j

Reputation: 305

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;
    }

}

Upvotes: 2

Manoj Pal
Manoj Pal

Reputation: 180

You can also use String.substring or charAt[].

Upvotes: -3

ak0692
ak0692

Reputation: 233

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

Upvotes: 0

Akhilesh Dhar Dubey
Akhilesh Dhar Dubey

Reputation: 2148

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);
}

}

Upvotes: 1

trutheality
trutheality

Reputation: 23455

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;
}

Upvotes: 1

Ravinder Reddy
Ravinder Reddy

Reputation: 23982

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();  

Upvotes: 3

user289086
user289086

Reputation:

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.

Upvotes: 13

user unknown
user unknown

Reputation: 36229

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

Upvotes: 1

David K
David K

Reputation: 690

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.

Upvotes: 4

Tim Bender
Tim Bender

Reputation: 20442

Or use a Pattern (also known as a regular expression) to try to match the words.

Upvotes: 1

rayd09
rayd09

Reputation: 1897

You can use java.util.Scanner.

Upvotes: 2

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9307

YOu can use StringTokenizer http://www.java-samples.com/showtutorial.php?tutorialid=236

Upvotes: 1

Related Questions