Coding Villa

Split and Join strings in C#

Author : Hassan Shafiq

Share with Friends
Views : 433
Ratings : 0
Bookmarks : 0
Farourities : 0

Description


In this sample article you will learn that how we can split and Join strings in C#.


Advertisements


 

 

String.split Method

 

To separate substrings in the string array, we often use delimiters such as, comma, semi colon, space, slash etc. We use split method to separate strings from these types of delimiters like:

 

 

mystring.Split(‘ ‘);

 

 

String.Join Method

 

String.Join method is used to join more than one string in a single string. It takes a string array and uses a separator string which is to be placed in between every element of the string array.

 

Syntax

 

String.Join(".", Strings[]);

 

 

 

The following sample code will show the splitting and joining of string. Here we have a string "If He Works Hard, he will succeed". The string.Split () will separate string from spaces, comma and dot and this output will be shown “If He Work Hard he will succeed”. The string.Join() will place a hyphen between every element of the string and the following output will be shown “If-He-Works-Hard- - he-will-succeed”. 

 

 

Code

 

 

using System;

using System.Text;

 

public static void Main()

        {

 

 

string mystring = "If He Works Hard, he will succeed.";

             string[] s = mystring.Split(' ',',','.');

             foreach (string space in s)

             Console.WriteLine(space);

 

 

           string whole = String.Join(" - ", s);

             Console.WriteLine("Result of join: "+ whole);

                Console.ReadLine();

 

        }

 



Article Tags
Share with Friends

Comments




Leave a Reply

Name (required)  
Mail (will not be published) (required)   
 
Enter This Code
Captcha
 

Advertisements

Article Categories

.NET


Databases


Mobile Development


Operating Systems


Web Development


Coding Villa on Facebook