如何拆分字符串并在C#中选择一个单词 [英] How to split string and pick a single word in C#
问题描述
你好我想拆分我的字符串并想在我的程序中只显示一个单词
假设这是我的字符串
string value = P 2016 -10-17,P 2016-10-18,P 2016-10-19,P 2016-10-20,P 2016-10-21;
和拆分之后我已收到(P 2016-10-17)字符串中的每一个我想只显示P并希望隐藏字符串中的日期
我尝试过:
string value = P 2016- 10-17,P 2016-10-18,P 2016-10-19,P 2016-10-20,P 2016-10-21;
char [] delimiters = new char [] {' ,'};
string [] parts = value .Split(delimiters,StringSplitOptions.RemoveEmptyEntries);
i使用上面的代码拆分它们成功完成拆分但我想在字符串
<上显示p blockquote>有两种方法可以做到这一点。
第一种方法是将字符串拆分两次:
string value = P 2016-10-17,P 2016-10-18,P 2016-10-19,P 2016-10-20,P 2016-10-21;
string [] parts = value .Split(' ,');
foreach ( string part in 部分)
{
string [] subParts = part.Split(' ');
if (subParts.Length > 0 )Console.WriteLine(subParts [ 0 ]);
}
第二种是使用正则表达式:
string value = P 2016-10-17,P 2016-10-18,P 2016-10-19,P 2016-10-20,P 2016-10-21;
MatchCollection matches = Regex.Matches( value , @ (小于= ^ |,?)(小于??前缀> +)(?= \s)跨度>);
foreach (匹配m 匹配)
{
控制台。的WriteLine(m.Value);
}
正则表达式有点复杂,但更灵活!
使用SubString和IndexOf,如下所示。
string newString = parts [0] .ToString()。Substring(0,parts [0] .ToString()。Length - 11);
您可以整理更精细的细节。
您可以使用Substring()
要做到这一点
以下内容应该有所帮助 -
for (< span class =code-keyword> int i = 0 ; i< parts.Length; i ++)
{
Console .WriteLine( {0},parts [i] .Substring( 0 ,input.Length-11)); // 11个字符用于排除日期和一个空格
// 您可以创建另一个数组来存储这些值
}
希望,它有帮助:)
hello i want to split my string and want to show only a single word in my program
suppose this is my string
string value = "P 2016-10-17,P 2016-10-18,P 2016-10-19,P 2016-10-20,P 2016-10-21";
and after split them i have received (P 2016-10-17) for each in the string here i want to just display only P and want to hide date in the string
What I have tried:
string value = "P 2016-10-17,P 2016-10-18,P 2016-10-19,P 2016-10-20,P 2016-10-21";
char[] delimiters = new char[] { ',' };
string[] parts = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
i have use above code for split them the split is successfully done but i want to display on p from the string
There are two ways to do this.
The first is to split the string twice:
string value = "P 2016-10-17,P 2016-10-18,P 2016-10-19,P 2016-10-20,P 2016-10-21"; string[] parts = value.Split(','); foreach (string part in parts) { string[] subParts = part.Split(' '); if (subParts.Length > 0) Console.WriteLine(subParts[0]); }
The second is to use a regex:
string value = "P 2016-10-17,P 2016-10-18,P 2016-10-19,P 2016-10-20,P 2016-10-21"; MatchCollection matches = Regex.Matches(value, @"(?<=^|,)(?<Prefix>.+?)(?=\s)"); foreach (Match m in matches) { Console.WriteLine(m.Value); }The regex is a little more complex, but a lot more flexible!
Make use of SubString and IndexOf as shown below.
string newString = parts[0].ToString().Substring(0, parts[0].ToString().Length - 11);
You can sort out the finer details.
You can useSubstring()
to do that
Something like following should help-
for(int i=0;i< parts.Length;i++) { Console.WriteLine("{0}",parts[i].Substring(0, input.Length-11)); // 11 characters to exclude the date and one whitespace in between //you can create another array to store these values now }
Hope, it helps :)
这篇关于如何拆分字符串并在C#中选择一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!