Menu
Topics Index
...
`

String splitting using delimiter


If you need explanation Read this topic

If you need Answer Take test on this topic

String splitting using delimiter
What will be the output of the following program?
public class SplitStringByUsingDelimiter
{
    public static void main(String args[])
    {
        String str = "Ram-Robert-Rahim";
        String[] temp;
        String delimiter = "-";
        temp = str.split(delimiter);

        for(int i = 0; i < temp.length ; i++)
        {
            System.out.print(temp[i] + " ");
            str = "Ram.Robert.Rahim";
            delimiter = "\\.";
            temp = str.split(delimiter);
        }
        System.out.println();

        for(int i = 0; i < temp.length ; i++)
        {
            System.out.print(temp[i] + " ");
            temp = str.split(delimiter, 2);
            System.out.println();

            for(int j = 0; j < temp.length ; j++)
            {
                System.out.print(temp[i] + " ");
            }
            System.out.println();
        }
    }
}

A. Ram Robert Rahim 
Ram 
Ram Ram 
Robert Robert
Rahim Rahim
B. Ram Robert Rahim 
Ram 
Ram Ram 
Robert.Rahim 
Robert.Rahim Robert.Rahim
C. Ram Robert Rahim 
Ram 
Ram Ram Ram 
Robert 
Robert Robert Robert 
Rahim 
Rahim Rahim Rahim
D. Compilation Error
E. Runtime 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.

a

Posted by Shubham Bansal    2015-02-06 14:47:32


ans b.str is initialized with string value. str .split() Returns an String array that decomposes the invoking string into parts on encountering regular expression specified by delimiter.str.split(delimiter, int value) Returns an String array that decomposes the invoking string into parts on encountering regular expression specified by delimiter. The number of pieces are specified by max. If the max is negative or zero, then the invoking string is fully decomposed. If the max is positive, then the last returned array contains the remaining of the invoking string

Posted by Maheshwari Natarajan    2015-02-06 20:22:40


Ans is B here we have the String as "Ram-Robert-Rahim".in this string we are separating the words using the - ,next we are spliting the string using the split() by passing delimiter as "-"..here split() method returns the array of Strings and we are storing in the temp array.

Execution:

in the first for loop we are traveling through the array every time we are printing the strings in the array and here we have another string "Ram.Robert.Rahim" here we are passing the "\\." which is not present so it wont split
next here we have nested loops here we are printing the strings in the array snd next we are calling the split() by passing the delimiter and integer..here int is to tell num of strings to be returned so it will return only 2 strings.

Posted by Uday Kumar    2015-02-09 03:49:50


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Shubham Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-09 05:10:14


© meritcampus 2019

All Rights Reserved.

Open In App