Paul
Paul

Reputation: 3142

String extraction in C#

I'm getting pretty frustrated with this, and hope the community can help me out.

I have a string, an example would be "1_ks_Males", another example would be "12_ks_Females".

What I need to do is write a method that extract's each value. So from the first example I'd want something like this:

1 ks Males

In separate variables.

I'm sure I'm just being incredibly thick, but I just can't get it!

Upvotes: 5

Views: 727

Answers (14)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

Simply use string.Split('_'). With your input strings it will return a string array with three elements.

Upvotes: 15

Glenn
Glenn

Reputation: 1975

string[] array =  "1_ks_Males".Split('_');

Assert.AreEqual("1",array[0])
Assert.AreEqual("ks",array[1])
Assert.AreEqual("Males",array[2])

Upvotes: 7

Oleg Dok
Oleg Dok

Reputation: 21766

Use Split function of the string for it:

var variables = "1_ks_Males".Split(new char[]{'_'}, StringSplitOptions.IgnoreEmpty);

Now variables[0] == "1", variables[1] == "ks", and variables[2] == "Males"

Upvotes: 4

Amar Palsapure
Amar Palsapure

Reputation: 9680

You can use Split function provided by String. Read more about it @ MSDN

var data = "1_ks_Males".Split('_');

Upvotes: 3

gabsferreira
gabsferreira

Reputation: 3137

var splitVar =  "1_ks_Males".Split('_');
var firstVar = splitVar[0];
var secondVar = splitVar[1];
var thirdVar = splitVar[2];

Upvotes: 4

CraigTP
CraigTP

Reputation: 44909

You'll want to look into the String.Split method of the String class. Here's the MSDN link.

Basically, if all of your strings have the values that you require separated by a consistent character (in your example, this is an underscore character), you can use the Split method which will split a single string into an array of new strings based upon a specific separator.

For example:

string s = "1_ks_Males";
string[] v = s.Split('_');
Console.WriteLine(v[0]);
Console.WriteLine(v[1]);
Console.WriteLine(v[2]);

would output:

1
ks
Males

Upvotes: 5

ipr101
ipr101

Reputation: 24236

You could use split -

var s = "1_ks_Males";
string[] values = s.Split('_');

Your values will then be contained in the `values' array -

var firstvalue = values[0];
var secondvalue = values[1];
var thirdvalue = values[2];

Upvotes: 5

Jason
Jason

Reputation: 15931

use String.Split which returns an array of values

var values = "12_ks_Females".split("_");
// values[0] == "12"
// values[1] == "ks"
// values[2] == "Females"

Upvotes: 5

Lasse Espeholt
Lasse Espeholt

Reputation: 17782

var values = "1_ks_Males".Split('_');
// values[0]: 1
// values[1]: ks
// values[2]: Males

Upvotes: 6

Richard Dalton
Richard Dalton

Reputation: 35793

You just need to use split:

var exampleString = "1_ks_Males";
var split = exampleString.split("_");

var first= split[0]; // 1
var second = split[1]; // ks
var third = split[2]; // Males

Upvotes: 7

Roy Dictus
Roy Dictus

Reputation: 33139

How about this?

var data = myString.Split("_");
var value = data[0];
var @type = data[1];
var gender = data[2];

Upvotes: 5

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

This should do the trick:

var values = myString.Split('_');

Upvotes: 4

Amar Palsapure
Amar Palsapure

Reputation: 9680

You can use Split function for String. Something like this

var split =  "1_ks_Males".Split('_');
var first = split[0];
var second = split[1];
var third = split[2];

Upvotes: 9

Adrian Fâciu
Adrian Fâciu

Reputation: 12552

You should use the String.Split method.

Like: string[] splitParts = "1_ks_Males".Split('_');

Upvotes: 4

Related Questions