如何获取包含'/';在字符串数组中

本文关键字:字符串 数组 何获取 获取 包含 | 更新日期: 2023-09-27 18:19:29

我有一个字符串数组,我根据空白分割了它。现在,根据我的要求,我必须获得其内容中包含"/"的数组元素,但我无法获得它。我不知道如何实现它。

这是我尝试过的代码:

 string[] arrdate = currentLine.Split(' ');

如何获取由/组成的数组元素?

如何获取包含'/';在字符串数组中

试试这个:

 string[] arrdate = currentLine.Split(' ');
 var dateItems = arrdate.Where(item => item.Contains("/")).ToArray()
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.
   }
}

如果你只想得到一个项目,那么试试这个

// 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]
}
}
// 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]
}
}