Binh Nguyen
Binh Nguyen

Reputation: 21

How to create array<string> from array<string> in C#

I have a arrayA

var arrayA = ["1111|0000-01-30|123456|CUSTOMER", "1111|0000-01-30|123457|CUSTOMER", "1111|0000-01-30|123458|CUSTOMER"];

How can i slipt "|" to array B

var arrayB= ["123456","123457","123458"]      

How can i do? I don't want to use for loop.

Upvotes: 0

Views: 64

Answers (1)

Ousmane D.
Ousmane D.

Reputation: 56423

Use Select to transform each splitted string into its third element and collect to an array:

arrayA.Select(s => s.Split('|')[2]).ToArray();

using System.Linq; required.

Upvotes: 3

Related Questions