简体   繁体   English

c#使用字符串而不是char作为分隔符来拆分字符串

[英]c# splitting strings using string instead of char as seperator

I have string string text = "1.2788923 is a decimal number. 1243818 is an integer. "; 我有字符串string text = "1.2788923 is a decimal number. 1243818 is an integer. "; Is there a way to split it on the commas only ? 有没有办法将它拆分为逗号? This means to split on ". " but not on '.' 这意味着拆分". "而不是'.' . When I try string[] sentences = text.Split(". "); 当我尝试string[] sentences = text.Split(". "); I get method has invalid arguments error.. 我得到方法有无效的参数错误..

Use String.Split Method (String[], StringSplitOptions) to split it like: 使用String.Split Method (String[], StringSplitOptions)将其拆分为:

string[] sentences = text.Split(new string[] { ". " },StringSplitOptions.None);

You will end up with two items in your string: 您最终会在字符串中输入两个项目:

1.2788923 is a decimal number
1243818 is an integer

你可以使用Regex.Split

string[] parts = Regex.Split(text, @"\. ");

The string(s) that you splitting on are expected to be in a separate array. 您拆分的字符串应位于单独的数组中。

String[] s = new String[] { ". " };
String[] r = "1. 23425".Split(s, StringSplitOptions.None);

Use Regular Expressions 使用正则表达式

public static void textSplitter(String text)
   {

  string pattern = "\. ";           

String[] sentences = Regex.Split(text, pattern);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM