Click here to Skip to main content
Licence CPOL
First Posted 1 Nov 2001
Views 69,712
Bookmarked 12 times

String Splitter

By Koundinya | 1 Nov 2001
User defined StringTokenizer
5 votes, 50.0%
1

2
1 vote, 10.0%
3

4
4 votes, 40.0%
5
2.87/5 - 20 votes
μ 2.87, σa 3.48 [?]

Introduction

I got a problem where NULL values should also play 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,,”programmer”;
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 beginners.

History

  • 1st November, 2001: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Koundinya

Web Developer

India India

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


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 2 Nov 2001
Article Copyright 2001 by Koundinya
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid