How to separate the digits of a variable in C # in pairs?

2

How do I get a variable that has a 4-digit value, divide those digits into pairs and save them to other variables?

example: 3025 = 30 25

    
asked by anonymous 30.05.2018 / 03:59

3 answers

3

If you want to divide any integer, without converting to string , and with support for arbitrary grouping size:

using System.Collection.Generic;

long[] DividirNumero(long valor, int tamanhoGrupo)
{ 
    if(tamanhoGrupo <= 0)
        return new [] {valor};

    int localTamanhoGrupo = (int) Math.Pow(10, tamanhoGrupo);
    Stack<long> resultado = new Stack<long>();
    valor = Math.Abs(valor);

    do 
    {
        resultado.Push(valor % localTamanhoGrupo);
        valor /= localTamanhoGrupo;

    } while (valor > 0);

    return resultado.ToArray();
}

// 1234 (tamanho agrupamento 0) => 1234
// 1234 (tamanho agrupamento 2) => 12 34
// 1234 (tamanho agrupamento 3) => 1 234
// 123456789 (tamanho agrupamento 2) => 1 23 45 67 89
// 123456789 (tamanho agrupamento 6) => 123 456789
// 123456789 (tamanho agrupamento 1) => 1 2 3 4 5 6 7 8 9
// 9223372036854775807 (tamanho agrupamento 3) => 9 223 372 36 854 775 807
// 2147483647 (tamanho agrupamento 2) => 21 47 48 36 47
// -2147483648 (tamanho agrupamento 2) => 21 47 48 36 48

(You can see an example here ).

    
30.05.2018 / 09:47
0

The question is very vague, you can not know the type of the variable and what the purpose of it would be, but considering it is a string, you can do this:

Example using 5 values to exemplify:

using System;

public class Program
{
    public static void Main()
    {
        string[] valores = new string[]{"3025","302","","1","12345"};


        for (int i =0; i < valores.Length; i++)
        {
            Console.WriteLine("Valor " + (i + 1)+":");
            string p1 = valores[i].Substring(0,valores[i].Length/2);
            string p2 = valores[i].Substring(valores[i].Length/2);


            Console.WriteLine("Parte 1: "+ p1);
            Console.WriteLine("Parte 2: "+ p2);
            Console.WriteLine("------------");

        }

    }
}
  

Result:

Valor 1:
Parte 1: 30
Parte 2: 25
------------
Valor 2:
Parte 1: 3
Parte 2: 02
------------
Valor 3:
Parte 1: 
Parte 2: 
------------
Valor 4:
Parte 1: 
Parte 2: 1
------------
Valor 5:
Parte 1: 12
Parte 2: 345
    
30.05.2018 / 04:23
0

This method supports any element type, as long as it is enumerable. In principle, declare the following method:

    /// <summary>
    /// Splits an array into several smaller arrays.
    /// </summary>
    /// <typeparam name="T">The type of the array.</typeparam>
    /// <param name="array">The array to split.</param>
    /// <param name="size">The size of the smaller arrays.</param>
    /// <returns>An array containing smaller arrays.</returns>
    internal static IEnumerable<IEnumerable<T>> Split<T>(T[] array, int size)
    {
        for (var i = 0; i < (float)array.Length / size; i++)
        {
            yield return array.Skip(i * size).Take(size);
        }
    }

To use in a String , you will do the following:

string palavra = "30250892";
IEnumerable<IEnumerable<char>> conjuntos = Split(palavra.ToCharArray(), 2); // não se esqueça do ToCharArray()

foreach (IEnumerable<char> par in conjuntos) {
     string parFormatado = string.Join("", par);
     Console.WriteLine(parFormatado);
}

// 30
// 25
// 08
// 92

You can use this method in any type, as long as it is enumerated (in lists).

Method Credits Split .

    
30.05.2018 / 06:01