鼎鼎知识库
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

15.Split的用法.md 1.2KB

字符串的处理离不开split方法

  • 对单个字符进行分割(注意这里是字符,不是字符串,故只能用单引号‘’) ``` string s=abcdeabcdeabcde; string[] sArray=s.Split(‘c’) ; foreach(string i in sArray) Console.WriteLine(i.ToString());

输出结果为:ab deab deab de

- 对多个字符进行分割

string s=“abcdeabcdeabcde”; string[] sArray1=s.Split(new char[3]{‘c’,’d’,‘e’}) ; foreach(string i in sArray1) Console.WriteLine(i.ToString());

输出结果为:ab ab ab

- 使用正则表达式

using System.Text.RegularExpressions; string str=“aaajsbbbjsccc”; string[] sArray=Regex.Split(str,“js”,RegexOptions.IgnoreCase); foreach (string i in sArray) Response.Write(i.ToString());

输出结果为:aaa bbb ccc ```

  • 项目中使用到split方法
  model.SceneName = scene.Name.Split(',')[0];
                model.TriggerMode = GetTriggerName(scene.Name.Split(',')[1]);
string[] kg= kaiguanIds.Split(',');
 foreach (var item in kg)
 {
     var scenekaiguan =new SceneKaiGuanItemViewModel();
     var kaiguanId = item.Split('_')[0];
     var kaiguanStatus = item.Split('_')[1];
 }
[0]第一个元素
[1]第二个元素