【发布时间】:2015-07-24 02:15:44
【问题描述】:
总结:我想将变量插入文本文件。我试着环顾四周,但无济于事。也许我正在使用错误的关键词进行搜索。我知道将变量插入字符串,很简单:
string str1 = "hello {0}";
string str2 = "world";
string final = string.Format(str1, str2); //Output should be "hello world"
按照同样的思路,我做了一个str1的文本文件,将文本文件加载到一个字符串变量中,重复上述过程。
str1.txt:
hello {0}
代码:
string str1 = System.IO.File.ReadAllText(@"..\..\str1.txt"); //Should load the text file as a string
string str2 = "world"
string final = string.Format(str1, str2); //Output should be "hello world"
现在,我在调用 string.Format 时收到“输入字符串格式不正确”错误,我不太确定原因。
编辑:我猜我没有提供足够的信息。对不起!我试图插入变量的文本文件是一个类的文本文件,其中 {0}、{1}、... 插入到各个点。
【问题讨论】:
-
调试并检查
str1的值
标签: c# string text-files