Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查字符串是否包含文件c的关键字#_C#_String_Contains_Keyword - Fatal编程技术网

C# 检查字符串是否包含文件c的关键字#

C# 检查字符串是否包含文件c的关键字#,c#,string,contains,keyword,C#,String,Contains,Keyword,我试图检查字符串是否包含文本文件中存在的关键字,但没有执行任何操作 如果你需要更多的信息,请问我 例如,我有一个keywords.txt,如下所示: example1 example2 example3 { "title":"new doc 4", "rotate":0, "sort_key":"new doc 4", "tag_ids":"", "doc_id":"ee4DM4Ly7CFBM3JFWAW60TUX", "co_to

我试图检查字符串是否包含文本文件中存在的关键字,但没有执行任何操作

如果你需要更多的信息,请问我

例如,我有一个keywords.txt,如下所示:

example1
example2
example3
{ 
     "title":"new doc 4",
     "rotate":0,
     "sort_key":"new doc 4",
     "tag_ids":"",
     "doc_id":"ee4DM4Ly7CFBM3JFWAW60TUX",
     "co_token":"",
     "p":"XQXLDEQyA2hf6BBfyXhSaUHL",
     "t":"1474932063",
     "c":"2",
     "updated":"1474932063"
}
我想看看这些关键字中是否有一个在json字符串中,如下所示:

example1
example2
example3
{ 
     "title":"new doc 4",
     "rotate":0,
     "sort_key":"new doc 4",
     "tag_ids":"",
     "doc_id":"ee4DM4Ly7CFBM3JFWAW60TUX",
     "co_token":"",
     "p":"XQXLDEQyA2hf6BBfyXhSaUHL",
     "t":"1474932063",
     "c":"2",
     "updated":"1474932063"
}
你可以在下面看到我的尝试 所以我希望你能帮我解决这个问题谢谢

if (json.Contains(File.ReadAllText(@"keywords.txt")))
{
    //My if logic
}
else
{
    //My else logic
}

若要检查文件中的任何单词是否包含在字符串中,则首先需要将文件中的任何单词放入数组中,然后需要字符串是否包含数组中的任何字符串

确定序列的任何元素是否存在或满足 条件

要做到这一点,请在下面尝试

var examples = File.ReadAllLines(@"keywords.txt");

if(examples.Any(x => json.Contains(x))
{
  //Your business logic
}
else
{
  //Your business logic
}

如果要检查精确匹配,可以尝试使用RegEx
IsMatch()
函数


若要检查文件中的任何单词是否包含在字符串中,则首先需要将文件中的任何单词放入数组中,然后需要字符串是否包含数组中的任何字符串

确定序列的任何元素是否存在或满足 条件

要做到这一点,请在下面尝试

var examples = File.ReadAllLines(@"keywords.txt");

if(examples.Any(x => json.Contains(x))
{
  //Your business logic
}
else
{
  //Your business logic
}

如果要检查精确匹配,可以尝试使用RegEx
IsMatch()
函数


下面的方法将有助于消除误报,它检查JSON字符串中是否存在特定的关键字,然后再进一步处理

只需执行
String.contains
可能会吞下这些误报和 证明是真的

输入文件关键字

keyword  //This will return as false
keyword1 //This will return true 
输入文件Json

{ 
   "title":"new doc 4",
   "rotate":0,
   "sort_key":"new doc 4",
   "tag_ids":"",
   "doc_id":"ee4DM4Ly7CFBM3JFWAW60TUX",
   "co_token":"",
   "p":"XQXLDEQyA2hf6BBfyXhSaUHL",
   "t":"1474932063",
   "c":"2",
   "updated":"keyword1"
}
var keyWordFilePath = @"keyWord.txt";
var jsonFilePath = @"json.txt";
var jsonString = File.ReadAllText(jsonFilePath);
var keywords = File.ReadAllText(keyWordFilePath).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

foreach (var key in keywords)
{

      //We are checking if json string contains key and also checking that it is only that definite key 
      var extractSpecificKey = jsonString.Substring(jsonString.IndexOf(key), key.Length+1);

      if (extractSpecificKey == key+ @"""")
      {

         Console.WriteLine($"Key {extractSpecificKey} present in json");

      }
     //False Positives
      else
      {
         Console.WriteLine($"Key {key} NOT present in json");
      }
}

Console.Read();

Main

{ 
   "title":"new doc 4",
   "rotate":0,
   "sort_key":"new doc 4",
   "tag_ids":"",
   "doc_id":"ee4DM4Ly7CFBM3JFWAW60TUX",
   "co_token":"",
   "p":"XQXLDEQyA2hf6BBfyXhSaUHL",
   "t":"1474932063",
   "c":"2",
   "updated":"keyword1"
}
var keyWordFilePath = @"keyWord.txt";
var jsonFilePath = @"json.txt";
var jsonString = File.ReadAllText(jsonFilePath);
var keywords = File.ReadAllText(keyWordFilePath).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

foreach (var key in keywords)
{

      //We are checking if json string contains key and also checking that it is only that definite key 
      var extractSpecificKey = jsonString.Substring(jsonString.IndexOf(key), key.Length+1);

      if (extractSpecificKey == key+ @"""")
      {

         Console.WriteLine($"Key {extractSpecificKey} present in json");

      }
     //False Positives
      else
      {
         Console.WriteLine($"Key {key} NOT present in json");
      }
}

Console.Read();

输出

Key keyword NOT present in json
Key keyword1" present in json

下面的方法将有助于消除误报,它检查JSON字符串中是否存在特定的关键字,然后再进一步处理

只需执行
String.contains
可能会吞下这些误报和 证明是真的

输入文件关键字

keyword  //This will return as false
keyword1 //This will return true 
输入文件Json

{ 
   "title":"new doc 4",
   "rotate":0,
   "sort_key":"new doc 4",
   "tag_ids":"",
   "doc_id":"ee4DM4Ly7CFBM3JFWAW60TUX",
   "co_token":"",
   "p":"XQXLDEQyA2hf6BBfyXhSaUHL",
   "t":"1474932063",
   "c":"2",
   "updated":"keyword1"
}
var keyWordFilePath = @"keyWord.txt";
var jsonFilePath = @"json.txt";
var jsonString = File.ReadAllText(jsonFilePath);
var keywords = File.ReadAllText(keyWordFilePath).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

foreach (var key in keywords)
{

      //We are checking if json string contains key and also checking that it is only that definite key 
      var extractSpecificKey = jsonString.Substring(jsonString.IndexOf(key), key.Length+1);

      if (extractSpecificKey == key+ @"""")
      {

         Console.WriteLine($"Key {extractSpecificKey} present in json");

      }
     //False Positives
      else
      {
         Console.WriteLine($"Key {key} NOT present in json");
      }
}

Console.Read();

Main

{ 
   "title":"new doc 4",
   "rotate":0,
   "sort_key":"new doc 4",
   "tag_ids":"",
   "doc_id":"ee4DM4Ly7CFBM3JFWAW60TUX",
   "co_token":"",
   "p":"XQXLDEQyA2hf6BBfyXhSaUHL",
   "t":"1474932063",
   "c":"2",
   "updated":"keyword1"
}
var keyWordFilePath = @"keyWord.txt";
var jsonFilePath = @"json.txt";
var jsonString = File.ReadAllText(jsonFilePath);
var keywords = File.ReadAllText(keyWordFilePath).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

foreach (var key in keywords)
{

      //We are checking if json string contains key and also checking that it is only that definite key 
      var extractSpecificKey = jsonString.Substring(jsonString.IndexOf(key), key.Length+1);

      if (extractSpecificKey == key+ @"""")
      {

         Console.WriteLine($"Key {extractSpecificKey} present in json");

      }
     //False Positives
      else
      {
         Console.WriteLine($"Key {key} NOT present in json");
      }
}

Console.Read();

输出

Key keyword NOT present in json
Key keyword1" present in json

在我的关键字文件中,每行有一个单词,例如:
example1example2
,我想看看在我的字符串json中,例如:
“title”:“newdoc 5”,“rotate”:0,“sort_key”:“newdoc 5”,“tag_id”:“doc_id”:“Xy67QdRhTR9XS159WLyCCTbK”,“co_token”:,“p”:“XadS23UUQbQRQt9gLPWDWTAQ”,“t”:“1474932060”,“c”:“1”,“更新”:“1474932061”
,我想看看这个字符串中是否有我的关键字文件中的一个词……我已经用更详细的内容编辑了我的主要消息shi@FanTaZy yf,我认为您需要在json中查找特定的关键字。例如,如果您的关键字包含
example
,而您的json字符串包含
example1
则会给您一个误报,您可以参考下面的解决方案!!在我的关键字文件中,每行有一个单词,例如:
example1example2
,我想看看在我的字符串json中,例如:
“title”:“newdoc 5”,“rotate”:0,“sort_key”:“newdoc 5”,“tag_id”:“doc_id”:“Xy67QdRhTR9XS159WLyCCTbK”,“co_token”:,“p”:“XadS23UUQbQRQt9gLPWDWTAQ”,“t”:“1474932060”,“c”:“1”,“更新”:“1474932061”
,我想看看这个字符串中是否有我的关键字文件中的一个词……我已经用更详细的内容编辑了我的主要消息shi@FanTaZy yf,我认为您需要在json中查找特定的关键字。例如,如果您的关键字包含
example
,而您的json字符串包含
example1
则会给您一个误报,您可以参考下面的解决方案!!如果关键字包含“keyword”,而json包含“keyword5”,这种方法将给出不正确的结果。你的想法是什么?答案如下:)@Clint,接得好,谢谢你帮助我改进我的答案。看看更新后的答案。@Prasad,不客气,我还没有在
Regex
中测试上述方法,我会确认一下:)如果关键字包含“关键字”,而json包含“关键字5”,这种方法将给出不正确的结果。你的想法是什么?答案如下:)@Clint,接得好,谢谢你帮助我改进我的答案。看看更新后的答案。@Prasad,不客气,我还没有在
Regex中测试上述方法,我会在测试后确认:)