[C#]字串切割

摘要:字串切割

經常需要使用切割(Split)字串來做處理,有幾個處理的方式,以下範例在不使用Split方法怎麼切割字串,

這邊需瞭解String物件,對此就會更加了解。


            foreach (var tmp in str)
            {
                Console.WriteLine(tmp);
            }

Output :

2012-04-28_194733

字串的筆記中,有提到字串是由字元所組成,因此可以透過foreach將每個字元取出,不過這邊將空白也取出來了,此時當然可以寫一個判別式處理空白,不過不需要這麼辛苦造輪子,C#中提供我們一個更好用的方法,Split !

空白可以以 ’' 為切割字元,不過這邊也可以使用null


            string[] newsArr;
            newsArr = news.Split(null);
            foreach (string tmp in newsArr)
            {
                Console.WriteLine(tmp);
            }

Output :

2012-04-28_195343

不過有時候是沒有切割的條件,只單純想把字串轉為char陣列,此時可以用另一個方法(in91提供)

另外,如果上一個範例為中文,結果並非如同所需的樣子,除非字元中間使用空白,此時可以透過字串為字元集合所組成的特性,使用ToCharArray()


            var chineseArr = chinese.ToCharArray();

            foreach (var item in chineseArr)
            {
                Console.WriteLine(item.ToString());
            }

Output :

2012-04-28_195805

 

大多時候是不會去一個一個字切割中文,除非需將它放置陣列中,不然可以採用第一個範例的方式。

有關中文的切割,還有另一個方式,不過非String物件中的方法,可以採用正則表達式的方法來達到切割。

既然提到正則表達式就順便筆記一下,此方法的用途,當遇到有兩個條件以上,並且不想要建立切割字元集合時。


            
            string  pattern = "[0-9]";

            string [] ROC = Regex.Split(str, pattern, RegexOptions.None);

            foreach (var tmp in ROC)
            {
                Console.WriteLine(tmp);
            }

Output :

2012-04-28_200819

 

在上一個範例提到了切割字元集合,當要切割一篇文章時,會有許多符號,以下面新聞的範例來說,要一次切割完成,就必須有許多的切割條件,此時可以這麼做。


            @"
                    By the CNN Wire Staff (CNN) -- 
                    Syria is not complying with its pledge to withdraw troops and heavy weapons from towns,
                    the United Nations said Thursday, as it highlighted the need for the government to fully commit to a proposed peace plan.
                ";
            char[] separators = { ' ', '\n', '\r', '\t', ',', ',', '!' };
            string[] paperArr = paper.Split(separators);
            foreach (string tmp in paperArr)
            {
                Console.WriteLine(tmp);
            }

在此範例中展現了如何建立多個切割字元條件,不過沒有將輸出結果展現出來,因為英文新聞切割後會有許多空白,很占版面,此時會想到第二個範例使用null,不過會發現chat array沒辦法放null,當遇到此問題時,Split有一個多載,是否要傳回空白字元。


            @"
                    By the CNN Wire Staff (CNN) -- 
                    Syria is not complying with its pledge to withdraw troops and heavy weapons from towns,
                    the United Nations said Thursday, as it highlighted the need for the government to fully commit to a proposed peace plan.
                ";
            char[] separators = { ' ', '\n', '\r', '\t', ',', ',', '!' };
            string[] paperArr = paper.Split(separators, StringSplitOptions.RemoveEmptyEntries);
            foreach (string tmp in paperArr)
            {
                Console.WriteLine(tmp);
            }

Output :

2012-04-28_201612 

最後提到一個比較不相關的東西,當要取字串中某個段落,可以使用Substring(),不過也可以用ToCharArray()


            int startIndex = stringOfChinese.IndexOf('好');
            int endIndex = 3;
            char[] stringOfChineseArr = stringOfChinese.ToCharArray(startIndex, endIndex);
            foreach (char str in stringOfChineseArr)
            {
                Console.WriteLine(str);
            }

Output :

2012-04-28_203017

結語:

在本篇筆記中,是否有發現其實都一直再處理字元集合(陣列),字串由字元集合所組成也不斷在筆記中提起,相信瞭解這個原理,在處理字串時,會更得心應手。

 

 

MSDN Library Reference :

Split()方法

How to 系列

Reference :

c# 字串切割方式

請教字串切割問題

 

如文章有錯誤,煩請告知,新人發帖請多包涵

 

創用 CC 授權條款