Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project i'm getting data from RFID reader using ReadLine() method and it is returning string ..and i have to analyze that data so i'm trying to separate the string using split method and in this i'm passing '\n' as delimiter but it is not working.. i also checked for the delimiter ('\n')problem by giving manually input but it seems that it is also giving perfect output in other program.

so do anyone know about other method of separating the string than please help me asap..

Current code:--
private void button1_Click(object sender, EventArgs e)
       {
              try
           {
               string data = "";

               data = serialPort1.ReadLine();

               string[] result = data.Split('\n');

               List<string> final = new List<string>();

               for (int i = 0; i < result.Length; i++)
               {
                   int j = 0;

                   while (j < i)
                   {
                       if (result[j] != result[i])
                       {
                          final.Add(result[j]);
                           j++;
                       }

                   }

               }

               for (int i = 0; i < final.Count; i++)
               {
                   textBox1.Text = textBox1.Text + final[i] + "\n";
               }

           }
           catch (Exception er)
           {
               textBox1.Text = "Reading Exception" + er;
           }
       }</string></string>
Posted
Updated 8-Dec-13 3:43am
v6
Comments
[no name] 8-Dec-13 8:40am    
Because serialPort1.ReadLine maybe reads until next '\n'...? Think about it.
Dean_Wi 8-Dec-13 8:50am    
actually in one readline() method i'm getting a lot of data(Repeated data), so i need to remove all repeated data and want to store useful data..
when i printing readline()method in richtextbox at that time i'm getting all data in newlines so i'm thinking that i can separate that data using split()method and by using '\n' as delimiter..
[no name] 8-Dec-13 9:01am    
Ok seems my intention is wrong. For the rest of the code I'm too lazy to go trough it.

But on a first look I think you can solve the job "determining duplicate" much more easily. Don't fight with loops to check occurence, simply use IndexOf.

http://msdn.microsoft.com/en-us/library/e4w08k17(v=vs.110).aspx

private void button4_Click(object sender, EventArgs e) {

List<<string>> result= new List<<string>>();

string serialInput = "a\nb\nc\nd\ne\nb";

string[] splited = serialInput.Split('\n');

for (int xIx= 0; xIx < splited.Count(); xIx++) {

int xIxOf= result.IndexOf(splited[xIx]);

if (xIxOf < 0) {

result.Add(splited[xIx]); } }

}


And replace the "<<" ">>" by "<" and ">", sorry I don't know how to fix it :)
Dean_Wi 9-Dec-13 13:11pm    
Thanks a lot idle63 for solution it worked...
[no name] 9-Dec-13 13:27pm    
You are welcome

If you are reading the data with one ReadLine method call and getting lots of data, then you aren't getting '\n' as a character - it's slightly complicated, because when you talk about datastreams, a new line might not be a '\n' alone - t can be two characters "\r\n" instead.
So the first thing to do is to look at exactly what you are getting:
C#
data = serialPort1.ReadLine();
StringBuilder sb = new StringBuilder();
foreach (char c in data)
    sb.AppendFormat("{0} : {1}\n", c >= 32 ? c : '*', ((int)c).ToString("X2"));
Console.WriteLine(sb.ToString());
That will tell you exactly what you have, and you should be able to identify a suitable split candidate from that.
 
Share this answer
 
Comments
WuRunZhe 9-Dec-13 1:47am    
Good, but why did you pass character which is over than ascii 32?
OriginalGriff 9-Dec-13 1:54am    
Because below that they aren't printable as such - they are "control codes" such as Tab, carriage return, and so forth.
If you want to know the length of the current newline character(s):
Environment.NewLine.Length
I seriously doubt your problem here has anything to do with line-feeds: splitting a "line" on line-feeds does not make sense.

What you split the line with should be related to the data you need to analyze, and its format, and, until we have some idea what that is, we're just dancing in the dark.

What would be helpful would be to see a sample of the data.
 
Share this answer
 

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