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

String Splitter

By Koundinya

User defined StringTokenizer
Java, Java, NT4, Win2K, Windows, Dev

Posted: 1 Nov 2001
Updated: 1 Nov 2001
Views: 61,797
Bookmarked: 4 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
20 votes for this Article.
Popularity: 3.73 Rating: 2.87 out of 5
5 votes, 50.0%
1
0 votes, 0.0%
2
1 vote, 10.0%
3
0 votes, 0.0%
4
4 votes, 40.0%
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

 

I got a problem where NULL values should also act a role in my program. But when I use StringTokenizer class to split, it eliminates null values, so I have written my own program that will help me to give null values.

 

 

import java.util.*;

public class Split

{

        public String[] split(String str,char x)

        {

                Vector v=new Vector();

                String str1=new String();

                for(int i=0;i<str.length();i++)

                {

                        if(str.charAt(i)==x)

                        {

                                v.add(str1);

                                str1=new String();

                        }

                        else

                        {

                                str1+=str.charAt(i);

                        }

                }

                v.add(str1);

                String array[];

                array=new String[v.size()];

                for(int i=0;i<array.length;i++)

                {

                        array[i]=new String((String)v.elementAt(i));

                }

 

                return array;

        }

        public static void main(String s[])

        {

                Split ss=new Split();

                String array[];

                array=ss.split(s[0],s[1].charAt(0));

                for(int i=0;i<array.length;i++)

                System.out.println(array[i]);

               

        }

}

 

Example: String str=”Koundinya,,”progarmer”;

StringTokeizer st=new StringTokenizer(str);

 

Split ss=new Split();

String array[];

array=ss.split(str,’,’);

 

StringTokenizer will split this into Koundinya and programmer

But  Split.class gives result as Koundinya null and programmer

 

 

I hope this program will help the beginners

 

 

Koundinya

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

Koundinya


Hi
I am from South India (Hyderabad). Basically I am lover of mathematics which made me to enter computer field. I started learning computer sciences with language B.B.C Basic.
Later I changed my track to C, C++ and Java.


Occupation: Web Developer
Location: India India

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   
  (Refresh) 
-- There are no messages in this forum --

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

PermaLink | Privacy | Terms of Use
Last Updated: 1 Nov 2001
Editor:
Copyright 2001 by Koundinya
Everything else Copyright © CodeProject, 1999-2008
Java | Advertise on the Code Project