如何仅拆分或替换某些报价? [csharp]

时间:2019-06-09 15:00:09

标签: c#

基本上,我想删除此json文本中的第一个和最后一个引号,但我不知道该怎么做。

"{"serverToken": "798358975235895283958208564208520o45", "username": "ImSkrt", "imageUrl": "https://s3.amazonaws.com/media.orderv.com/orderv_live/static/player_avatars/12039273888.1557487912.907537.jpg", "message": "Success!"}"

它最终应该看起来像这样

{"serverToken": "798358975235895283958208564208520o45", "username": "ImSkrt", "imageUrl": "https://s3.amazonaws.com/media.orderv.com/orderv_live/static/player_avatars/12039273888.1557487912.907537.jpg", "message": "Success!"}

我已经尝试替换它,但是它将替换所有引号。

WebClient wc = new WebClient();
            HttpClient hc = new HttpClient();
            HttpClient hc2 = new HttpClient();

            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://f2aa611yne.execute-api.us-east-1.amazonaws.com/production/players/confirm/");

            List<KeyValuePair<string, string>> p = new List<KeyValuePair<string, string>>();

            p.Add(new KeyValuePair<string, string>("device_id", "0AF0406E-6DD1-452E-BE14-3B5A94ACE320"));
            p.Add(new KeyValuePair<string, string>("passcode", textBox8.Text));
            p.Add(new KeyValuePair<string, string>("phone_number", textBox4.Text));

            HttpResponseMessage response = await hc.PostAsync("https://f2aa611yne.execute-api.us-east-1.amazonaws.com/production/players/confirm/", new FormUrlEncodedContent(p));

            string s = await(response.Content.ReadAsStringAsync());

            Console.WriteLine(s);

            string ss = s.Replace(@"\", "");

            Console.WriteLine(ss);

            string sss = ss.Replace("\"", "");

2 个答案:

答案 0 :(得分:4)

您可以这样使用string.Trim

string ss = s.Trim('\"');

这将清除在字符串的开头和结尾出现的所有引号。

答案 1 :(得分:0)

使用substring()删除字符串的第一个和最后一个字符

string s = await(response.Content.ReadAsStringAsync());

string s2 = s.Substring(1,s.length - 2);