5,662,937 members and growing! (112 online)
Email Password   helpLost your password?
General Programming » String handling » Strings     Intermediate

String Splitter

By Wee Ding

A user defined String Tokenizer (Java)
Java, Java, Dev

Posted: 9 Mar 2003
Updated: 9 Mar 2003
Views: 30,913
Bookmarked: 2 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 1.57 Rating: 1.30 out of 5
13 votes, 81.3%
1
1 vote, 6.3%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
2 votes, 12.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This is a simple program to slip a string by providing a delimiter string. I met problems when I want to use delimiter as "[XX]" with JDK split function, hence I wrote a simple one here. This material is mainly for beginner. I publish this right after I wrote, hence I didn't double check of this codes thoroughly but I suppose the strSplit function shld be working.

public class StrSplit { 

  public static String[] strSplit (String _str, String _x) {
    Vector _v = new Vector();
    String _stmp = new String();
    int i=0,j=0,cnt=0;
    while ((i=_str.indexOf(_x,i))!=-1) {
      cnt++;
      if (cnt%2==1) {
        i = j = i+_x.length();
        continue;
      }
      _stmp = _str.substring(j,i);
      _v.add(_stmp);
      _stmp = new String();
      j = i+_x.length();
    }
    if (j < _str.length()-1) {
      _stmp = _str.substring(j,_str.length());
      _v.add(_stmp);
    }
    String[] _array = new String[_v.size()];
    for (int k=0;k<_array.length;k++)
      _array[k] = new String(((String)_v.elementAt(k)).trim());
    return _array;
  } 

  /** Test **/
  public static void main(String s[]) {
    StrSplit tt=new StrSplit();
    String array[];
    array=tt.strSplit("[STR] Rank bagus manis [STR] Grade A CC BBB","[STR]");
    for(int i=0;i<array.length;i++)
    System.out.println(array[i]);
  }

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Wee Ding



Occupation: Web Developer
Location: Singapore Singapore

Other popular String handling articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralA few commentsmemberRyan Binns5:26 5 Jul '03  
GeneralAlready exists in JDKmemberRui "viZual" Lopes8:35 10 Mar '03  
GeneralRe: Already exists in JDKmemberJoe Billy19:26 12 Oct '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Mar 2003
Editor:
Copyright 2003 by Wee Ding
Everything else Copyright © CodeProject, 1999-2008
Java | Advertise on the Code Project