我有一个基于空格分割的字符串数组。现在,根据我的要求,我必须得到其内容中包含'/'的数组元素,但我无法得到它。我不明白如何实现它。
以下是我尝试过的代码:
string[] arrdate = currentLine.Split(' ');
如何获取由/
组成的数组元素?
答案 0 :(得分:6)
试试这个:
string[] arrdate = currentLine.Split(' ');
var dateItems = arrdate.Where(item => item.Contains("/")).ToArray()
答案 1 :(得分:1)
foreach (string s in arrdate)
{
if (s.contains("/"))
{
//do something with s like add it to an array or if you only look for one string assign it and break out of the loop.
}
}
答案 2 :(得分:0)
如果你只想获得一个项目,那么试试这个
// split on the basis of white space
string[] arrdate = currentLine.Split(' ');
// now find out element with '/' using lambda
string item = arrdate.Where(item => item.Contains("/")).FirstOrDefault();
// if you don't want to use lambda then try for loop
string item;
for(int i = 0; i < arrdate.Length; i++)
{
if(arrdate[i].Contains("/"))
{
item = arrdate[i]
}
}
答案 3 :(得分:-1)
// split on the basis of white space
string[] arrdate = currentLine.Split(' ');
// now find out element with '/' using lambda
string item = arrdate.Where(item => item.Contains("/")).FirstOrDefault();
// if you don't want to use lambda then try for loop
string item;
for(int i = 0; i < arrdate.Length; i++)
{
if(arrdate[i].Contains("/"))
{
item = arrdate[i]
}
}