How to separate a String according to a separator in C#?

Asked

Viewed 11,852 times

7

I have a string in this format:

string valores = "Numero1#Numero2#Numero3#Numero4#";

How do I tell it in an array of integers to get it:

int Numero[1] = Numero1;
int Numero[2] = Numero2;
int Numero[3] = Numero3;
int Numero[4] = Numero4;

I tried something like this but it didn’t roll:

cod_cg_meta_periodo.Split("#",System.StringSplitOptions.RemoveEmptyEntries);
  • How did it not happen? It was wrong? It gave an unexpected result?

  • Error appeared Error 64 The best overloaded method match for 'string.Split(params char[])' has some invalid Arguments

  • Your call does not work because the first parameter must be an array of chars, not a single char.

  • I edited my answer to exemplify the correct use.

  • Only one question @Joaopaulo is a string array so ?

3 answers

11


You can use the method string.Split:

var array = valores.Split('#');

If you want to pass options, you have to do so:

var array = valores.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);

Or when the separator is a more complex string:

var array = valores.Split(new string[] { "#" }, // lista de separadores complexos
                          StringSplitOptions.RemoveEmptyEntries);
  • 1

    The middle one worked great. I thought I could only use one string character to do the splitting, but it asks for the char array itself. When allow milestone as resolved. Thank you!

  • 1

    Just one more thing. How do I use the values? The first string will be array[0] ?

  • 1

    In the string you used in the question, using the option RemoveEmptyEntries, this is how the array: [0] => "Numero1"; [1] => "Numero2"; [2] => "Numero3" and [3] => "Numero4".

4

String array

string valores = "numero1#numero2#numero3#numero4#";
string[] itemValores = valores.Split('#');

To read their positions

foreach (var item in itemValores)
{
    //item tem o valor de cada item da lista itemValores            
}

if (itemValores.Count() > 0)
{
   var str = itemValores[0];
}

4

If the goal is to interpret the numbers in the string as integers, you can do the following:
To:

string valores = "1#2#3#4#";
int[] numeros;

Separates the string and creates an array to store the numbers.

string[] numeros_str = valores.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
numeros = new int[numeros_str.Length];

Interprets strings containing numbers.

for(int i = 0; i < numeros.Length; i++)
{
    numeros[i] = Int32.Parse(numeros_str[i]);
}

Another option, I think is better, is using LINQ:

string valores = "1#2#3#4#";
int[] numeros = valores.Split(new string[] {"#"}, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
  • 4

    With LINQ also looks nice like this: valores.Split('#').Select(Int32.Parse).ToArray()

  • 2

    Actually, it gets better. I’ll edit it. Thank you.

Browser other questions tagged

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