【问题标题】:Trying to search two strings in a document in the same line [closed]试图在同一行的文档中搜索两个字符串[关闭]
【发布时间】:2021-12-25 19:01:19
【问题描述】:
            WebClient wb = new WebClient();
            string License = wb.DownloadString("DocumentSite");
            if (License.Contains(LK.Text + UN.Text))

我想逐行搜索,而不是整个文档

【问题讨论】:

标签: c# wpf winforms


【解决方案1】:

您实际上可以为此使用正则表达式。每个字符串的正向前瞻(以任意数量的字符为前缀)并锚定到行首就足够了

var regex = "^" + string.Concat(new[]{LK.Text, UN.Text}.Select(s => $"(?=.*?{s})"));
if (Regex.Match(licence, regex, RegexOptions.Multiline))
{
....

【讨论】:

    【解决方案2】:

    您可以使用StringReader 逐行遍历字符串。

    private async Task ReadLicenseAsync(string license)
    {
      using var textReader = new StringReader(license);
      string line = string.Empty;
      while ((line = await textReader.ReadLineAsync()) != null)
      {
        // TODO::Handle line
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 1970-01-01
      相关资源
      最近更新 更多