string.Split()

 1 using System;
 2 
 3 namespace splitTest
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             string testStr = "oDo,you,,love,,,,meo";
10             string[] subStr = testStr.Split(new char[] { 'o', ',' });
11             foreach(string item in subStr)
12             {
13                 if(item == string.Empty)
14                 {
15                     Console.WriteLine("我是空字符串");
16                 }
17                 else
18                 {
19                     Console.WriteLine(item);
20                 }
21             }
22             Console.WriteLine("subStr字符数组的个数是{0}",subStr.Length);
23             Console.WriteLine("======================================================");
24             testStr = "abciiideiiiifghiijklm";
25             subStr = testStr.Split(new string[] { "ii" },StringSplitOptions.None);
26             foreach (string item in subStr)
27             {
28                 if (item == string.Empty)
29                 {
30                     Console.WriteLine("我是空字符串");
31                 }
32                 else
33                 {
34                     Console.WriteLine(item);
35                 }
36             }
37             Console.WriteLine("subStr字符数组的个数是{0}", subStr.Length);
38             Console.Read();
39         }
40     }
41 }

输出结果

我是空字符串
D
我是空字符串
y
u
我是空字符串
l
ve
我是空字符串
我是空字符串
我是空字符串
me
我是空字符串
subStr字符数组的个数是13
======================================================
abc
ide
我是空字符串
fgh
jklm
subStr字符数组的个数是5

Split()以指定的某个字符或某些字符把特定字符串分割成若干子字符串;

public static string[] Split(参数);

第10行代码运行结束,subStr引用指向的堆中结构如下:

Empty D Empty y u Empty l ve Empty Empty Empty me Empty

  

猜你喜欢

转载自www.cnblogs.com/maoshuyi/p/9926510.html