Solved

Extracting integers from comma separated string

Posted on 2011-09-06
11
383 Views
Last Modified: 2012-05-12
I have a string of comma separated numbers. I want to get the numbers that do not begin with E and put them into separate integers. Here is the string that I have.

E0490,E0493,E0494,E0495,E0497,E0498,94399960,94401460

So I want the last two numbers, however the amount of numbers beginning with E may change and the amount of other numbers may change. So I cannot just find the index of the comma after the last E number and trim the string. I am using C#.

How would I go about doing this?

0
Comment
Question by:tango2009
[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
  • 5
  • 5
11 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 36487480
You can split it into an array:

string[] SplitString = MyVar.Split(',')

Then use SplitString.Length to find out how many there are and get the last 2.
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36487492
yes, you can use string[] SplitString = MyVar.Split(',') to split you string to string[]
and in this array you can remove string that beggin with E
0
 

Author Comment

by:tango2009
ID: 36487497
That is what I originally tried, but sometimes the string may have three non E numbers or four. How do I get around this? Is there a way I can test the length of each element in the array and pull the ones out which are over a certain length.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tango2009
ID: 36487503
minhvc

How can I remove those that begin with E?
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 36487521
Well, adjusting to C# (I'm a VB guy) it would be something like:

foreach (string s in SplitString)
{
  if (s.Length>50)
  {
    --Code here for over a value
  }
  if (s.SubString(0, 1)='E')
  {
    --Code here for starts with E
  }
}

Something like this anyway
0
 

Author Comment

by:tango2009
ID: 36487597
Thanks for that, the only problem I have is putting them into separate strings. Using your code this is what I have below in the code section. When it iterates through it of course replaces the first number I want with the second. How do I separate them so one string will take the first number I want and a second string will take the second.
string[] digits = UniqueCode.Split(',');

                

                foreach (string s in digits)
               {
                 if (s.Length>5)
               {
                   HUCode = s;
                     
                  
               }
                 if (s.Substring(0,1).Contains("E"))
               {
                   s.Remove(0,5);
                  
               }
             }

Open in new window

0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 36487643
Not sure if I understand exactly what you want. If you want to split codes to one array and numbers to another, you can try something like:

string[] digits = UniqueCode.Split(',');
string[] Codes = new string[digits.Length - 1];
string[] Numbers = new string[digits.Length - 1];
int a,b;
a=0;
b=0;
                foreach (string s in digits)
               {
                 if (s.Length>5)
               {
                   Numbers[a]=s;
                  a +=1;
               }
                 if (s.Substring(0,1).Contains("E"))
               {
                   Codes[b]=s;
                  b +=1;
               }
             }

Open in new window

0
 

Author Comment

by:tango2009
ID: 36487651
What I want to do is to get these two numbers from the string

94399960 94401460

assign the first number to one string and the second number to another string.

so the result should be

string one = 94399960
string two = 94401460
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 36487667
You can split them again using ' '.
0
 

Author Comment

by:tango2009
ID: 36487681
Oh of course you can, sorry stupid question.
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 36487724
There are no stupid questions. As long as you learn, it's all good. You can also try breaking the string with substring and indexof(' ',0). But I prefer splitting.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add projects t working set in maven 2 44
ejb message driven bean mdb creation steps 2 37
Dynamics CRM Plugin Query TotalRecordCount returning -1 13 63
asp Google Map 2 64
A short article about problems I had with the new location API and permissions in Marshmallow
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

726 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