如何从字符串数组中获取特定字符串

本文关键字:字符串 获取 数组 | 更新日期: 2023-09-27 17:58:37

我有一个Abc'r'n123PQR'r'n456的字符串数组,我从容器中得到它,我只想要使用webdriver的"ABC""PQR。如何获得它?

我使用了Split("'r'n"),但它只返回2个字符串"ABC"123PQR'r'n456

有什么建议吗?语言是使用Web驱动程序的C#。

如何从字符串数组中获取特定字符串

要按顺序获取单词,请尝试此

string sequence = "Abc'r'n123PQR'r'n456";
string[] itemsArray = sequence.Split(new char[] { ''r', ''n' },
    StringSplitOptions.RemoveEmptyEntries);
List<string> itemsList = new List<string>(itemsArray);
List<string> itemsListFind = itemsList.FindAll(
    delegate(string item) 
    { 
        return 
            item.ToUpper().Contains("ABC") ||
            item.ToUpper().Contains("PQR"); 
    });
string[] result = itemsListFind.ToArray();

结果是:

{string[2]}
    [0]: "Abc"
    [1]: "123PQR"

是吗?

根据建议的测试用例进行编辑:

        Regex reg = new Regex(@"([A-Za-z].*)", RegexOptions.Singleline);
        // Split the string on line breaks, removing parts consisted of only numbers
        // ... The return value from Split is a string array.
        string[] lines = Regex.Split(Regex.Replace(value, @"(?<='n)(.['d]*)(?='r)|(?<='n)(.['d]*)(?=$)|(?<=^)(.['d]*)(?='r)", string.Empty), "'r'n");
        //Removing empty spaces with Linq
        lines = lines.Where(x => !string.IsNullOrEmpty(x)).ToArray(); 
        for (int i = 0; i < lines.Count(); i++) 
        {
            Match m = reg.Match(lines[i]);
            if (m.Success)
            {
                lines[i] = m.Value;
            }
            Console.WriteLine(lines[i]);
        }
    }