Loading ...

Using Split function | CodeAsp.Net

Using Split function

  • 0
    /5
    Avg: 0/5: (0votes)
  • (0)
  • (3520)
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)

  • :*
  • :*
  • :
 *

Top articles

Quick Vote

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