Q:

Explain String.Split() method of String class in C# with an Example

belongs to collection: C# Basic Programs | String programs

0

Given a string and we have to split the string in multiple strings by separating it by the given delimiter.

String.Split()

String.Split() Method returns array of strings, and we pass separator (delimiter) in character format to split string. Separators may be ',' , ':' , '$' etc.

Syntax:

String[] String.Split(char ch);

Example:

    Input string: Hello,friends,how,are,you?

    If we separate string from comma (,) (it is also known as delimiter), 
    result is given below...

    Output:
    Hello 
    friends 
    how 
    are 
    you?

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int i=0;
            String str1;
            String []str2;

            Console.Write("Enter string : ");
            str1 = Console.ReadLine();

            str2 = str1.Split(',');

            Console.WriteLine("Separated strings are: ");

            for (i = 0; i < str2.Length; i++)
            {
                Console.WriteLine(str2[i] + "");
            }

        }
    }
  
}

Output

First run:
Enter string : Hello,friends,how,are,you? 
Separated strings are:
Hello 
friends 
how 
are 
you?

Second run:
Enter string : Hi there,how are you?
Separated strings are:
Hi there
how are you?

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | String programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to convert string into uppercase in C#?... >>
<< Explain LastIndexOf() method of String class with ...