解析数据对之间使用 CRLF 的逗号分隔的成对数字字符串

发布于 2024-12-07 00:50:06 字数 492 浏览 1 评论 0原文

我正在使用 C#。 我已经搜索了这些例子,并尝试了几个小时。这种情况有所不同。我找不到解决办法。

我有一个从串行端口上的设备捕获的长字符串。 该字符串的格式为:

整数,整数CRLF整数,整数CRLF

(重复...)。以 CRLF 结尾。

如果您在 RichTextBox 中显示此字符串,它看起来像:

2442,6266
7727,2727
6320,272
etc... 

因此,同样,该字符串的格式为

TextWhichIsAnInteger,TextWhichIsAIntegerCRLF

(重复)。

我想将它放入两个 int 类型的数组(或列表)中。 并且,作为奖励,进入 2 列数组或成对整数列表。 这让我抓狂。如果您能帮忙提供代码,我将非常感激并永远崇拜您。

I am using C#.
I have scoured the examples, and tried for hours. This situation is different. I can't find a solution.

I have a long string captured from a device on a serial port.
The string's format is:

integer,integerCRLFinteger,integerCRLF

(repeats...). Ends with CRLF.

If you show this string in a RichTextBox it looks like:

2442,6266
7727,2727
6320,272
etc... 

So, again, the string's format is

TextWhichIsAnInteger,TextWhichIsAIntegerCRLF

(repeats).

I would like to get it into two arrays (or lists) of type int.
And, as a bonus, into a 2 column array or list of paired ints.
This is driving me nuts. If you can help provide the code, I will be completely grateful and will worship you forever.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

つ可否回来 2024-12-14 00:50:06

像这样的东西吗?从我的头顶。未经测试。但它应该给你这个想法。

using System.Collections.Generic;

List<int> aTypes = new List<int>();
List<int> aValues = new List<int>();

string sInput = "1234,5678\n9876,4321\n";
// Split by linebreak.
string[] aLinebreaks = sInput.Split('\n');
foreach(string sNumericString in aLineBreaks)
{
  // Split by comma.
  string[] aNumbers = sNumericString.Split(',');
  aTypes.Add(Convert.ToInt32(aNumbers[0]);
  aValues.Add(Convert.ToInt32(aNumbers[1]);
}

...

aTypes.ToArray();
aValues.ToArray();

Something like this? From the top of my head. Untested. But it should give you the idea.

using System.Collections.Generic;

List<int> aTypes = new List<int>();
List<int> aValues = new List<int>();

string sInput = "1234,5678\n9876,4321\n";
// Split by linebreak.
string[] aLinebreaks = sInput.Split('\n');
foreach(string sNumericString in aLineBreaks)
{
  // Split by comma.
  string[] aNumbers = sNumericString.Split(',');
  aTypes.Add(Convert.ToInt32(aNumbers[0]);
  aValues.Add(Convert.ToInt32(aNumbers[1]);
}

...

aTypes.ToArray();
aValues.ToArray();
再可℃爱ぅ一点好了 2024-12-14 00:50:06

为什么先.替换然后.拆分?

此外,元组也是配对的好工具。

    string myNumbers = "123,456,789\r\n";
    myNumbers += "123,456,789\r\n";
    var delimiters = new[] {"\r\n"};
    string[] lines = myNumbers.Split(delimiters, StringSplitOptions.None);
    var result = new List<Tuple<int, int>>();
    foreach (var line in lines)
    {
        var integers = line.Split(',');
        result.Add(Tuple.Create(Int32.Parse(integers[0]), Int32.Parse(integers[1])));
    }

Why .Replace then .Split ?

Also, Tuples are great tools to make pairs.

    string myNumbers = "123,456,789\r\n";
    myNumbers += "123,456,789\r\n";
    var delimiters = new[] {"\r\n"};
    string[] lines = myNumbers.Split(delimiters, StringSplitOptions.None);
    var result = new List<Tuple<int, int>>();
    foreach (var line in lines)
    {
        var integers = line.Split(',');
        result.Add(Tuple.Create(Int32.Parse(integers[0]), Int32.Parse(integers[1])));
    }
や三分注定 2024-12-14 00:50:06

这样的事情对你有用吗?

string input = "2442,6266\r\n7727,2727\r\n1234,1234";

string[] numberGroup = input.Replace("\r\n", ":").Split(':');
IDictionary<int,int> numberPairs = new Dictionary<int, int>();
foreach(string str in numberGroup)
{
    string[] pair = str.Split(',');
    numberPairs.Add(Convert.ToInt32(pair[0]), Convert.ToInt32(pair[1]));
}


foreach(int key in numberPairs.Keys)
{
    Console.WriteLine("Number Pair: [{0}]:[{1}]", key, numberPairs[key]);
}

//Output:
//Number Pair: [2442]:[6266]
//Number Pair: [7727]:[2727]
//Number Pair: [1234]:[1234]

编辑

如果您不想重复,可以使用这种方式,就像 Pano 指出的那样,但我会告诉拆分器删除空结果,这样它就不会在索引调用之一上爆炸转换面积。

string input = "2442,6266\r\n7727,2727\r\n1234,1234";

string[] numberGroup = input.Split(new[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
IList<Tuple<int, int>> numberPairs = new List<Tuple<int, int>>();
foreach(string str in numberGroup)
{
    string[] pair = str.Split(',');
    numberPairs.Add(Tuple.Create(Convert.ToInt32(pair[0]), Convert.ToInt32(pair[1])));
}


foreach(Tuple<int,int> item in numberPairs)
{
    Console.WriteLine("Number Pair: [{0}]:[{1}]",item.Item1, item.Item2);
}

Does something like this work for you?

string input = "2442,6266\r\n7727,2727\r\n1234,1234";

string[] numberGroup = input.Replace("\r\n", ":").Split(':');
IDictionary<int,int> numberPairs = new Dictionary<int, int>();
foreach(string str in numberGroup)
{
    string[] pair = str.Split(',');
    numberPairs.Add(Convert.ToInt32(pair[0]), Convert.ToInt32(pair[1]));
}


foreach(int key in numberPairs.Keys)
{
    Console.WriteLine("Number Pair: [{0}]:[{1}]", key, numberPairs[key]);
}

//Output:
//Number Pair: [2442]:[6266]
//Number Pair: [7727]:[2727]
//Number Pair: [1234]:[1234]

EDIT

If you don't want duplicates there is this way, like Pano pointed out, but I would tell the splitter to remove empty results so it doesn't blow up on one of the index calls in the convert area.

string input = "2442,6266\r\n7727,2727\r\n1234,1234";

string[] numberGroup = input.Split(new[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
IList<Tuple<int, int>> numberPairs = new List<Tuple<int, int>>();
foreach(string str in numberGroup)
{
    string[] pair = str.Split(',');
    numberPairs.Add(Tuple.Create(Convert.ToInt32(pair[0]), Convert.ToInt32(pair[1])));
}


foreach(Tuple<int,int> item in numberPairs)
{
    Console.WriteLine("Number Pair: [{0}]:[{1}]",item.Item1, item.Item2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文