-3

I already tried to determinate the digits in a sentence using 'isDigit', but this gives me a 'bool' output. I need an 'int' output.

What i want to do is, say, i have the sentence "cheese23"; "2" and "3" will be put in their own variable, so i can add/subtract/multiply/ etc them. (x=2,y=3;)

help will be hugely appreciated (self-teaching beginner here)

venerik
  • 5,766
  • 2
  • 33
  • 43
  • 1
    What do you *think* you need to do? Try something and we'd be happy to steer you in the correct direction. – Kirk Woll Sep 30 '15 at 19:54
  • sorry, but i dont know what i should "think" i need to do... C# is new to me, i'm just learning how to do stuff by googling it (that's how i found out about 'isDigit') and lots of try and error. I was just looking for help, sorry if i came too ignorant, but i will keep trying – Zugoldragon Sep 30 '15 at 19:57
  • no -- you **really** need to be able to sling some code. If you can't even do that then you need to work from material such as tutorials and documentation instead. – Kirk Woll Sep 30 '15 at 19:59
  • I'm not sure what search engine you use for googling - Both Google - https://www.google.com/?gws_rd=ssl#q=c%23+get+number+string and Bing https://www.bing.com/search?q=c%23%20get%20number%20string provide decent results... – Alexei Levenkov Sep 30 '15 at 20:00
  • @kirk im doing that actually, i just had a random idea and wanted to put it in action, just had no idea how. – Zugoldragon Sep 30 '15 at 20:06
  • @Alexei thanks, this helps (i was using the wrong wording and getting different answers as a result) – Zugoldragon Sep 30 '15 at 20:09

1 Answers1

0

Do:

int[] intArray = "Cheese23".Where(Char.IsDigit).Select(c => int.Parse(c.ToString())).ToArray();  

This extracts the numbers in the string in the order and creates an array out of it.
Then you can do intArray[0] to get 2 and intArray[1] to get 3. Search up LINQ to see how those chain of methods did it.

wingerse
  • 3,670
  • 1
  • 29
  • 61
  • thanks a lot, i understand this perfectly,but can i ask, why did you put (c=> int......). I tried changing the position of >, but it gives me an error. why is that? – Zugoldragon Sep 30 '15 at 20:47
  • `.Where(Char.IsDigit)` is an `IEnumerable` so `.Select(c => int.Parse(c.ToString()))` changes it to `IEnumerable`. – wingerse Sep 30 '15 at 20:50
  • `c => blabla` That is a lambda expression by the way. https://msdn.microsoft.com/en-us/library/bb397687.aspx – wingerse Sep 30 '15 at 20:51