Manipulate String C#

Asked

Viewed 257 times

3

I have strings that come so:

"Cadastro,Novo"
"Cadastro,Editar"
"Cadastro,Excluir"
"Cadastro,Morfar"
"Cadastro,Etc"

"Entidade,Novo"
"Entidade,Editar"
"Entidade,Excluir"
"Entidade,Morfar"
"Entidade,Etc"

And I have two columns I intend to fill:

Coluna1 | Coluna2

The order must be as follows:

Coluna1 = Cadastro 
Coluna2 = "Novo,Editar,Excluir,OqueVim"

But I cannot repeat the value Register when I will concatenate the values in Coluna2.

Can someone explain to me how to do this?

My code is like this:

Class to Add Final Values

public class ListaFinal
{
  public string valor1 { get; set; }
  public string valor2 { get; set; }
}

Code:

 var listaFinal = new List<ListaFinal>();
      var adiciona = new List<string>();
      adiciona.Add("Cadastro.Novo");
      adiciona.Add("Cadastro.Editar");
      adiciona.Add("Cadastro.Deletar");
      adiciona.Add("Cadastro.Excluir");
      adiciona.Add("Cadastro.ALGO");

      adiciona.Add("Entidade.Item1");
      adiciona.Add("Entidade.Enti");
      adiciona.Add("Entidade.dade");
      adiciona.Add("Entidade.outro");
      adiciona.Add("Entidade.test");

      string coluna1 = "";
      string coluna2 = "";
      foreach (var x in adiciona)
      {
        var aux = "";
        coluna2 = x.Split('.')[1];
        if (string.IsNullOrEmpty(coluna1))
        {
          coluna1 = x.Split('.')[0];

          listaFinal.Add(new ListaFinal
          {
            valor1 = coluna1,
            valor2 = coluna2
          });
        }
        else
        {
          aux = x.Split('.')[0];
          if (aux != coluna1)
          {
            coluna1 = aux;

            listaFinal.Add(new ListaFinal
            {
              valor1 = coluna1,
              valor2 = coluna2
            });

          }

        }
        coluna1 = coluna1;
      };

Final result: 2 items in the list, with the fields before the comma without repeating, but I have not concatenated the values after the comma.

  • You mean that every value before the comma You want to have all the values that appear to it after the comma?

  • Yes, but without repeating the value that is before the comma

1 answer

3


Use String.Split (http://msdn.microsoft.com/pt-br/library/b873y76a(v=vs.110). aspx):

var strings = suaString.Split(',');

In the case, strings is a Array.

As requested in comment, to use in the form of dictionaries (or maps), in C# works as follows:

var dict = new Dictionary<string, string[]>();
var subarray = new string[strings.Length - 1];
Array.Copy(strings, 1, subarray, 0, dict.Length - 1)
dict.Add(strings[0], subarray);

EDIT

After the author of the question placed the code, I modified it to the following:

        var listaFinal = new Dictionary<String, List<String>>();
        var adiciona = new List<string>();
        adiciona.Add("Cadastro.Novo");
        adiciona.Add("Cadastro.Editar");
        adiciona.Add("Cadastro.Deletar");
        adiciona.Add("Cadastro.Excluir");
        adiciona.Add("Cadastro.ALGO");

        adiciona.Add("Entidade.Item1");
        adiciona.Add("Entidade.Enti");
        adiciona.Add("Entidade.dade");
        adiciona.Add("Entidade.outro");
        adiciona.Add("Entidade.test");

        foreach (var x in adiciona)
        {
            var s = x.Split('.').ToList();
            if (!listaFinal.ContainsKey(s.First())) listaFinal[s.First()] = new List<string>();
            listaFinal[s.First()].AddRange(s.Skip(1).ToList());
        };

        foreach (var chaveValor in listaFinal)
        {
            // chaveValor.Key contém o elemento "Cadastro" ou "Entidade".
            // chaveValor.Value contém uma lista de strings que é o valor depois do  ponto de cada elemento.
        }
  • Gypsy, I don’t know C#, so Voce can expand your response so that Voce stores each strings[1:] on a map or similar to C# with strings[0]?

  • I got it. I’m expanding.

  • I made a code here, it returns me without repeating, but does not concatenate the value after the comma, it is replacing

  • @Can Rod please put this code in the question?

  • I just edited the answer, I hope to understand

  • He just took the last amount :/

  • @Rod Verdade. I fixed it.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.