5

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!

7
  • 7
    Wow, what a fool I feel right now. Thanks! Commented Jan 24, 2012 at 10:34
  • 3
    13 virtually identical answers within 3 minutes... Commented Jan 24, 2012 at 10:35
  • 7
    At least he can be sure now that that's the correct way to do it ;-) Commented Jan 24, 2012 at 10:35
  • 1
    My head is hanging in shame as I type... Maybe it was the extra beer last night. Commented Jan 24, 2012 at 10:36
  • please search before post a question. Commented Jan 24, 2012 at 10:48

14 Answers 14

15

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

Sign up to request clarification or add additional context in comments.

Comments

9

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];

Comments

7

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

Comments

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

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

Comments

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

Comments

5

How about this?

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

Comments

5

use String.Split which returns an array of values

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

Comments

5

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];

Comments

5

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

Comments

4

You should use the String.Split method.

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

Comments

4

This should do the trick:

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

Comments

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

3 Comments

Edit this, the downvoters may to choose to clean downvote if you correct the answer, if you delete - it never happens
@OlegDok: Downvotes on deleted answers don't count towards your reputation.
@OlegDok: Only until you recalc your reputation.
4

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"

Comments

3

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.