简体   繁体   中英

How to get specific parts of string and assign them to variables in c#?

passing a string like this to the code behind:

0,-1|1,-1|2,-1|3,-1|4,-1|5,-1|6,-1|7,-1|8,-1

I need to be able to asign the values before and after the "," symbol per each "|" symbol that exits in the string, into separated variables, "line" for first value (before the ",") and "group" for the next one (after the ",").

Right now I'm trying with this, but I'm having some issues with converting from string[] to string.

public static string GuardarGrupos(string parametro){


    var data = parametro.Split(Convert.ToChar("|"));

    var datos = "";
    string[] linea;
    var grupo = "";

    //Iterate through each of the letters
    foreach (var check in data)
    {

        datos = data[0];
        linea = data[0].Split(Convert.ToChar(","));

    }

    return linea;
}

Any Idea how can I achieve this?

Make a class or struct to hold your values:

public class DataObject
{
   public string X {get; set;}

   public string Y {get; set;}
}

Return a List<T> of type DataObject

public static List<DataObject> GuardarGrupos(string parametro){
    //List to return
    List<DataObject> returnList = new List<DataObject>();

    //Split the string on pipe to get each set of values
    var data = parametro.Split('|'); //No need to do a convert.char(), 
    //String.Split has an overload that takes a character, use single quotes for character

    //Iterate through each of the letters
    foreach (var check in data)
    {
       //Split on comma to get the individual values
       string[] values = check.Split(',');
       DataObject do = new DataObject()
       {
          X = values[0];
          Y = values[1];
       };
       returnList.Add(do);
    }

    return returnList;
}

Once you have the List<DataObject> , you can form line and group using linq and string.Join :

List<DataObject> myList = GuardarGrupos("0,-1|1,-1|2,-1|3,-1|4,-1|5,-1|6,-1|7,-1|8,-1");
string line = string.Join(",", myList.Select(x => x.X);
string group = string.Join(",", myList.Select(y => y.Y);

Instead of using local variables , create a Class that stores your retrieved values. then in the main you could handle/manipulate those values as required.

    public class MyData
    {
        public string Datos { get; set; }
        public string Linea { get; set; }
        public string Grupo { get; set; }
    }

    public static List<MyData> myFunction(string parametro)
    {
        List<MyData> result = new List<MyData>();
        var data = parametro.Split(Convert.ToChar("|"));

        foreach (var check in data)
        {
            MyData temp = new MyData();
            var line = check.Split(',');
            temp.Datos = line[0];
            temp.Linea = line[1];
            temp.Grupo = check;

            result.Add(temp);
        }

        return result;
    }

    static void Main(string[] args)
    {
        var t = myFunction("0,-1|1,-1|2,-1|3,-1|4,-1|5,-1|6,-1|7,-1|8,-1");
    } 

Here is a robust solution that's just a simple iteration over a string.

void Main()
{
    var xs = "0,-1|1,-1|2,-1|3,-1|4,-1|5,-1|6,-1|7,-1|8,-1";

    var stack = new Stack<Pair>();
    stack.Push(new Pair());

    foreach(var x in xs)
        if(x == '|')
            stack.Push(new UserQuery.Pair());
        else
            stack.Peek().Accept(x);

    foreach (var x in stack.Reverse())      
        Console.WriteLine(x);       
}

sealed class Pair
{
    Action<char> _Accept;

    readonly List<char> Line = new List<char>();
    readonly List<char> Group = new List<char>();

    public Pair()
    {
        _Accept = x => Line.Add(x);
    }

    public void Accept(char c)
    {
        if(c == ',')
            _Accept = x => Group.Add(x);
        else        
            _Accept(c);
    }

    public override string ToString()
    {
        return "Line:" + new string(Line.ToArray()) + " Group:" + new string(Group.ToArray());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM