[C#] 利用Split作字串分割

  • 7844
  • 0
  • C#
  • 2017-07-28

摘要:C# 利用Split作字串分割

有時候我們會直接塞一大筆資料到一個欄位。

因此在取出時需要做一些分割。並整理資料內容。查了文件,可以利用Split 來完成,也練習了一下,並紀錄下來。

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

namespace ClassLibrary_Split
{
    public class Class1
    {
        static void Main(string[] args)
        {
            string X = "145,213,178,546,ad";//宣告字串
            char[] crst = new char[] { ',' }; // 使用char 陣列,並且傳回子字串陣列
            foreach (string str in X.Split(crst))
            {
                //判斷字串內容是否相符
                string Y = "145";
                string Z = "178";
                if (str == Y)
                {
                    System.Console.WriteLine(str + "=" + Y + "=TURE");
                }
                if (str == Z)
                {
                    System.Console.WriteLine(str + "=" + Z + "=TURE"); 
                }
                else
                {
                    System.Console.WriteLine("字串不相等");
                }
            }
            Console.Read();
        }
    }
}

參閱文件:http://msdn.microsoft.com/zh-tw/library/ms228362(v=vs.80).aspx

 

 

水滴可成涓流,涓流可成湖泊大海。
汲取累積知識,將知識堆積成常識;將常識探究成學識;將學識簡化為知識;授人自省。