0

I have the following list:

    List<string> scores = new List<string>
    {
        "1 point",
        "2 points",
        "5 points",
        "10 points",
        "15 points",
        "20 points",
        "25 points",
        "30 points",
        "40 points",
        "50 points"
    };

My code GUI selects one of these and returns a value from 0 to 9.

How can I convert return a number from 1 to 50 given the 0-9 number?

3
  • How does your GUI decide that one of these values should return 0-9? Based on its index? Commented Aug 29, 2017 at 3:11
  • what do you mean 0 to 9? Is it an index value or the value of your list itself? Commented Aug 29, 2017 at 3:13
  • Duplicates hopefully cover both question in title (extract number) and body (index of element or element by index). If you are asking about something else please edit to clarify. (Probably mapping 0-9 range to 1-50 numbers should be done with Dictionary<int, int> altogether....) Commented Aug 29, 2017 at 3:44

2 Answers 2

3

As this link suggests, Regex can be helpful here:

Given an integer (between 0 and 9, do the checking beforehand to ensure it is in that range):

resultString = Regex.Match(scores[x], @"\d+").Value;
var points = Int32.Parse(resultString);

P.S. You'll need to have using System.Text.RegularExpressions;

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

Comments

2

If by My code GUI selects one of these you mean a selection is made from this list and you would like to know its index you could try.

var testInput = "10 points";
var scores = new List<string>
{
    "1 point",
    "2 points",
    "5 points",
    "10 points",
    "15 points",
    "20 points",
    "25 points",
    "30 points",
    "40 points",
    "50 points"
}; 
var index = scores.IndexOf(testInput); //<- Returns 3

If you mean your value is 3 and you want to turn that to 10 points you can do the following.

var index = 3;
var scores = new List<string>
{
    "1 point",
    "2 points",
    "5 points",
    "10 points",
    "15 points",
    "20 points",
    "25 points",
    "30 points",
    "40 points",
    "50 points"
}; 
var score = scores[index]; //<- Returns 10 points

1 Comment

couldn't have done it better!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.