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?

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • How does your GUI decide that one of these values should return 0-9? Based on its index? – mishamosher Aug 29 '17 at 03:11
  • what do you mean 0 to 9? Is it an index value or the value of your list itself? – Vijunav Vastivch Aug 29 '17 at 03: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` altogether....) – Alexei Levenkov Aug 29 '17 at 03:44

2 Answers2

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;

Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41
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
Nico
  • 12,493
  • 5
  • 42
  • 62