# Get range given endpoints

Hi EE:

I would like to get a range of alphabets or numbers (integers, whole numbers etc) given endpoints. Say, I provide a string "a,z", I should be able to get back a string a,b,c,d,e,f,...,x,y,z and likewise for numbers. How would be the easiest way to accomplish this?

thx
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
have you tried

Convert the input to two ascii number,
Add all chars between those two numbers?

0

Author Commented:
if you can provide the code for that you will get the cake ;)
0

Commented:
static string getRange(string input)
{
StringBuilder sb = new StringBuilder(30);
char[] range = input.ToCharArray();
for (char i = range[0]; i <= range[2]; i++)
{
sb.Append(i).Append(',');
}
return sb.Remove(sb.Length - 1,1).ToString();
}

Assume the input is in the format of "x,y"  y > x
0

Author Commented:
I am not sure how this will handle somthing like "0,15". I am expecting the correct answer of "1,14" but I get "1,0". Is there a way to make the last char '15' give 14 and not 0?

thx
0

Commented:
static string getRange(string input)
{
StringBuilder sb = new StringBuilder();
string[] s = input.Split(',');
int start;
if (int.TryParse(s[0], out start)) //Number
{
int end = int.Parse(s[1]);
for (int n = start; n <= end; n++)
{
sb.Append(n.ToString()).Append(',');
}
}
else //Char
{
for (char i = s[0].ToCharArray()[0]; i <= s[1].ToCharArray()[0]; i++)
{
sb.Append(i).Append(',');
}
}
return sb.Remove(sb.Length - 1, 1).ToString();
}
0

Experts Exchange Solution brought to you by ConnectWise

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
One minor request, could this be modifed as  "0-15" will return "1-14" (no need for iterating a list as 1,2,3,4,....14). Also, can this be restricted so that the lower bound doesnt go lesser than 0?

many thx
0

Author Commented:
BTW I also get an error saying TryParse doesnt exist for int. Looking that up, it exists for Double
0

Commented:
Are you using .net 1.1?

In this case, you can use Parse and capture exception.

To get 1-14 is simple, add 1 to start and minus 1 from end then create your return string.
You can verify the start to make sure it's greater than 0.
change
for (int n = start; n <= end; n++)
{
sb.Append(n.ToString()).Append(',');
}
to
return (start + 1).ToString() + "-" + (end - 1).ToString;

For character,

return (s[0].ToCharArray()[0] + 1).ToString() + "-" + (s[1].ToCharArray()[0] + 1).ToString();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.