Results 1 to 6 of 6

Thread: How to use StringTokenizer to split a string in java?

  1. #1
    Join Date
    Aug 2009
    Posts
    57

    How to use StringTokenizer to split a string in java?

    Hello to all,
    I am S.Y.B.Sc.I.T. student. I am started learning java programming language. In last lecture our sir has learned how to use StringTokenizer. Today they asked one question How to use StringTokenizer to split a string in java? None of us able to give answer. Can anyone know How to use StringTokenizer to split a string in java? Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to use StringTokenizer to split a string in java?

    The following code is an example of using StringTokenizer to split a string. Just go through it.
    Code:
    import java.util.StringTokenizer;
     
    public class StringTo
    {
        public static void main(String[] args)
        {
            StringTo st1 =
                    new StringTokenizer("a stringtokenizer sample");
     
            
            System.out.println("tokens count: " + st1.countTokens());
     
            
            while (st1.hasMoreElements())
            {
                String token1 = st1.nextElement().toString();
                System.out.println("token = " + token1);
            }
     
            
            st1 = new StringTokenizer("2010/11/10", "/");
            while (st1.hasMoreElements())
            {
                String token1 = st1.nextToken();
                System.out.println("token = " + token1);
            }
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to use StringTokenizer to split a string in java?

    Hey it is very easy process to use StringTokenizer to split a string in java. I had written one example of it. Just go through it.


    Code:
    import java.util.*; 
      
      
     class ParseS 
        {   
       public static void main  ( String args [  ]  )   
        {  
       String s1 = "1st,2nd, 3rd 4th, 5th"; 
       StringTokenizer st1; 
      
      
       st1 = new StringTokenizer  ( s1, ", " ) ; 
      
      
       while  ( st1.hasMoreTokens  (  )  )  
          {  
         System.out.println  ( " >  > " + st1.nextToken  (  )  + " <  < " ) ; 
          }  
        }   
        }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to use StringTokenizer to split a string in java?

    You have to use following code to do this process. Following code show how to use StringTokenizer to split a string in java? Just try to understand each steps.

    Code:
    import java.util.*;
    //import java.text.DecimalFormat;
    //import java.awt.*;
    //import java.awt.event.*;
    import javax.swing.*;
    
    
    public class CusIdentity
    {
          
          
          public void CompanyNameAfterUpdate()
          {
                
               String  cName = "welcome";
              String sT = cName.trim();
              String sCID = "";
              String custId1;
             
              
              sT = sT.replace(' ',' ');
             
             
              if (sTemp.length() < 5) {
                   sTemp += "     ".substring(sTemp.length());
                  }
                 
                 
                 
                  String STT = sT;
                  STT tokens = new StringTokenizer (STT);
                 
                  System.out.println( tokens1.countTokens() + "\nThe numbers of tokens are: \n" );
                  while ( tokens1.hasMoreTokens() );
                  System.out.println(tokens1.nextToken() + "\n");
                 
               
                 
                 
                }
            }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to use StringTokenizer to split a string in java?

    Hey it is safer way to split the string would be by using the StringTokenizer API. Here's an example of it.




    Code:
    import java.util.StringTokenizer;
    
    public class StringTest2 {
       public static void main(String[] args) {
          final String SS = "^";
          final String MS = "T-1^T-2^T-3";
          final StringTokenizer stToken = new StringTokenizer(
             MS, SPLIT_STR);
          final String[] SS = new String[stToken.countTokens()];
    
          int i = 0;
          while(stToken.hasMoreElements()) {
             SS[index++] = stT.nextToken();
          }
          for(in=0; i < SS.length; i++) {
             System.out.println("Tokenizer : " + SS[i]);
          }
       }
    }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to use StringTokenizer to split a string in java?

    Hey most of the user use StringTokenizer to split a string in java. Here I written one of it's example.


    Code:
    package com.javadb.examples;
    
    import java.util.StringTokenizer;
    
    
    public class Main {
    
        public static void main(String[] args) {
            StringTokenizer TK =
                    new StringTK("This is one of the StringTokenizer Java Code Example");
    
            
            System.out.println("The Number of tokens: " + TK.countTokens());
    
           
            while (TK.hasMoreElements()) {
                String token = TK.nextToken();
                System.out.println(token);
            }
    
    
            
        }
    }

Similar Threads

  1. StringTokenizer problem in java
    By Jensen Ackles in forum Software Development
    Replies: 5
    Last Post: 06-04-2010, 03:16 PM
  2. Tokens used to split string
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 04:55 AM
  3. StringTokenizer class of java
    By Mithun Seth in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 01:12 PM
  4. Split a string using separators
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 01:10 AM
  5. How to split the string in c++?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 09:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,506,916.94728 seconds with 17 queries