split string in java

Java equivalent of PHP explode function

In PHP we can easily break a long string into smaller parts by using the explode() function of PHP.
$longstring="I am a cool guy";
$brokenstring=explode(" ", $longstring);
After the execution of the second command the variable $brokenstring is an array such that,
$brokenstring[0]="I"
$brokenstring[1]="am"

$brokenstring[2]="a"
$brokenstring[3]="cool"
$brokenstring[4]="guy"





In java we use String.split() function that achieves the same objective.
class splitString
    {
        public static void main(String[] args)
            {
            String s1 = "I am a cool guy";
            String[] array = s1.split(" ");
      
            for(int i=0; i
                {
                System.out.println(array[i]);
                }
            }
    }

 /*
array[0] ="I"
array[1] ="am"
array[2] ="a" 
array[3] ="cool"
array[4] ="guy" 
*/

The output will be like:
I
am
a
cool
guy


Comments

Popular posts from this blog

ubuntu package installation

Drupal Bootstrap Database

How to fix 500 internal privoxy error