Menu
Topics Index
...
`

splitting String by using split Method


If you need explanation Read this topic

If you need Answer Take test on this topic

splitting String by using split Method
What is the output of the following program?
public class RegEx {
    public static void main(String[] args) {
        String input = "Hello Welcome to Merit Campus";
        String[] result1 = input.split("\\s");
        String[] result2 = input.split("\\d");
        String[] result3 = input.split("\\S");
        System.out.println(result1.length + " " + result2.length + " " + result3.length);
    }
}

A. 5 1 20
B. 5 0 5
C. 20 0 5
D. 5 0 0
E. Compilation Error
Topic: Additional String Methods in Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

C is the answer in split method we use \\ to mention the character where we want to split since a single slash will b considered escape char

Posted by Karteek Paruchuri    2013-08-20 03:55:45


ans is A..\s splits the string at every space,\d splits at every integer and \S splits at every non-white space..

Posted by Soumya Tata    2013-08-20 04:00:10


Ans is A....as the split method of String class will take a regular expression and split the input String based on that pattern..here //s indicates split the string when a space occurs

Posted by Krishna Sai    2013-08-20 04:01:10


D

Posted by Sreenath Vallapaneni    2013-08-20 04:33:35


d

Posted by Manasa Sudini    2013-08-20 07:31:49


Congratulations Soumya Tata. You are this dose winner. Please send us your mobile details in a private message and we will process the recharge asap.

Posted by Merit Campus    2013-08-21 04:05:11


© meritcampus 2019

All Rights Reserved.

Open In App