在C#中手动拆分字符串 [英] Manual string split in C#
本文介绍了在C#中手动拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我试图操纵一个字符串:
In my code, I am attempting to manipulate a string:
Some text - 04.09.1996 - 40-18
我想将其分为三个子字符串:某些文本
, 1996年4月9日
和 40-18
.
I'd like to split this into three substrings: Some text
, 04.09.1996
, and 40-18
.
当我使用带有连字符作为分隔符的 Split
方法时,返回值是由四个字符串组成的数组: Some text
, 04.09.1996
, 40
和 18
.我如何才能使该代码如上所述工作?
When I use the Split
method with a hyphen as a separator, the return value is an array of four strings: Some text
, 04.09.1996
, 40
, and 18
. How can I make this code work as described above?
推荐答案
您应该在-
周围用空格分开:
You should just split with spaces around -
:
.Split(new[] {" - "}, StringSplitOptions.RemoveEmptyEntries);
请参见 C#演示
var res = "Some text - 04.09.1996 - 40-18".Split(new[] {" - "}, StringSplitOptions.RemoveEmptyEntries);
foreach (var s in res)
Console.WriteLine(s);
结果:
Some text
04.09.1996
40-18
这篇关于在C#中手动拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文