[英]Convert an escaped string to an String Array
I have this kind of String 我有这种弦
string input="[\"Modell\",\"Jan.\",\"Feb.\",\"Mrz.\",\"Apr.\",\"Mai\",\"Jun.\",\"Jul.\",\"Aug.\",\"Sep.\",\"Okt.\",\"Nov.\",\"Dez.\"]";
I need to convert it into something like this: 我需要将其转换成这样的东西:
string[] output;//convert "input" to it
I was looking at here , here and here , but it didn't help me. 我一直在这里 , 这里和这里看 ,但并没有帮助我。
How can I convert my string
to string[]
is this case ? 如何将我的
string
转换为string[]
?
您的输入采用json格式作为字符串数组,因此您可以在nuget上使用非常流行的库Newtonsoft.Json
,然后在C#中反序列化回字符串数组:
var result = JsonConvert.DeserializeObject<string[]>(input);
What about this: 那这个呢:
var output = input.Trim(new[] { '[', ']' }).Split(',').Select(x => x.Trim('\"')).ToArray();
Although this might work for your example I recommend using the approach given by @Cuong Le using Json-Deserializer. 尽管这可能对您的示例有效,但我建议使用@Cuong Le给出的方法,并使用Json-Deserializer。 It is much more robust and also handles nested structures.
它更加健壮,还可以处理嵌套结构。
不是很好,但是可以工作:
string[] output = input.Replace("[", "").Replace("\"", "").Replace("]", "").Split(',').ToArray<string>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.