简体   繁体   中英

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 ?

您的输入采用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. It is much more robust and also handles nested structures.

不是很好,但是可以工作:

string[] output = input.Replace("[", "").Replace("\"", "").Replace("]", "").Split(',').ToArray<string>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM