Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

SPLIT METHOD IN STRING CLASS

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class SplitTest {
public static void main(String[] args) {
String[] tokens = args[0].split(args[1]);
System.out.println("count " + tokens.length);
for(String s : tokens)
System.out.println(">" + s + "<");
}
}

java SplitTest "ab5 ccc 455 67 @" "\d"

Can any one explain please how the split method is working

I have tried but i could not understand.
OR

Tell me the source where i can find good tutorial for this.

Thanks
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you familiar with regular expressions? Without that, the method will remain a mystery.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Ulf

I know regular expressions.

I could not understand how the split is working .

This is the out put i am getting


count 7
>ab<
> ccc<
>< //line 1
>< //lien 2
> <
>< //line 3
> @<

The line 1,2,3 i could not understand
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai anil,
In that program your delimiter is "digit".
So in the argument ("ab5 ccc 455 67 @")
the tokens are

>ab<
> ccc<
>< //line 1
>< //lien 2
> <
>< //line 3
> @<
The line 1,2,3 came because 4is one delimiter after that immediate delimiter 5 so they took as empty(>< token.

Try the output with this:java SplitTest "ab5 ccc 45 5 67 @" "\d"


You will get:
count 7
>ab<
> ccc <
>< \\\1
> <
> <
>< \\\2
> @<
1,2 is giving empty token because you are having continues delimiter.

Now you are clear with this spilt() .
Regards,
Premavenkat.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Test your code by passing the string "ab5xcccx455x67x@" instead, where the spaces were replaced by 'x'. This will help you better understand how it works.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

now i am clear with it,but please check with this output


C:\practice>java SplitTest "ab5xcccx455x67x@7777" "\d"
count 7
>ab<
>xcccx<
><
><
>x<
><
>x@<

why it is not giving space for 77777
thanks
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class Demo {
public static void main(String[] args) {
String[] tokens = args[0].split(args[1]);
System.out.println("count " + tokens.length);
for(String s : tokens)
System.out.println(">" + s + "<");
}
}

if this is the program and we invoke the program

java Demo "777" "7"
its giving count 0

but if the program is invoked
java Demo "777sdfa" "7"
its giving
><
><
><
>sdfa<

first when i invoked the program it did not print 3 spaces why so?
second) of all when i invoked the program second time it printed a space for the first 7 in the arg[0]?

third) if we use scanner class for tokenization then with above mentioned invokations the and that we are using "7" as delimiter

java tokenize "777"
><
><

java tokenize "777sdfa"
><
>sdfa<
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic