Home > Back-end >  Trying to search two strings in a document in the same line
Trying to search two strings in a document in the same line

Time:12-26

            WebClient wb = new WebClient();
            string License = wb.DownloadString("DocumentSite");
            if (License.Contains(LK.Text   UN.Text))

I want to search line by line and not the whole document

CodePudding user response:

You can use the StringReader to step through the string line by line.

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

CodePudding user response:

You can actually use Regex for this. A positive lookahead for each string (prefixed by any number of characters), and anchored to the beginning of a line, should suffice

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