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

Asked

Viewed 251 times

2

How I take a variable that has a value of 4 digits, divide these digits into pairs and save them into other variables?

example: 3025 = 30 25

3 answers

3

If you want to split any integer number, do not convert to string, and with support for arbitrary cluster 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).

0

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

Example using 5 values to illustrate:

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("------------");

        }

    }
}

Upshot:

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

0

This method supports any type of element, provided 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’ll 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 listed (in lists).


Method credits Split.

Browser other questions tagged

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