字符串数组是否还有SubString或Split函数?

时间:2013-11-29 19:06:29

标签: c# .net string

当我有字符串时,我可以使用Substring()Split()函数,并将结果分别存储到stringstring[]

我想在字符串数组下面处理与字符串相同的方法。

+ ---------------------------------------------- - +
| $ | $ |名称| $ | $ | NIC | $ | $ |性别|
+ ----------------------------------------------- + < / p>

怎么可能?

5 个答案:

答案 0 :(得分:3)

以下是获取NIC和&之间的内容的示例性别:

var betweenNicAndGender = stringArray.SkipWhile(s => !s.Equals("NIC"))
                                     .Skip(1) // skip NIC
                                     .TakeWhile(s => !s.Equal("Gender"));

答案 1 :(得分:2)

最佳解决方案取决于您实际尝试解决的问题以及数据的大小。看一下标准LINQ方法(Skip,Take)和Array.IndexOf当然不会有什么坏处。

答案 2 :(得分:1)

| $ | $ | Name | $ | $ |NIC| $ | $ |Gender |
     

我想输出Name和NIC之间的内容以及NIC和Gender之间的内容

这样的事情:

int firstIndex  = stringArray.IndexOf("Name");
int secondIndex = stringArray.IndexOf("NIC");

var result = input.Skip(firstIndex + 1).Take(secondIndex - firstIndex)

添加错误检查取决于您。

答案 3 :(得分:1)

public string[] SubstringArray(string[] inputArray, int index, int count)
{ 
    string[] result = new string[count];

    for(int i = index; i < count+index; i++)
        result[i - index] = inputArray[i];

    return result;
}

public List<string[]> SplitStringArray(string[] inputArray, string splitter)
{
    List<string[]> splitResult = new List<string[]>();
    List<string> tempString = new List<string>();

    foreach(string x in inputArray)
    {
        if(x == splitter)
        {
            splitResult.add(tempString.ToArray());
            tempString = new List<string>();
        }
        else
        {
            tempString.add(x);
        }
    }
    return splitResult;
}  

答案 4 :(得分:0)

您可以使用

string.Join("", yourarrayName);