0

I have a split string,

 string s = Console.ReadLine();
 string[] values = s.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

s should receive input like this:

string literal, numeric value, numeric value, numeric value OR string literal

I realize that all this input gets read as a string, but I'm trying to validate the numbers in the string (checking for >0), as well as assign each value in the string to a variable. What would be the best way to go about this?

Brian Kubricky
  • 63
  • 1
  • 1
  • 9
  • "What would be the best way to do this using a find method with the Equals() method?" ==> I do not understand this question. – Martin Mulder Apr 24 '13 at 07:31
  • And if a number is > 0 what do you want to do? – Tim Apr 24 '13 at 07:41
  • `StringSplitOptions.RemoveEmptyEntries` will throw off your validation as well. Example: `string,1,,' will result in a 2 element array - so you'd have to look at the original string to see what was missing. `StringSplitOptions.None` would be a better choice (and I believe it's the default) – Tim Apr 24 '13 at 07:47

2 Answers2

0

You're looking for a specific pattern. I'd suggest to use a regex, and then get the number groups - and do the > 0 validation check.

aquaraga
  • 4,138
  • 23
  • 29
0
string s = Console.ReadLine();
string[] values = s.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string stringValue0 = values[0];
int numericValue1 = int.Parse(value[1]); // Assuming the value is an valid interger.
int numericValue2 = int.Parse(value[2]); // Assuming the value is an valid interger.
int numericvalue3;
string stringValue3;
if (!int.TryParse(values[3], out numericValue3) // Trying to convert the text to an interger. If it fails, assign it to the stringValue3.
    stringValue3 = values[3];

You can always use int.TryParse to validate if a text contains a number.

Martin Mulder
  • 12,642
  • 3
  • 25
  • 54