How to enter the doubles quotes in string

冷暖自知 提交于 2019-12-24 01:29:05

问题


I have piece of code where I am trying to enter the double quotes, My code is as follow,

resulted_value = "{series_name : \"" + final_resulted_series_name + "\",period_name: \"" + period_name + "\",period_final_value: \"" + period_final_value + "\"}";

here in result \ is shown every-time, I have used "\"+variable+\"" and also @"""variable""" but I am not getting desired output..any help will be greatly appreciated.

------------------------EDIT 1-------------------- am getting following output

["{series_name : \"Actual\",period_name:Q1 / 2013\",period_final_value:17\"}","\"{series_name : \\\"Actual\\\",period_name:Q1 / 2013\\\",period_final_value:17\\\"}\"","{series_name : \"Actual\",period_name:Q2 / 2013\",period_final_value:15\"}","\"{series_name : \\\"Actual\\\",period_name:Q2 / 2013\\\",period_final_value:15\\\"}\"","{series_name : \"Actual\",period_name:Q3 / 2013\",period_final_value:13\"}","\"{series_name : \\\"Actual\\\",period_name:Q3 / 2013\\\",period_final_value:13\\\"}\"","{series_name : \"Actual\",period_name:Q1 / 2013\",period_final_value:14.103\"}","\"{series_name : \\\"Actual\\\",period_name:Q1 / 2013\\\",period_final_value:14.103\\\"}\"","{series_name : \"Actual\",period_name:Q2 / 2013\",period_final_value:14.404499999999999\"}","\"{series_name : \\\"Actual\\\",period_name:Q2 / 2013\\\",period_final_value:14.404499999999999\\\"}\"","{series_name : \"Actual\",period_name:Q3 / 2013\",period_final_value:14.966999999999999\"}","\"{series_name : \\\"Actual\\\",period_name:Q3 / 2013\\\",period_final_value:14.966999999999999\\\"}\"","{series_name : \"Actual\",period_name:Q1 / 2013\",period_final_value:15.67\"}","\"{series_name : \\\"Actual\\\",period_name:Q1 / 2013\\\",period_final_value:15.67\\\"}\"","{series_name : \"Actual\",period_name:Q2 / 2013\",period_final_value:16.005\"}","\"{series_name : \\\"Actual\\\",period_name:Q2 / 2013\\\",period_final_value:16.005\\\"}\"","{series_name : \"Actual\",period_name:Q3 / 2013\",period_final_value:16.63\"}","\"{series_name : \\\"Actual\\\",period_name:Q3 / 2013\\\",period_final_value:16.63\\\"}\"","{series_name : \"Alarm\",period_name:Q1 / 2013\",period_final_value:17\"}","\"{series_name : \\\"Alarm\\\",period_name:Q1 / 2013\\\",period_final_value:17\\\"}\"","{series_name : \"Alarm\",period_name:Q2 / 2013\",period_final_value:15\"}","\"{series_name : \\\"Alarm\\\",period_name:Q2 / 2013\\\",period_final_value:15\\\"}\"","{series_name : \"Alarm\",period_name:Q3 / 2013\",period_final_value:13\"}","\"{series_name : \\\"Alarm\\\",period_name:Q3 / 2013\\\",period_final_value:13\\\"}\"","{series_name : \"Alarm\",period_name:Q1 / 2013\",period_final_value:14.103\"}","\"{series_name : \\\"Alarm\\\",period_name:Q1 / 2013\\\",period_final_value:14.103\\\"}\"","{series_name : \"Alarm\",period_name:Q2 / 2013\",period_final_value:14.404499999999999\"}","\"{series_name : \\\"Alarm\\\",period_name:Q2 / 2013\\\",period_final_value:14.404499999999999\\\"}\"","{series_name : \"Alarm\",period_name:Q3 / 2013\",period_final_value:14.966999999999999\"}","\"{series_name : \\\"Alarm\\\",period_name:Q3 / 2013\\\",period_final_value:14.966999999999999\\\"}\"","{series_name : \"Alarm\",period_name:Q1 / 2013\",period_final_value:15.67\"}","\"{series_name : \\\"Alarm\\\",period_name:Q1 / 2013\\\",period_final_value:15.67\\\"}\"","{series_name : \"Alarm\",period_name:Q2 / 2013\",period_final_value:16.005\"}","\"{series_name : \\\"Alarm\\\",period_name:Q2 / 2013\\\",period_final_value:16.005\\\"}\"","{series_name : \"Alarm\",period_name:Q3 / 2013\",period_final_value:16.63\"}","\"{series_name : \\\"Alarm\\\",period_name:Q3 / 2013\\\",period_final_value:16.63\\\"}\"","{series_name : \"Target\",period_name:Q1 / 2013\",period_final_value:17\"}","\"{series_name : \\\"Target\\\",period_name:Q1 / 2013\\\",period_final_value:17\\\"}\"","{series_name : \"Target\",period_name:Q2 / 2013\",period_final_value:15\"}","\"{series_name : \\\"Target\\\",period_name:Q2 / 2013\\\",period_final_value:15\\\"}\"","{series_name : \"Target\",period_name:Q3 / 2013\",period_final_value:13\"}","\"{series_name : \\\"Target\\\",period_name:Q3 / 2013\\\",period_final_value:13\\\"}\"","{series_name : \"Target\",period_name:Q1 / 2013\",period_final_value:14.103\"}","\"{series_name : \\\"Target\\\",period_name:Q1 / 2013\\\",period_final_value:14.103\\\"}\"","{series_name : \"Target\",period_name:Q2 / 2013\",period_final_value:14.404499999999999\"}","\"{series_name : \\\"Target\\\",period_name:Q2 / 2013\\\",period_final_value:14.404499999999999\\\"}\"","{series_name : \"Target\",period_name:Q3 / 2013\",period_final_value:14.966999999999999\"}","\"{series_name : \\\"Target\\\",period_name:Q3 / 2013\\\",period_final_value:14.966999999999999\\\"}\"","{series_name : \"Target\",period_name:Q1 / 2013\",period_final_value:15.67\"}","\"{series_name : \\\"Target\\\",period_name:Q1 / 2013\\\",period_final_value:15.67\\\"}\"","{series_name : \"Target\",period_name:Q2 / 2013\",period_final_value:16.005\"}","\"{series_name : \\\"Target\\\",period_name:Q2 / 2013\\\",period_final_value:16.005\\\"}\"","{series_name : \"Target\",period_name:Q3 / 2013\",period_final_value:16.63\"}","\"{series_name : \\\"Target\\\",period_name:Q3 / 2013\\\",period_final_value:16.63\\\"}\""]

and I want to eliminate the \ and want every value inside "" so that it can be read and used in further code.


回答1:


The only way I can recommend to build this, if you are unsure, is with a serializer. For example:

var obj = new {
    series_name = final_resulted_series_name,
    period_name,
    period_final_value 
};
var json = JsonConvert.SerializeObject(obj);

which produces (for values abc, def and ghi respectively):

{"series_name":"abc","period_name":"def","period_final_value":"ghi"}

If you are using concatenation, then it will eventually bite you. You need to know all the encoding rules, edge-cases and corner-cases to do this manually.




回答2:


Write the output to a console window, the quotes are escaped fine. You're probably viewing in the debugger which will conserve the \ slashes.

string final_resulted_series_name = "foo";
string period_name = "bar";
string period_final_value = "foobar";
string resulted_value = "{series_name : \"" + final_resulted_series_name + "\",period_name: \"" + period_name + "\",period_final_value: \"" + period_final_value + "\"}";

Console.Write(resulted_value);

Gives:

{series_name : "foo", period_name: "bar", period_final_value: "foobar"}

This probably isn't the best way to construct a json string as Marc kindly points out. I would recommend using a json serializer.

If you're using json.net you can use the jsonConvert method:

JsonConvert.SerializeObject(obj);

If not, you can use the following:

public virtual string ToJson()
{
    string json = null;
    using (MemoryStream ms = new MemoryStream())
    {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(this.GetType());
        ser.WriteObject(ms, this);
        json = Encoding.UTF8.GetString(ms.ToArray());
    }

    return json;
}

You should mark your object as using a DataContract and being Serializable to use this.

[DataContract]
[Serializable]



回答3:


Console.WriteLine("You can use \"slashes\" to escape \" double quotes\"");
Console.WriteLine(@"Or use ""@"" before the string and double ""double quotes""");

Output:

You can use "slashes" to escape " double quotes"

Or use "@" before the string and double "double quotes"

Bear in mind that if you are inspecting the string in the debugger, you will still see the string as they appear in code (I.E. including escaping slashes, or double double quotes).



来源:https://stackoverflow.com/questions/19927779/how-to-enter-the-doubles-quotes-in-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!