1

I have the following problem. I have these strings with whitespace between them.

"+name:string"            "+age:int"

I split them with this code:

List<string> stringValueList = new List<string>();
stringValueList = System.Text.RegularExpressions.Regex.Split(stringValue, @"\s{2,}").ToList<string>();

now the elements of List looks like this

"+name:string"
"+age:int"

Now I want to split these strings and create Objects. This looks like this:

// Storing the created objects in a List of objects
List<myObject> objectList = new List<myObject>();
for(i = 1; i < stringValueList.Count ; i+=2)
{
   myObject object = new myObject();

   object.modifier = '+';
   object.name = stringValueList[i-1].Trim('+');   // out of the example the object.name should be "name"
   object.type = stringValueList[i];   // out of the example the object.type value should "string"

   objectList.Add(object);
}

At the end I should get two objects with these values:

List<myObject> objectList{ myObject object1{modifier = '+' , name ="name" , type="string"}, myObject object2{modifier='+', name="age" type="int"}}

But my result looks like this:

List<myObject> objectList {myObject object1 {modifier='+', name="name:string" type="+age:int"}}

So instead of getting 2 Objects, I am getting 1 Object. It puts both strings into the elements of the first object.

Can anyone help me out? I guess my problem is in the for loop because i-1 value is the first string in the List and i is the second string but I cant change this.

3 Answers 3

4

I guess my problem is in the for loop because i-1 value is the first string in the List and i is the second string but I cant change this.

I don't know why you do i += 2, because apparently you want to split each string in two again. So just have to change that.

Use foreach(), and inside your loop, split your string again:

foreach (var stringValue in stringValueList)
{
    myObject object = new myObject();

    var kvp = stringValue.Split(':');

    object.modifier = '+';
    object.name = kvp[0].Trim('+');
    object.type = kvp[1];

    objectList.Add(object);
}

Of course this code assumes your inputs are always valid; you'd have to add some boundary checks to make it more robust.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! It was exactly what I needed.
2

Alternatively, you could expand your Regex formula to do the whole thing in one go.

For example, with (?<=")[+](.*?):(.*?)(?="), all you'd have to do is assign the matched group values.

foreach (Match m in Regex.Matches(stringValue, "(?<=\")[+](.*?):(.*?)(?=\")"))
{
    myObject obj = new myObject
    {
        modifier = '+',
        name = m.Groups[1].Value,
        type = m.Groups[2].Value
    };
    objectList.Add(obj);
}

Comments

1

It's interesting to see how others approach a problem. I would have done something like this:

public class MyObject
{
    public char Modifier { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }

    public static IEnumerable<MyObject> Parse(string str)
    {
        return str
        .Split(' ')
        .Where(s => string.IsNullOrEmpty(s) == false)
        .ToList()
        .ForEach(i =>
        {
            var sections = i.Remove(0, 1).Split(':');
            return new MyObject()
            {
                Modifier = i[0],
                Name = sections[0],
                Type = sections[1]
            };
        });
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.