Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get range given endpoints

Posted on 2006-11-01
8
Medium Priority
?
226 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:LuckyLucks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 13

Expert Comment

by:joechina
ID: 17852256
have you tried

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

0
 

Author Comment

by:LuckyLucks
ID: 17853588
if you can provide the code for that you will get the cake ;)
0
 
LVL 13

Expert Comment

by:joechina
ID: 17858101
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:LuckyLucks
ID: 17867214
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
 
LVL 13

Accepted Solution

by:
joechina earned 2000 total points
ID: 17867427
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
 

Author Comment

by:LuckyLucks
ID: 17867550
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 Comment

by:LuckyLucks
ID: 17867598
BTW I also get an error saying TryParse doesnt exist for int. Looking that up, it exists for Double
0
 
LVL 13

Expert Comment

by:joechina
ID: 17869131
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question