How to use split method in Java?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • korea saeed
    New Member
    • Jan 2011
    • 7

    How to use split method in Java?

    hi its a code but its not work plz write whats wrong ?

    import java.lang.Strin g;

    public class Main {

    public static void main(String[] args) {

    String test="java is best";

    System.out.prin tln( test.split(" "));

    }
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Split method return the array of objects. So placing it in print function won't print the string after splitting.

    Please try the following.
    Code:
    String test="java is best";
    String[] strobj = test.split(" ");
    for (String string : strobj) {
    	System.out.println( string);
    }
    Please see the method description here.

    And please do not double post.

    Regards
    Dheeraj Joshi

    Comment

    • korea saeed
      New Member
      • Jan 2011
      • 7

      #3
      thx you are soooooooooooooo oooooooo good ;)

      Comment

      • john4java
        New Member
        • Mar 2012
        • 1

        #4
        If you do not want to get that in a String array and print it straight then u can use this ...

        String test="java is best";
        System.out.prin tln( test.split(" ")[2]); ......

        this will give you : best
        so it mean
        [0] = java
        [1] = is
        [2] = best

        Comment

        Working...