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...

C#中split的使用

  • 2009-02-11 17:36
  • 17KB
  • 下载

c# split函数使用

vs2005使用通过: using System; using System.Collections.Generic; using System.Text; namespace Tests...

C#中使用Split分隔字符串的技巧

1、使用String.Split单字符分割 String.Split方法 命名空间:System 程序集:mscorlib(在 mscorlib.dll 中) 返回的字符串数组包含...

C#中使用Split分隔字符串的技巧

C#中使用Split分隔字符串的技巧 作者:icech整理 发布时间:2009-8-18 16:12:13 文章来源:西部E网 在分割字符串的时候我们常常会用到Split,下面我们就来学习一下分割字符...

C#控制台基础 使用split分离字符串出现了很多的空,怎么除去空

镇场诗:慈心积善融学习,技术誓为大家学。善心速造多好事,前人栽树后乘凉。我今于此写经验,愿见文者得启发。 ——————————————————————————————————————————————...

C#中split使用方法

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

C# split 函数的用法

  • 2015-10-27 16:40
  • 16KB
  • 下载

C#中Split用法

  • 2012-05-04 14:42
  • 20KB
  • 下载

C#的split用法

转载自:http://www.cnblogs.com/yugen/archive/2010/08/18/1802781.html 1、用字符串分隔:  using System.Text....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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