Help C# >> how to explode a string?

iam so begginer in c# . .

just started it few days ago …

I have a float number ( 3.5 ) and I won’t to explode it so that I can get 3 and 5 each other alone …

in PHP i’d do :

$a=explode (".",$string);
print $a[0]; // 3
print $a[1]; // 5

don’t know about c# … which function to use ? :cry:

The first thing to realize is that C#is strongly typed, and a float is not the same as a string. PHP could do internal conversion.

Anyways the first thing to do is to cast the float to a string, then you can split it.

 			float f = (float)3.5;
 			string s = f.ToString();
 			string[] split = s.Split(".".ToCharArray());
 			//split[0] holds 3
 			//split[1] holds 5

// C# Code
float val = 3.5f;
string strVal = val.ToString();
string[] exploded = strVal.Split('.');


float val = 3.5f;
string strVal = val.ToString();
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"\\.");
string[] exploded = r.Split(strVal);

In this case, I would use the first example. Since the 2nd is overkill for this sample. Just reminding that Regular Expression is handy!

And yes, C# is strongly typed, but everything derives from object, and object provides us with a virtual ToString method, which all intrinsic types overload.