C#将字符串变量存储到文本文件.txt中
本文关键字:文本 文件 txt 存储 字符串 变量 | 更新日期: 2023-09-27 17:58:13
-
如何将字符串变量的内容存储到文本文件中?
-
如何在字符串变量中搜索特定文本,例如查找单词book是否在字符串中?
要将文件保存为文本,可以执行以下操作:
System.IO.File.WriteAllText("C:'your_path'your_file", Your_contents);
在字符串中搜索内容:
var position = Your_string.IndexOf("Book");
如果position等于-1,那么您要搜索的内容就不存在。
如果你真的在哪里找到这些信息,那么很简单:
System.IO.File.WriteAllText(myPathToTheFile, myStringToWrite);
要在另一个字符串中找到一个字符串,只需执行以下操作:
myString.Contains(someOtherString); // boolean
myString.IndexOf(someOtherString); // find the 0 based index of the target string
File.WriteAllText("MyFile.txt", myString); // Write all string to file
var wordBookIndex = myString.IndexOf("book"); // If the string is found, index != -1
System.IO命名空间有各种用于文件(和其他)IO的方法和类,这个方法和类可以很容易地满足您的目的:
http://msdn.microsoft.com/en-us/library/system.io.file.writealltext.aspx
至于在单个字符串中搜索,请使用string.IndexOf:
http://msdn.microsoft.com/en-us/library/system.string.indexof.aspx
回答一个问题
使用System.IO命名空间,因为没有类可用于在文件中编写文本
回答2个问题
您可以使用正则表达式或使用IndexOf函数在字符串