Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am receiving data from a device using serial port. I have to split and show separately the string which starts by ** and ends with $ symbol.

Here the data looks like.

**9-Realtech-00:00:09-28/04/16-0$**10-Realtech-00:00:10-28/04/16-1$**11-Realtech-00:00:11-28/04/16-0$**12-Realtech-00:00:12-28/04/16-1$**13-Realtech-00:00:13-28/04/16-0$**14-Realtech-00:00:14-28/04/16-1$**15-Realtech-00:00:15-28/04/16-0$**16-Realtech-00:00:16-28/04/16-1$**17-Realtech-00:00:17-28/04/16-0$**18-Realtech-00:00:18-28/04/16-1$**19-Realtech-00:00:19-28/04/16-0$**20-Realtech-00:00:20-28/04/16-1$.

What I have tried:

I have tried this code.

this.richTextBox1.Text += data.ToString();
string x = richTextBox1.Text;
string match = x.Split(new string[] { "**" }, StringSplitOptions.None)[1].Split('$')[0].Trim();

But it always shows the first set of data "9-Realtech-00:00:09-28/04/16-0". It is not going to next set of data's. Please help me to solve this.
Posted
Updated 28-Apr-16 18:45pm
Comments
PIEBALDconsult 29-Apr-16 0:30am    
:cough: Regular Expresions :cough:
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

1 solution

Try this

C#
string x = "**9-Realtech-00:00:09-28/04/16-0$**10-Realtech-00:00:10-28/04/16-1$**11-Realtech-00:00:11-28/04/16-0$**12-Realtech-00:00:12-28/04/16-1$**13-Realtech-00:00:13-28/04/16-0$**14-Realtech-00:00:14-28/04/16-1$**15-Realtech-00:00:15-28/04/16-0$**16-Realtech-00:00:16-28/04/16-1$**17-Realtech-00:00:17-28/04/16-0$**18-Realtech-00:00:18-28/04/16-1$**19-Realtech-00:00:19-28/04/16-0$**20-Realtech-00:00:20-28/04/16-1$.";
        

var temp = x.Split(new string[] { "**" }, StringSplitOptions.RemoveEmptyEntries);
        List<string> result = new List<string>();
        foreach (string item in temp)
            result.AddRange(item.Split(new string[] { "$" }, StringSplitOptions.RemoveEmptyEntries));
 
Share this answer
 
Comments
S.Soundar 29-Apr-16 1:21am    
Its working. Thank you karthik sir. But have another problem.
var text = string.Empty;
foreach (String s in result)
{

richTextBox2.Text = s.ToString();

}
string fn = richTextBox2.Text;
string[] d = fn.Split('-');
dataGridView1.Rows.Add(d);
while i display this in datagrid by using this code it is displaying some duplicate datas and some incomplete data. Kindly help me sir.
Karthik_Mahalingam 29-Apr-16 1:28am    
use this code.
dataGridView1.Columns.Add("Col", "Col");
foreach (var item in result)
dataGridView1.Rows.Add(item);
Karthik_Mahalingam 29-Apr-16 1:24am    
why do u want to split by (-).
do you want to remove the duplicates?
you should write as

richTextBox2.Text += s.ToString();
S.Soundar 29-Apr-16 1:30am    
I have to filter the string starts with ** and ends with $ and need to split by _ and show each data into gridview for debugging purpose with column heading sir.

I tried richTextBox2.Text += s.ToString(); still i am facing the same problem sir.
Karthik_Mahalingam 29-Apr-16 1:52am    
use this

var temp = x.Split(new string[] { "**" }, StringSplitOptions.RemoveEmptyEntries);
List<string> result = new List<string>();
foreach (string item in temp)
result.AddRange(item.Split(new string[] { "$" }, StringSplitOptions.RemoveEmptyEntries));

List<string> final = new List<string>();
foreach (string item in result)
final.AddRange(item.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries));
final = final.Distinct().ToList();

dataGridView1.Columns.Add("Column1", "Column1");
foreach (var item in final)
dataGridView1.Rows.Add(item);

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