Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find comma in a Textbox full of integers

Tags:

c#

wpf

xaml

I got a textbox that should be filled with int of 4 number, something like this [0000, 4444, 5555, 6666]. I need to get where is the comma and then put the 4 numbers in a var. Can you help me?

like image 999
beachnugga Avatar asked Dec 01 '25 01:12

beachnugga


2 Answers

Have you tried String.Split?

string[] allTokens = textBox1.Text.Split(new []{ ','}, StringSplitOptions.RemoveEmptyEntries);
int[] allInts = Array.ConvertAll<string, int>(allTokens, int.Parse);

If the format can be invalid you can use int.TryParse:

int num = 0;
int[] allInts = allTokens
    .Where(s => int.TryParse(s, out num))
    .Select(s => num)
    .ToArray();
like image 123
Tim Schmelter Avatar answered Dec 03 '25 15:12

Tim Schmelter


You will get int list

var numbers = TextBox1.Text.Split(',').Select(str => {
                         int value;
                         bool success = int.TryParse(str, out value);
                         return new { value, success };
                     })
              .Where(pair => pair.success)
              .Select(pair => pair.value).ToList();

Reference

like image 28
Nikhil K S Avatar answered Dec 03 '25 14:12

Nikhil K S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!