我正在尝试使用 String.Format 创建一个字符串并添加参数。但由于某种原因,我收到了错误 -
System.FormatException: Input string was not in a correct format.
这是我的代码
string queryPattern =
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
"PREFIX db: <http://dbpedia.org/ontology/> " +
"PREFIX prop: < http://dbpedia.org/property/> " +
"SELECT ?movieLink ?title ?genreLink ?genre ?releaseDate " +
"WHERE { " +
"?movieLink rdf:type db:Film; " +
"foaf:name ?title. " +
"OPTIONAL { ?movieLink prop:genre ?genreLink. " +
"?genreLink rdfs:label ?genre. " +
"FILTER(lang(?genre) = 'en') }. " +
"OPTIONAL{ ?movieLink <http://dbpedia.org/ontology/releaseDate> ?releaseDate }. " +
"{0}" +
"{1}" +
"FILTER(lang(?title) = 'en') " +
"}" +
"ORDER BY DESC(?releaseDate)" +
"{2}";
return String.Format(queryPattern, genreMatch, dateMatch, limit);
如有任何帮助,我们将不胜感激。
最佳答案
string.Format
使用花括号 ({}
) 来指示占位符。您的格式字符串无效,因为它包含另外几个花括号。
你需要通过将它们加倍来转义这些括号:
string s = "Teststring {{ {0} }}";
string r = string.Format(s, 42);
在 r 中的结果:
Teststring { 42 }
例如你的行
"WHERE { " +
应该是
"WHERE {{ " +
关于C# 系统.FormatException : Input string was not in a correct format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42959013/