how to print first string in a line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lekshminair
    New Member
    • Jan 2007
    • 3

    how to print first string in a line

    hello friends,
    can u help me.
    how to print first string in a line.(before first space)
    for example:
    String str={"Hello world java"}

    output
    Hello
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #2
    may be this code will help:
    Code:
    import java.util.StringTokenizer;
    public class A1 {
    public static void main(String args[])
    {
    A2 newA2=new A2();
     int a[]={1,2,3,4,5,6,7,8,9};
    int i=a.length;
    for(int j=0;j<i;j++)
     newA2.PrintA(a[j]);
    String aa=new String("hello world java");
    StringTokenizer tokens = new StringTokenizer (aa," ");
    System.out.println(tokens.nextElement());
    }
    }

    Originally posted by lekshminair
    hello friends,
    can u help me.
    how to print first string in a line.(before first space)
    for example:
    String str={"Hello world java"}

    output
    Hello

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by hirak1984
      may be this code will help:
      Code:
      import java.util.StringTokenizer;
      public class A1 {
      public static void main(String args[])
      {
      A2 newA2=new A2();
      int a[]={1,2,3,4,5,6,7,8,9};
      int i=a.length;
      for(int j=0;j<i;j++)
      newA2.PrintA(a[j]);
      String aa=new String("hello world java");
      StringTokenizer tokens = new StringTokenizer (aa," ");
      System.out.println(tokens.nextElement());
      }
      }
      It is much prefered these days not to use tokenizer but to use the split method
      Code:
       String s = "Hello World"; 
      String[] words = s.split(" ");
      now the array words contains the two strings

      Comment

      Working...