tags:

views:

513

answers:

3

I want to split a string into an array. The string is as follows:

:hello:mr.zoghal:

I would like to split it as follows:

hello mr.zoghal

I tried ...

string[] split = string.Split(new Char[] {':'});

and now I want to have:

  string  something = hello ;
  string  something1 = mr.zoghal;

How can I accomplish this?

+3  A: 

String myString = ":hello:mr.zoghal:";

string[] split = myString.Split(':');

string newString = string.Empty;

foreach(String s in split) {
 newString += "something = " + s + "; ";
}

Your output would be: something = hello; something = mr.zoghal;

Chris Lively
This will produce newString == "something = ; something = hello; something = mr.zoghal; something = ;"
Jason
Good answer, although I'd not pick concatenating a new string as sample.better go like: string something = split[0]; string something1 = split[1];I'd say learning about arrays should be a priority here rather than going over string.split semantics.
Tigraine
@Tigraine: The OP has changed his question to be semantically different from his original question.
Jason
+2  A: 

For your original request:

string myString = ":hello:mr.zoghal:";
string[] split = myString.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
var somethings = split.Select(s => String.Format("something = {0};", s));
Console.WriteLine(String.Join("\n", somethings.ToArray()));

This will produce

something = hello;
something = mr.zoghal;

in accordance to your request.

Also, the line

string[] split = string.Split(new Char[] {':'});

is not legal C#. String.Split is an instance-level method whereas your current code is either trying to invoke Split on an instance named string (not legal as "string" is a reserved keyword) or is trying to invoke a static method named Split on the class String (there is no such method).

Edit: It isn't exactly clear what you are asking. But I think that this will give you what you want:

string myString = ":hello:mr.zoghal:";
string[] split = myString.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string something = split[0];
string something1 = split[1];

Now you will have

something == "hello"

and

something1 == "mr.zoghal"

both evaluate as true. Is this what you are looking for?

Jason
Is there a reason you do `String.IsNullOrEmpty(s) == false` and not `!String.IsNullOrEmpty(s)`?
lc
Personal preference. There are no good reasons other than it is what I prefer because I have overlooked too many "!"s in my days.
Jason
sorry for my bad spell over 24 none sleep working on unkn0wn langueage is little hard , ok , yes that is what i want , But i get this error : Error 2 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments C:\Users\xxx\Documents\Visual Studio 2008\Projects\vir-fu\vir-fu\Program.cs 167 30 vir-fu
@zimzim: Use string[] split = myString.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
Jason
+1  A: 

It is much easier than that. There is already an option.

string mystring = ":hello:mr.zoghal:";
string[] split = mystring.Split(new char[] {':'}, StringSplitOptions.RemoveEmptyEntries);
Bryce Kahle