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

时间:2016-07-26 18:25:26

标签: c#

我有一个Abc\r\n123PQR\r\n456的字符串数组,我从容器中获取,我只想"ABC""PQR"使用webdriver。怎么弄?

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

有什么建议吗? 语言是使用webdriver的C#。

2 个答案:

答案 0 :(得分:1)

按顺序获取单词,试试这个

$trialEnd = new DateTime();
    $trialEnd->setTimestamp(time()+120);

    $user = Users::find($this->user()['user_id']);
    $user->subscription($stripe_plan['stripe_plan'])->trialFor($trialEnd)->create($data['stripeToken'], [
        'email' => $this->user()['email']
    ]);
    $user->save();

结果是:

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();
是吗?

答案 1 :(得分:0)

编辑,根据提出的测试用例:

        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]);
        }
    }