delimited string test

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
let's say you have a delimited string value of

String value = val1,val2,val3;

and you do something like the following

String s = value.split(',')[3];

well this is outside the range of index, so how can i say if this does occur
just make it an empty string and proceed?

thanks,
rodchar
 
rodchar said:
hey all,
let's say you have a delimited string value of

String value = val1,val2,val3;

and you do something like the following

String s = value.split(',')[3];

well this is outside the range of index, so how can i say if this does occur
just make it an empty string and proceed?
Test for it.

string[] values = value.split(',');
string s = values.Length > 3 ? values[3] : "";
 
hey all,
let's say you have a delimited string value of

String value = val1,val2,val3;

and you do something like the following

String s = value.split(',')[3];

well this is outside the range of index, so how can i say if this does occur
just make it an empty string and proceed?

thanks,
rodchar

What about by checking?
string[] parts = myStr.split( new char[]{','});
string d = ( index < parts.Length ) parts[index]: "";
if (
 
Back
Top