Jay
Jay

Reputation: 41

How to retrieve each value from string and add them to the list

This is my code so far in C#

string myString = "value:12345, data:34298, end, value2:678910, data2:48957, end, value3:56546, data3:83576, end";
var tuple = new List<Tuple<int, int>>();
foreach (var value in myString)
{
    int startPos = myString.IndexOf("value:") + "value:".Length;
    int length = myString.LastIndexOf(", data") - startPos;
    string valuesX = myString.Substring(startPos, length);
    int values = int.Parse(valuesX);
    int startPos2 = myString.IndexOf("data:") + "data:".Length;
    int length2 = myString.LastIndexOf(", end") - startPos2;
    string dataX = myString.Substring(startPos2, length2);
    int data = int.Parse(dataX);
    tuple.Add(Tuple.Create(values, data));
}

I want to retrieve the value number and data number from string and put them into a list using integer data type

this picture demonstrates what I want to achieve:

picture demonstration

The code simply does not work and I would appreciate any help, thank you

Upvotes: 0

Views: 169

Answers (3)

NetMage
NetMage

Reputation: 26936

Using Linq, you can process the string into substrings and then into values in Lists:

var tuples = myString.Split(new[] { ", end" }, StringSplitOptions.None) ' split into data strings
                     .Select(s => s.Split(new[] { ", " }, StringSplitOptions.RemoveEmptyEntries) ' split into data string pairs
                     .Select(s2 => Int32.Parse(s2.Split(':')[1])).ToList()) ' extract number and make int32 pairs
                     .Where(s => s.Count > 0) ' exclude empty data strings
                     .Select(s => (s[0], s[1])).ToList(); ' create tuples and put in a List

Upvotes: 1

pattpass
pattpass

Reputation: 131

You can use a regular expression. Here is my code below.

string myString = "value:12345, data:34298, end, value2:678910, data2:48957, end, value3:56546, data3:83576, end";

var matches = Regex.Matches(myString, @"value[0-9]*\:([0-9]+) *, * data[0-9]*\:([0-9]+)");
var pairs = new List<(int key, int value)>();
foreach (Match match in matches)
{
    var a = int.Parse(match.Groups[1].ToString());
    var b = int.Parse(match.Groups[2].ToString());
    pairs.Add((a,b));
}

Upvotes: 2

Matt
Matt

Reputation: 1757

1st you can't just loop through a string like that, you will need to split it.

Under the following assumptions:

  1. The structure Value:#, Data:#, end never changes
  2. The number for value and data are always integers

then the following will work.

string myString             = "value:12345, data:34298, end, value2:678910, data2:48957, end, value3:56546, data3:83576, end";
List<Tuple<int, int>> tuple = new List<Tuple<int, int>>();

// first split the string on commas so we have a collection to loop through
string[] splitString = myString.Split(',');

// Assuming the Value:#, Data:#, end structure, we can loop by 3 each time
for (int i = 0; i < splitString.Length; i = i+3)
{
    /* Assuming the above structure, we can be sure that
    * i = Value
    * i+1 = Data
    * i+2 = end - which we can ignore
    * all will be integers by assumption so no need to check, just parse
    * Always in string:# pattern so we split on : and take the second item
    */
    int value = Int32.Parse(splitString[i].Split(':')[1]);
    int data  = Int32.Parse(splitString[i + 1].Split(':')[1]);

    tuple.Add(Tuple.Create(value, data));
}

Upvotes: 2

Related Questions