Results 1 to 5 of 5
  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    9

    Default How to split a String using split function

    This tip will show the use of split function to split a String according to particular separator character.

    In this program Split() function is splitting the String according to :.

    Java Code:
    public class StringSplit {
     
    public void doit() {
     	String str = "CompChamps:Guys:are:There";
     	String [] temp = null;
     	temp = str.split(":");
     	dump(temp);
    }
    
    public void dump(String []s) {
     	for (int i = 0 ; i < s.length ; i++) {
     		System.out.println(s[i]);
     	}
    }
    }
    public static void main(String args[]) throws Exception{
      	StringSplit ss = new StringSplit();
                 ss.doit();
    }

  2. #2
    jazz2k8's Avatar
    jazz2k8 is offline Senior Member
    Join Date
    Apr 2008
    Posts
    150
    Rep Power
    7

    Thumbs up

    Try this code :
    public class splitdemo {

    private static String REGEX = ":";
    private static String INPUT = "CompChamps:Guys:are:There";
    public static void main(String[] argv) {
    /*A compiled representation of a regular expression.
    regular expression, specified as a string, must first be compiled into an instance of this class.*/
    Pattern p = Pattern.compile(REGEX);
    // editing your input file, regex.txt
    String[] items = p.split(INPUT);
    for (int i = 0; i < items.length; i++) {
    System.out.println(items[i]);
    }
    }
    }

  3. #3
    peter_lawrey is offline Member
    Join Date
    Mar 2008
    Posts
    10
    Rep Power
    0

    Default

    A shorter example
    Java Code:
    public class StringSplit {
        public static void main(String[] argv) {
            System.out.println(Arrays.asList(
                "CompChamps:Guys:are:There".split(":")));
        }
    }

  4. #4
    neeraj.singh is offline Member
    Join Date
    Feb 2009
    Location
    Delhi
    Posts
    63
    Rep Power
    0

    Default

    to split a string use
    StringTokenizer class
    this will provide you a wide properties and options.

  5. #5
    mikeitalydz is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    Hello every one im new to Java and my question here is how i cant get the inistials of any word such the name John i want to get only the letter "J" will be displayed i tried to use split fanction but didnt help and this is my code :
    String fname;

    String mname;
    String lname;




    fname = JOptionPane.showInputDialog(alert, "What is Your First Name: ");
    mname = JOptionPane.showInputDialog(alert, "What Is your Middle Name: ");
    lname = JOptionPane.showInputDialog(alert, "What Is your Last Name: ");
    JOptionPane.showMessageDialog(alert, "The full name is : " +fname.substring(0)." "+mname.substring(0)+ " "+lname.substring(0)+ " . ");

    after i got the full name like John Bob, Mike
    i want to initials the names so i get the first letter of each word the outpit will be J B M
    I need your help
    thank you

Similar Threads

  1. problem with split method
    By abhiN in forum New To Java
    Replies: 7
    Last Post: 02-10-2009, 01:54 PM
  2. how to split a file
    By nagaraaju in forum New To Java
    Replies: 0
    Last Post: 03-14-2008, 08:45 AM
  3. Replies: 3
    Last Post: 12-17-2007, 02:35 AM
  4. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM
  5. PDF Split and Merge 0.7 beta 1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-24-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •