Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

SPLIT METHOD IN STRING CLASS

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • 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: 43016
76
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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<
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic