C#split的使用

原创 2006年05月18日 21:28:00

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

namespace SplitShow_String
{
    class Program
    {
       static void Main(string[] args)
        {
            string s1 = ",ONE,,TWO,,,THREE,,";
            string s2 = "[stop]" +
                        "ONE[stop][stop]" +
                        "TWO[stop][stop][stop]" +
                        "THREE[stop][stop]";
            char[] charSeparators = new char[] {','};
            string[] stringSeparators = new string[] {"[stop]"};
            string[] result;
        // ------------------------------------------------------------------------------
        // Split a string delimited by characters.
        // ------------------------------------------------------------------------------
            Console.WriteLine("1) Split a string delimited by characters:/n");

        // Display the original string and delimiter characters.
            Console.WriteLine("1a )The original string is /"{0}/".", s1);
            Console.WriteLine("The delimiter character is '{0}'./n",
                               charSeparators[0]);

        // Split a string delimited by characters and return all elements.
            Console.WriteLine("1b) Split a string delimited by characters and " +
                              "return all elements:");
            result = s1.Split(charSeparators, StringSplitOptions.None);
            Show(result);

        // Split a string delimited by characters and return all non-empty elements.
            Console.WriteLine("1c) Split a string delimited by characters and " +
                              "return all non-empty elements:");
            result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
            Show(result);

        // Split the original string into the string and empty string before the
        // delimiter and the remainder of the original string after the delimiter.
            Console.WriteLine("1d) Split a string delimited by characters and " +
                              "return 2 elements:");
            result = s1.Split(charSeparators, 2, StringSplitOptions.None);
            Show(result);

            // Split the original string into the string after the delimiter and the
            // remainder of the original string after the delimiter.
                Console.WriteLine("1e) Split a string delimited by characters and " +
                                  "return 2 non-empty elements:");
                result = s1.Split(charSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
                Show(result);

            // ------------------------------------------------------------------------------
            // Split a string delimited by another string.
            // ------------------------------------------------------------------------------
                Console.WriteLine("2) Split a string delimited by another string:/n");

            // Display the original string and delimiter string.
                Console.WriteLine("2a) The original string is /"{0}/".", s2);
                Console.WriteLine("The delimiter string is /"{0}/"./n", stringSeparators[0]);

            // Split a string delimited by another string and return all elements.
                Console.WriteLine("2b) Split a string delimited by another string and " +
                                  "return all elements:");
                result = s2.Split(stringSeparators, StringSplitOptions.None);
                Show(result);

            // Split the original string at the delimiter and return all non-empty elements.
                Console.WriteLine("2c) Split a string delimited by another string and " +
                                  "return all non-empty elements:");
                result = s2.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
                Show(result);

            // Split the original string into the empty string before the
            // delimiter and the remainder of the original string after the delimiter.
                Console.WriteLine("2d) Split a string delimited by another string and " +
                                  "return 2 elements:");
                result = s2.Split(stringSeparators, 2, StringSplitOptions.None);
                Show(result);

            // Split the original string into the string after the delimiter and the
            // remainder of the original string after the delimiter.
                Console.WriteLine("2e) Split a string delimited by another string and " +
                                  "return 2 non-empty elements:");
                result = s2.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries);
                Show(result);

                Console.ReadLine();
    }

    // Display the array of separated strings.
        public static void Show(string[] entries)
        {
            Console.WriteLine("The return value contains these {0} elements:", entries.Length);
            foreach (string entry in entries)
                {
                Console.Write("<{0}>", entry);
                }
            Console.Write("/n/n");
            }
        }
}

C#中Split详解

一、String.Split方法提供了如下6个重载函数: 名称 说明 String.Split (Char[]) 返回包含此实例中的子字符串(由指定 C...
  • czw2010
  • czw2010
  • 2012年08月11日 19:27
  • 43628

C#实现字符串按多个字符采用Split方法分割

C#实现字符串按多个字符采用Split方法分割
  • educast
  • educast
  • 2010年09月29日 13:55
  • 5372

C#中 Split的用法 及Split 分隔符转义

1、用字符串分隔:  using System.Text.RegularExpressions; string str="aaajsbbbjsccc"; string[] sArray=Regex.S...
  • lanximu
  • lanximu
  • 2013年12月13日 00:23
  • 3810

c#中Split 分离字符以及空格消除方法

c#中Split函数 1        split几种分离方法 1)用字符串分隔: using System.Text.RegularExpressions; string str="aaaj...

巧用C# Split()函数获取SQL语句中操作字段

这是前天工作时要求的,将SQL语句的操作字段获取出来挂在树节点上,感觉这个函数以后还有可能会用到,特此总结一下,函数中没有实现Select *的操作,只要添加判断条件即可。 工具函数:Split()...

C#中String.Split方法使用详解

String.Split 方法有6个重载函数: 1) public string[] Split(params char[] separator) 2) public string[] Split...

C#中split函数的用法

Split 1、用字符串分隔:  using System.Text.RegularExpressions; string str="aaajsbbbjsccc"; string[] ...
  • GIS_YES
  • GIS_YES
  • 2012年07月19日 09:16
  • 2516

C# SplitContainer 控件详细用法

引自:http://blog.sina.com.cn/s/blog_75c55a1501011280.html 1.可以将 Windows 窗体 SplitContainer 控件看作是一个复合体,...
  • zcg1041
  • zcg1041
  • 2017年01月03日 14:16
  • 6107

C#中split用法

1、用字符串分隔: using System.Text.RegularExpressions; string str="aaajsbbbjsccc";string[] sArray=Regex.Spl...

C# 之 字符串截取--Split

上一篇博客《C# 之 字符串截取--Substring》 介绍了Substring函数,同时实现将"所属机构名称/教师姓名/课程类型/课程名称"中的所属机构名称,教师姓名,课程类型,课程名称分别截取出...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C#split的使用
举报原因:
原因补充:

(最多只允许输入30个字)