Pham Cong The Anh
Pham Cong The Anh

Reputation: 13

How to Extract Data From a String Into Two Arrays

I have string which contains two sets of data, call it 1 and 2 respectively. In the data strings each are separated by symbol @ or & like:

@data1&data2@data1&data2@....

I want to extract it to become an array such as x[] = all the data1, y[] = data2

I tried the method strtok but after processing of the string the method gave me an inverted string like this:

"abcd" becomes an array of d c b a

Which does not work for my situation because I need to split it into two arrays in the order in which it was found.

Upvotes: 0

Views: 177

Answers (3)

ΩmegaMan
ΩmegaMan

Reputation: 31616

Regular expressions allow one to parse and extract based on patterns within the "reg-ex" language.


For example to create a pattern follow these steps.

  1. One knows that there is a literal character @ and a literal character &. So an initial pattern contains @ &.
  2. Then we need to match and capture the text after those items to put into X and Y. That is done in regular expression by specifying a match capture set of ( ). So now our pattern looks like this @()&().
  3. To match any text one can say \w+. Which says we are looking for a word type character (a-Z and 0-9) which can be represented as a \w. Since we are looking for 1 or more characters, we add a + to the \w making it \w+. Now our pattern looks like this @(\w+)&(\w+)
  4. With the pattern we expect two sets of data to be returned from the regex parser which will return two Match instances.
  5. Now we extract the data from the match sets.
  6. With the two sets of data, an X and Y, we will create individual arrays.

string data = "@data1&something1@data2&something2";

string pattern = @"@(\w+)&(\w+)"; 
// The C# literal character `@` must precede the string holding regex pattern.

var matches = Regex.Matches(data, pattern)
                   .OfType<Match>()
                   .Select(mt => new { X = mt.Groups[1].Value, Y = mt.Groups[2].Value })
                   .ToList();

// See what the data looks like in `matches`

var XArray = matches.Select(item => item.X).ToList(); 

// See what the data looks like in "XArray"

var YArray = matches.Select(item => item.Y).ToList();

// See what the data looks like in "YArray"

enter image description here


To Learn More See

Even though the documentation is for Visual Studio 2010, it still is ok and good to use.

Upvotes: 0

Kien Chu
Kien Chu

Reputation: 4895

Assuming your data format is unchanged

string input = "@data1&data2@data3&data4";

var keyValues = input.Split(new[] { '@'}, StringSplitOptions.RemoveEmptyEntries) // split to ["data1&data2", "data3&data4"]
                .Select(i => i.Split('&')) // split to [ ["data1", "data2"], ["data3" , "data4"] ]
                .Select(k => new KeyValuePair<string, string>(k[0], k[1]));

var x = keyValues.Select(s => s.Key); // x array
var y = keyValues.Select(s => s.Value); // y array

Upvotes: 0

Barsik the Cat
Barsik the Cat

Reputation: 373

I have no idea what exactly is stored in your data1, data2, etc, but it seems like you need to use Split() method of your string.

string input = "@data1&data2@data1&data2@";
string[] splitInput = input.Split({'@', '&'});

Each element of splitInput will contain one of your data blocks.

Upvotes: 1

Related Questions