this-Me
this-Me

Reputation: 2145

String split and spread the result across different arrays

I have a string which gets the data in the following format

string str = "0,A 1,B 2,C 3,D 4,E";

How do i split this string in two different arrays values and quads such that

values array contains the following values :

values[0] = "0" 
values[1] = "1" 
values[2] = "2" 
values[3] = "3"
values[4] = "4"

and quads array contains

quads[0] = "A" 
quads[1] = "B" 
quads[2] = "C" 
quads[3] = "D"
quads[4] = "E"

str.Split(' ').ToArray() shall return me in this format

[0] = 0,A
[1] = 1,B
[2] = 2,C
[3] = 3,D
[4] = 4,E

I can enumerate this and populate in two different arrays in the format i need.

Is there a much shorter/simple way to do (using LINQ aggregates/Regex ) what I'm trying to achieve ?

Thanks

Thanks and Regards

Upvotes: 1

Views: 699

Answers (6)

Rex
Rex

Reputation: 367

Here's an implementation in PHP:

$a = explode(" ", "0,A 1,B 2,C 3,D 4,E");

$vals = array();
$quads = array();
$tmp = array();
$i=0;
foreach ($a as $elem) {
   $tmp = explode(",",$elem);
   $vals[$i] = $tmp[0];
   $quads[$i] = $tmp[1];
   $i++;
}

//Note: use of incremental var $i to synchronize both arrays.

Output:

vals array:

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 )

quads array:

Array ( [0] => A [1] => B [2] => C [3] => D [4] => E )

Upvotes: 0

Measurity
Measurity

Reputation: 1346

Using split string on the whitespace and afterwards on the , helps with splitting the string in pairs.

string str = "0,A 1,B 2,C 3,D 4,E";
var pairs = str.Split().Select(p => p.Split(',')).Select(p => new
                                                           {
                                                               Key = p[0],
                                                               Quad = p[1]
                                                           });
string[] keys = pairs.Select(p => p.Key).ToArray();
string[] quads = pairs.Select(p => p.Quad).ToArray();

Upvotes: 1

Dennis_E
Dennis_E

Reputation: 8894

Split takes a char[], so you can split by space AND comma. (It also returns an array, so ToArray() is not necessary)

var split = str.Split(' ', ',')
    .Select(s => s[0]) //from string[] to char[]
    .GroupBy(char.IsDigit); //group by boolean

This should return 2 groups; Then you can add .Select(g => g.ToArray()) to get 2 string arrays.

Upvotes: 2

Shaaak
Shaaak

Reputation: 585

With a little help from linq

var splitted = str.Split(' ');
var quads = splitted .Select(pair => pair.Split(',').Last()).ToArray();
var values = splitted .Select(pair => pair.Split(',').First()).ToArray();

Upvotes: 0

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13755

here how you can get your first two results

 static void Main(string[] args)
        {
            string str = "0,A 1,B 2,C 3,D 4,E";
            var values = str.Where(c => char.IsDigit(c)).ToArray();
            var quads  = str.Where(c => char.IsLetter(c)).ToArray();

        }

Upvotes: 1

Perfect28
Perfect28

Reputation: 11317

Here is a way you can do it.

string str = "0,A 1,B 2,C 3,D 4,E"; 
var firstSplit = str.Split(" ".ToCharArray()) ; 

var values = firstSplit.Select(s => Convert.ToInt32(s.Split(",".ToCharArray())[0])).ToArray() ;
var quads = firstSplit.Select(s => s.Split(",".ToCharArray())[1]).ToArray();

Upvotes: 2

Related Questions