鼎鼎知识库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

15.Split的用法.md 1.2KB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. 输出结果为:ab deab deab de
  9. ```
  10. - 对多个字符进行分割
  11. ```
  12. string s="abcdeabcdeabcde";
  13. string[] sArray1=s.Split(new char[3]{'c','d','e'}) ;
  14. foreach(string i in sArray1)
  15. Console.WriteLine(i.ToString());
  16. 输出结果为:ab ab ab
  17. ```
  18. - 使用正则表达式
  19. ```
  20. using System.Text.RegularExpressions;
  21. string str="aaajsbbbjsccc";
  22. string[] sArray=Regex.Split(str,"js",RegexOptions.IgnoreCase);
  23. foreach (string i in sArray) Response.Write(i.ToString());
  24. 输出结果为:aaa bbb ccc
  25. ```
  26. - 项目中使用到split方法
  27. ```
  28. model.SceneName = scene.Name.Split(',')[0];
  29. model.TriggerMode = GetTriggerName(scene.Name.Split(',')[1]);
  30. ```
  31. ```
  32. string[] kg= kaiguanIds.Split(',');
  33. foreach (var item in kg)
  34. {
  35. var scenekaiguan =new SceneKaiGuanItemViewModel();
  36. var kaiguanId = item.Split('_')[0];
  37. var kaiguanStatus = item.Split('_')[1];
  38. }
  39. [0]第一个元素
  40. [1]第二个元素
  41. ```