Hyrein
Hyrein

Reputation: 391

Array Indexer to string builder

Code :

            string[] X = Regex.Split(X, ",");
            string[] A = Regex.Split(A, ",");
            string[] B = Regex.Split(B, ",");
            string[] C= Regex.Split(C, ",");
            string[] D = Regex.Split(D, ",");



            for (int i = 0; i < splitfirsts.Length; i++)
            {
                Console.WriteLine("{0}{1}{2}{3}{4}",
                      X[i], A[i], B[i], C[i], D[i]);               
            }

The information inside string[] :

X = 1,2,3

A = 4,5,6

B = 7,8,9

C = Adam,Mark,Tom

D = 16,21,88

The Result will Return :

147Adam16

258Mark21

369Tom88

What i trying to achieve :

string newstring = 147Adam16258Mark21369Tom88

It's there anyway to achieve it with a simple solution?

Upvotes: 0

Views: 83

Answers (2)

Ritwik Sen
Ritwik Sen

Reputation: 296

string[] X = Regex.Split(X, ",");
string[] A = Regex.Split(A, ",");
string[] B = Regex.Split(B, ",");
string[] C= Regex.Split(C, ",");
string[] D = Regex.Split(D, ",");

StringBuilder sb = new StringBuilder();

for (int i = 0; i < splitfirsts.Length; i++)
{ 
  sb.AppendFormat("{0}{1}{2}{3}{4}",
          X[i], A[i], B[i], C[i], D[i]);             
}

Console.WriteLine(sb.ToString());

Upvotes: 0

mindOfAi
mindOfAi

Reputation: 4622

You can achieve this without using string builder. If you want to just output 147Adam16258Mark21369Tom88, you can use Console.Write. Try this:

for (int i = 0; i < splitfirsts.Length; i++)
        {

            Console.Write("{0}{1}{2}{3}{4}",
                      X[i], A[i], B[i], C[i], D[i]);
        }

But, if you want to store it, you can use StringBuilder by this:

StringBuilder sb = new StringBuilder();

        for (int i = 0; i < splitfirsts.Length; i++)
        {
            sb.AppendFormat("{0}{1}{2}{3}{4}",
              X[i], A[i], B[i], C[i], D[i]);

        }

Console.Write(sb);

Hope it helps!

Upvotes: 1

Related Questions