delimited string test

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
 
J

Jeroen Mostert

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] : "";
 
I

Ignacio Machin ( .NET/ C# MVP )

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 (
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top