2

I have a list as a single string like - "['2','4','5','1']" and length of this is 17 as each char is counted.

Now I want to parse it into list object like - ['2','4','5','1'] whose length will be 4 as the number of elements in a list.

How can I do this in C#?

Can it be done without doing basic string operations? If yes then how?

7
  • 4
    list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<char>>("['2','4','5','1']"); Commented Dec 13, 2019 at 7:42
  • 3
    Or var result = str.Split(',','[',']').Where(s => !string.IsNullOrEmpty(s)).ToList(); Commented Dec 13, 2019 at 7:43
  • 2
    what do you mean by "basic string-operations"? Could you please elaborate on what you want to achieve and in particular what you´ve tried already? We´re not here to do the thinking for you. You have to think yourself. However we can help you, if you have a specific problem or when you´re stuck. Commented Dec 13, 2019 at 7:43
  • 2
    @SᴇM StringSplitOptions.RemoveEmptyEntries - don't know if there's a difference in performance, but I'd rather use the option. Commented Dec 13, 2019 at 7:46
  • 2
    @Fildor Agreed, in that case - var result = str.Split(new[] { ',','[',']' }, StringSplitOptions.RemoveEmptyEntries); Commented Dec 13, 2019 at 7:49

3 Answers 3

7

Without basing string operations

Your string value looks like valid JSON array.

using Newtonsoft.Json;

var list = JsonConvert.DeserializeObject<List<char>>("['2','4','5','1']");

// => ['2','4','5','1']

If you need output as integers set output type to be list of integers and JSON serializer will convert it to integers.

var list = JsonConvert.DeserializeObject<List<int>>("['2','4','5','1']");

// => [2, 4, 5, 1]

Converting to integers will handle negative values as well ;)

var list = JsonConvert.DeserializeObject<List<int>>("['-2','4','-5','1']");

// => [-2, 4, -5, 1]
Sign up to request clarification or add additional context in comments.

1 Comment

Nice idea @Fabio !
5

You can try regular expressions and Linq in order to Match all the integers and turn them (ToList()) into List<int>:

using System.Linq;
using System.Text.RegularExpressions;

...

string str = "['2','4','5','1']";

var result = Regex
  .Matches(str, @"\-?[0-9]+")              // \-? for negative numbers
  .Cast<Match>()
  .Select(match => int.Parse(match.Value)) // int.Parse if you want int, not string
  .ToList();

Comments

4

Try to Split by , and then use Regex to get only digits:

var str = "['2','4','5','1']".Split(new char[] {',' })
    .Select(s => Regex.Match(s, @"\d+").Value);

Or thanks to @Fildor :

var str = Regex.Matches("['2','4','5','1']", @"\d+").Cast<Match>()
    .Select(s=> s.Value);

1 Comment

If you regex match anyway, why split first?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.