-3

for example suppose that I have a string like "231143" and I wanna convert it to an int[] array which I can easily access 2,3,1,1,4,3 as an int. What should I do?

1
  • What have you tried yourself? Commented May 7, 2020 at 13:36

1 Answer 1

1

Without any error checking you can do:

var value = "231143";
var array = value.Select(c => c - '0').ToArray();

This makes use of a trick whereby you can subtract '0' from the value of a single character holding a number to get its integer value.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that this solution is only correct if it is guranteed that the input string only contains digits
@Ackdari I think we can safely assume this given by the wording of the question.
Yes, that's what I meant. thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.