Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a data coming from the serial port of a controller. The data is in string format and has about 20 lines of data. I need to select a particular line say line 6 and fetch a particular word from that line.
I need it in C#

Please help me with a solution

What I have tried:

I am pretty new to coding. this is my first attempt at such a requirement
Posted
Updated 6-Dec-17 21:12pm
v2
Comments
Richard MacCutchan 28-Nov-17 6:57am    
You just need to study string or character handling. Search the text for line end characters so you can split into separate lines. Then search each line for the word(s) you are interested in.
Karthik_Mahalingam 5-Dec-17 23:51pm    
what is your input string and expected output ?
Member 13545569 7-Dec-17 2:43am    
String input:

DT 07-12-17 H 13 Min 23 S 34
M- -2.00 M 0.00 M+ 7.00 OK
**CS351**
W- 950 W 1000.36 W+ 1200

Expected output. These strings/int I need to extract from the above given string data

-2 0 7 ok
950 1000.36 1200

1 solution

OP wrote:
String input:

DT 07-12-17 H 13 Min 23 S 34
M- -2.00 M 0.00 M+ 7.00 OK
**CS351**
W- 950 W 1000.36 W+ 1200

Expected output. These strings/int I need to extract from the above given string data

-2 0 7 ok
950 1000.36 1200


using System;

namespace CP
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "DT 07-12-17 H 13 Min 23 S 34 \n" +
                               " M- -2.00 M 0.00 M+ 7.00 OK \n" +
                                "**CS351** \n" +
                                "W- 950 W 1000.36 W+ 1200";
            string[] lines = input.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
            string output = "";
            foreach (string line in lines)
            {
                string item = line.Trim();
                if (item.StartsWith("M"))
                {
                    output += GetNumbers( item) + " ";
                    if (item.EndsWith("OK"))
                        output += "ok";

                    output += Environment.NewLine;
                }
                if (item.StartsWith("W"))
                {
                    output += GetNumbers( item) + " ";
                    output += Environment.NewLine;
                }

            }
            Console.WriteLine(output );
            //-2 0 7  ok
            // 950 1000.36 1200  


        }

        private static string GetNumbers( string item)
        {
            var parts = item.Split(' ');
            string output = "";
            foreach (string num in parts)
            {
                double d;
                if (double.TryParse(num.Trim(), out d))
                {
                    output += d + " " ;
                }
            }
            return output;
        }

    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900