Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split 16 digit string into 4 parts and store them in an array in C#

I have a string composed of 16 digits (a hexadecimal number), which will be entered in a textbox as one large number. For example, '1111222233334444".

I need to

  • read this number in,
  • divide it into four groups, such as 1111 2222 3333 4444.
  • store the groups into four variables, or an array

I have found some methods to do this, but they just write to console. So after the user enters that data, I need to have something like:

    string first = 1111; 
    string second = 2222; 
    string third = 3333; 
    string fourth = 4444.

Any help is appreciated!

like image 349
user1789259 Avatar asked Dec 20 '22 14:12

user1789259


2 Answers

You can do it with substring.

string strNumber = "1111222233334444";

string []strArr = new string[4];

for(int i=0; i < 4; i++)
{
   strArr[i] = strNumber.Substring(i*4, 4);
}
like image 125
Adil Avatar answered May 16 '23 00:05

Adil


Here it is:

string initial_string = TextBox1.Text;  //read from textbox 

string [] number = new string[4];

number[0] = initial_string.Substring(0,4);
number[1] = initial_string.Substring(4,4);
number[2] = initial_string.Substring(8,4);
number[3] = initial_string.Substring(12,4);
like image 43
888 Avatar answered May 16 '23 01:05

888