?
Solved

How to parse data from C# string in a ListBox or directly from the returning string recieved from the Serial Port

Posted on 2009-04-20
4
Medium Priority
?
577 Views
Last Modified: 2012-05-06
I need to extract the numbers from the string in the code I shall attach, but the trick will be that there may be one, or up to about 5 possible items returned. I only want to extract the 6 digit numbers contained in the string.

Here is a string that contains an example. Each value is comma separated so it should be easy, I just don't have a clue how to go about doing it. The example has two numbers to extract.

at+cops=?
COPS cmd may take 1 min for resp.
+COPS: (2,"My Network","","310410"),(0,"My Network","","310260")  //Need to extract these numbers from a string like this one.


private void timer2_Tick(object sender, EventArgs e)
        {
            _sp.ReadTimeout = 120000;
            List<string> response = new List<string>();
            response = SendCommand("at+cops=?", 4);
            foreach (string line in response)
            {
                mainListBox.Items.Insert(0, line);
                if (line.Contains("+COPS: "))  //here is where I identify the line with the information
                {
                     //Ineed code here to extract numbers to an array
                }
                        else
                        {
                            continue;
                        }
            }

Open in new window

0
Comment
Question by:tazz500
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 1600 total points
ID: 24189675
Hello Tazz500
Someway on these line would might help you:
List<string> response = new List<string>();
 
            //+COPS: (2,"My Network","","310410"),(0,"My Network","","310260")  
 
            string fullText =       ("+COPS: (2," + @"""My Network""" + "," + @"""""" + "," + @"""310410""" + "),") 
                            +          ("(0," + @"""My Network""" + "," + @"""""" + "," + @"""310260""" + "), ")
                            +          ("(0," + @"""My Network""" + "," + @"""""" + "," + @"""420421""" + ") ");
 
            string[] fullItem = fullText.Split(')');
            var extractedValues = new List<int>();
            foreach (var itemString in fullItem)
            {
                string[] innerItem = itemString.Split(',');
                foreach (var innerItemString in innerItem)
                {
                    if(innerItemString.ToCharArray().Length == 8)
                    {
                        string[] strings1 = innerItemString.Split('"');
                        extractedValues.Add(Convert.ToInt32(strings1[1]));
 
                    }
                }
 
              
            }

Open in new window

0
 
LVL 3

Assisted Solution

by:BalkisBr
BalkisBr earned 400 total points
ID: 24189792
Hi,

you can use this code with regex, feel free to improve it
string[] array = Regex.Split(line, "([\"]\\d+[\"])");            
            
List<int> FinalList = new List<int>();
foreach (string s in array)
    if (Regex.IsMatch(s, "([\"]\\d+[\"])"))
        FinalList.Add(Convert.ToInt32(s.Replace("\"",""))); 

Open in new window

0
 

Author Comment

by:tazz500
ID: 24189816
Thanks for the help! Between some thing you said and another source I came up with this....
private void timer2_Tick(object sender, EventArgs e)
        {
            _sp.ReadTimeout = 120000;
            List<string> response = new List<string>();
            response = SendCommand("at+cops=?", 4);
            foreach (string line in response)
            {
                mainListBox.Items.Insert(0, line);
                Regex regex = new Regex(@"^\+COPS: (\(\d+,""[^""]*"",""[^""]*"",""(\d+)""\),?)+\s*$");
                Match match = regex.Match(line);
                if (match.Success)
                {
                    List<string> matches = new List<string>();
                    foreach (Capture capture in match.Groups[2].Captures)
                    {
                        matches.Add(capture.Value);
                    }
 
                    foreach (string providers in matches)
                    {
                    comboBox1.Items.Add(providers);
                    }                     
                 
              }
 
            }             

Open in new window

0
 
LVL 3

Expert Comment

by:BalkisBr
ID: 24189960
Great, good solution!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

862 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