C#将字符串变量存储到文本文件.txt中

jay*_*arp 9 c# string text store text-files

  1. 如何将字符串变量的内容存储到文本文件中?

  2. 如何在字符串变量中搜索特定文本,例如查找单词簿是否在字符串中?

kem*_*002 19

要将文件保存为文本,您可以执行以下操作:

System.IO.File.WriteAllText("C:\your_path\your_file", Your_contents);
Run Code Online (Sandbox Code Playgroud)

要搜索字符串中的内容:

var position = Your_string.IndexOf("Book");
Run Code Online (Sandbox Code Playgroud)

如果position等于-1,那么你要搜索的内容就不存在了.


Teo*_*gul 6

File.WriteAllText("MyFile.txt", myString); // Write all string to file
var wordBookIndex = myString.IndexOf("book"); // If the string is found, index != -1
Run Code Online (Sandbox Code Playgroud)


Tej*_*ejs 6

如果有机会,你实际上难以找到这些信息的位置,它很简单:

System.IO.File.WriteAllText(myPathToTheFile, myStringToWrite);
Run Code Online (Sandbox Code Playgroud)

要在另一个字符串中查找字符串,您只需执行以下操作:

myString.Contains(someOtherString); // boolean
myString.IndexOf(someOtherString); // find the 0 based index of the target string
Run Code Online (Sandbox Code Playgroud)