Loading ...

Using Split function

Who is online?  0 guests and 0 members
home  »  articles  »  Using Split function

Using Split function

(1970)
0
/5
Avg: 0/5: (0 votes)
Published: 7/26/2009 by  mausumi dutta

CSharp string Split function returns an array of String containing the substrings delimited by the given System.Char array.

Parameters:separator - the given delimiter

Returns:An array of Strings delimited by one or more characters in separator

code:

using System;
 using System.Text;

 namespace StringSearch
 {
    public class TesterStringSearch1
    {
       public void Run()
       {
           // create some strings to work with
           string s1 = "I,AM,MAUSUMI,at,TCS.";

           // constants for the space and comma characters
           const char Space = ' ';
           const char Comma = ',';

           // array of delimiters to split the sentence with
           char[] delimiters = new char[]
          {
              Space,
              Comma
          };

           // use a StringBuilder class to build the
           // output string
           StringBuilder output = new StringBuilder();
           int ctr = 1;

           // split the string and then iterate over the
           // resulting array of strings
           foreach (string subString in s1.Split(delimiters))
           {
               // AppendFormat appends a formatted string
               output.AppendFormat("{0}: {1}\n",ctr++,subString);
           }
           Console.WriteLine(output);

       }

       [STAThread]
       static void Main()
       {
          TesterStringSearch1 t = new TesterStringSearch1();
          t.Run();
       }
    }
 }

Output:

I

AM

MAUSUMI

at

TCS

 

Comments (no comments yet)

  • Name:*
  • Email:*
  • Website:
Type the characters you see in the image *

Confirm

Product Spotlight

ASP.NET Hosting Spotlight

Most Recent Articles

 

Product Spotlight

ASP.NET Hosting Spotlight

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?