Home » .Net

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

String.Split() Method of String Class in C#: Here, we will learn how to split a string in multiple strings using String.Split() method in C#?

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?

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?





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.