简体   繁体   English

C#System.FormatException:输入字符串的格式不正确

[英]C# System.FormatException: Input string was not in a correct format

im trying to create a string using String.Format and add parameters. 我试图使用String.Format创建一个字符串并添加参数。 But for some reason, I'm receiving the error - 但是由于某种原因,我收到了错误消息-

System.FormatException: Input string was not in a correct format. System.FormatException:输入字符串的格式不正确。

Here is my code 这是我的代码

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);

Any help would be greatly appreciated. 任何帮助将不胜感激。

string.Format uses curly braces ( {} ) to indicate placeholders. string.Format使用大括号( {} )表示占位符。 Your format string is invalid because it contains another few curly braces. 您的格式字符串无效,因为它包含另外几个花括号。

You need to escape those braces by doubling them: 您需要通过将它们加倍来逃避这些大括号:

string s = "Teststring {{ {0} }}";
string r = string.Format(s, 42);

results in r: 结果为r:

Teststring { 42 }

So for example your line 所以例如你的线

"WHERE { " +

should be 应该

"WHERE {{ " +

You are not allowed to use curly braces in your format string other than for placeholders, thus " {0} " is OK, " {some text} " is not. 除占位符外,不允许在格式字符串中使用花括号,因此“ {0} ”可以,而“ {some text} ”则不能。 You can solve your problem by using double curly braces:" {{some text}} " 您可以使用大括号将您的问题解决:“ {{some text}}

Not sure how you are passing values to the parameters. 不确定如何将值传递给参数。 You can use the parameters directly in the string instead of using the string format function. 您可以直接在字符串中使用参数,而不是使用字符串格式功能。

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              }. " +

genreMatch +
dateMatch +
"FILTER(lang(?title) = 'en') " +
"}" +
"ORDER BY DESC(?releaseDate)" +
limit;

return queryPattern;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.FormatException:输入字符串的格式不正确。 c# - System.FormatException: Input string was not in a correct format. c# System.FormatException:输入字符串的格式不正确 - System.FormatException:Input string was not in a correct format System.FormatException:输入字符串的格式不正确 - System.FormatException: Input string was not in a correct format C# 错误 System.FormatException: &#39;输入字符串的格式不正确。&#39; 在数字输入上 - C# Error System.FormatException: 'Input string was not in a correct format.' on number input C# 下载方法 - System.FormatException: '输入字符串的格式不正确' - C# Download Method - System.FormatException: 'Input string was not in a correct format' System.FormatException: &#39;输入字符串的格式不正确。&#39; 在 C# 黄皮书教科书中 - System.FormatException: 'Input string was not in a correct format.' in C# Yellowbook textbook c# 如何修复:“未处理的异常:System.FormatException:输入字符串的格式不正确。” - c# How Can I Fix: “Unhandled Exception: System.FormatException: Input string was not in a correct format.” C# Excel combobox 中的空行(导致错误“System.FormatException:输入字符串格式不正确。”) - C# Excel empty rows in combobox (causing error “System.FormatException:Input string was not in a correct format.”) 类型为“ System.FormatException”的未处理的异常输入字符串的格式不正确 - Unhandled exception of type “System.FormatException” Input string was not in correct format 错误:System.FormatException:输入字符串的格式不正确 - ERROR : System.FormatException: Input string was not in the correct format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM