Parsing a comma seperated string of paired numbers with CRLF between data pairs
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. An开发者_如何学Cd, 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.
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();
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])));
}
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);
}
精彩评论