How can I create a format that all items in a List/Text file have to conform to? C#

Charlie Gale 21 Reputation points
2022-07-15T22:23:40.323+00:00

I am writing a program that displays data coming in from an Arduino. At the moment I am getting a few errors while filtering out parts of data coming in. Data is collected through serial port in small clumps of data every 100ms (lots of data). I want to be able to create a format for all data that is kept in the string, so that all of the data is useful.

Data is displayed to a RichTextBox using CanText.Append(string.Join("\n", dataList)); where dataList is a list of all data coming in. IDs that are wanting to be deleted are stored in filteredIDs (list) in the format of "ID: 201"

Example of Data
ID: 201, Data: 0 0 40 0 0 0 0 80
ID: 201, Data: 0 0 40 0 0 0 0 80
ID: 20F, Data: FF FF 27 10 40 0 FF 77
ID: 190, Data: 0 53 0 0 3C 1 0 3C
ID: 20F, Data: FF FF 27 10 40 0 FF 7D
ID: 280, Data: 0 9
ID: 201, Data: 0 0 40 0 0 0 0 80
ID: 231, Data: 1 6F 64 0 0 0 80
ID: 205, Data: 2 95 1 F5 3 F4 1 F5
ID: 201, Data: 0 0 40 0 0 0 0 80
ID: 4B0, Data: 27 10 27 10 27 10 27 10
ID: 20F, Data: FF FF 27 10 40 0 FF 98
ID: 20F, Data: FF FF 27 10 40 0 FF 9B
ID: 190, Data: 0 54 0 0 3C 1 0 3C
ID: 4B0, Data: 27 10 27 10 27 10 27 10
ID: 280, Data: 0 9
ID: 20F, Data: FF FF 27 10 40 0 FF AA
ID: 433, Data: 80 4 50 1 0 A0 0 0
ID: 20F, Data: FF FF 27 10 40 0 FF AF
ID: 280, Data: 0 9

What i am using to filter my data

		`foreach (var filterID in filteredIDs)  
		{  
			dataList.RemoveAll(x => x.Contains(filterID));  

		}`  

Which seems to work with 1 or 2 values in filteredIDs, but when more are added to the list this is displayed instead.

Data: C 7 C F2 E BB 2C 63
CE 44 49 38 7E 4C 2B 1
D9 90 A5 B AF
E D1 A5 BE 31
ID: 202, Data: 1C 20 F CB 43 D9 42 7
7 F B0
ID: 205, Data: 86 1F B3 61 C3 5F 88 40
ID: 206, Data: 65 FB A0 A4 50 4E 19 D1
Data: D8 4B 24 87 C5 4C 2D 19
ta: 8B 77 74 F3 C AD 79 D1
5 3F 9D 47 E6 A4 8D 76
B 58 D7 E9 A4
ID: 202, Data: B3 E2 64 8C FD 82 EE 4F
7C 6E AF F6
ID: 202, Data: 8E BE 75 32 3 64 38 D
ID: 202, Data: 7A C9 CC 6E 8D 43 E7 C3
1D 5F BD
F9 F5 5F
ID: 202, Data: B0 81 28 E4 23 5D 6C 20
ID: 202, Data: DE EA C0 CE F0 D B4 A2

I was wondering if there was a way to remove all of the items in the list/ lines that are not in the format "ID: NUM, Data: XX XX XX XX XX XX XX XX"

Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-07-18T07:29:34.803+00:00

    @Charlie Gale , you could try the following code to remove all of the items in the list/ lines that are not in the format "ID: NUM, Data: XX XX XX XX XX XX XX XX".

      string []lines=File.ReadAllLines(@"Tool.txt");  
      var  result = lines.ToList().Where(x => x.StartsWith("ID")&&x.Contains("Data"));  
    

    Result:

    221755-image.png

    Hope my code could help you.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.