在C#中拆分String的问题 [英] Problem in splitting String in C#

查看:80
本文介绍了在C#中拆分String的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用ReadLine()方法从RFID阅读器获取数据并且它返回字符串..我必须分析该数据,所以我试图使用split方法分离字符串,并且在此我' m传递'\ n'作为分隔符,但它不工作..我还通过手动输入检查分隔符('\ n')问题,但似乎它也在其他程序中提供完美的输出。 />


所以有人知道其他分离字符串的方法比请尽快帮助我..



当前代码: -

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>

推荐答案

如果您使用一个ReadLine方法调用读取数据并获取大量数据,那么您不是得到'\ n'作为一个字符 - 它有点复杂,因为当你谈论数据流时,新行可能不是'\ n'单独 - t可以是两个字符\\\\ n而不是。

所以要做的第一件事就是准确看看你得到了什么:

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:
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.


如果你想知道当前换行符的长度:
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.


这篇关于在C#中拆分String的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆