Browse Source

split的用法

master
QYYEE 5 years ago
parent
commit
8ea2031f82
1 changed files with 46 additions and 0 deletions
  1. 46
    0
      后端/C#基础/Split的用法.md

+ 46
- 0
后端/C#基础/Split的用法.md View File

1
+> 字符串的处理离不开split方法
2
+- 对单个字符进行分割(注意这里是字符,不是字符串,故只能用单引号‘’) 
3
+```
4
+string s=abcdeabcdeabcde;
5
+string[] sArray=s.Split('c') ;
6
+foreach(string i in sArray)
7
+Console.WriteLine(i.ToString());
8
+
9
+输出结果为:ab deab deab de
10
+```
11
+- 对多个字符进行分割
12
+```
13
+string s="abcdeabcdeabcde";
14
+string[] sArray1=s.Split(new char[3]{'c','d','e'}) ;
15
+foreach(string i in sArray1)
16
+Console.WriteLine(i.ToString());
17
+
18
+输出结果为:ab ab ab
19
+```
20
+- 使用正则表达式
21
+```
22
+using System.Text.RegularExpressions;
23
+string str="aaajsbbbjsccc";
24
+string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
25
+foreach (string i in sArray) Response.Write(i.ToString());
26
+
27
+输出结果为:aaa bbb ccc
28
+```
29
+
30
+- 项目中使用到split方法
31
+
32
+```
33
+  model.SceneName = scene.Name.Split(',')[0];
34
+                model.TriggerMode = GetTriggerName(scene.Name.Split(',')[1]);
35
+```
36
+```
37
+string[] kg= kaiguanIds.Split(',');
38
+ foreach (var item in kg)
39
+ {
40
+     var scenekaiguan =new SceneKaiGuanItemViewModel();
41
+     var kaiguanId = item.Split('_')[0];
42
+     var kaiguanStatus = item.Split('_')[1];
43
+ }
44
+[0]第一个元素
45
+[1]第二个元素
46
+```

Loading…
Cancel
Save