c# - How to see if (for example) array[3] exists -
i want type strings in console. example "hello im 20", , want split line little strings. strings want something. when put in "hello im", should possible write line like: "forgot put in ago"so tried (words[2] == null), doens't work.
string line line = console.readline(); string[] words = new string[3]; words = line.split(' '); foreach(string word in words) { if (words[2] == null) { } else { int = int32.parse(words[2]); //do here } }
the item in array @ position 3 fourth item, since start counting @ 0 (as say, arrays 0-based).
so have check if length @ least 4 (0, 1, 2 , 3).
since length integer, , integers can't contain fractions (you can't have half item in array), safe check if count more three:
if (words.length > 3) { } a side note: variable assignment makes no sense. in fact, set array twice. assign variable @ once:
string[] words = line.split(' ');
Comments
Post a Comment