Var type

M

Maarten

HI,

i want to split a variable containing this -----> prog/2
i want to split on the /

the value is entered in a datagrid, but when i declare the collumn fir
stringinputs
i get an error the the inputformat is wrong.
Regex r = new Regex("(/)");

string[] s = r.Split(strvalues);

MessageBox.Show(s[0]+s[1]);

What is wrong here ?

Regards Maarten
 
J

Jon Skeet [C# MVP]

Maarten said:
i want to split a variable containing this -----> prog/2
i want to split on the /

the value is entered in a datagrid, but when i declare the collumn fir
stringinputs
i get an error the the inputformat is wrong.
Regex r = new Regex("(/)");

string[] s = r.Split(strvalues);

MessageBox.Show(s[0]+s[1]);

What is wrong here ?

Well, the first thing I'd say you're doing wrong is using regular
expressions when you're not actually splitting by a pattern - you're
just splitting on a single character, which can easily be done with
String.Split.

However, as far as I can tell, the above should work as far as I can
tell.
Could you post a short but complete program which demonstrates the
problem? Here's a short but complete program which shows it working (in
both ways - different results, note).

using System;
using System.Text.RegularExpressions;

public class Test
{
static void Main()
{
string input = "prog/2";

Console.WriteLine ("Regex.Split:");
Regex re = new Regex("(/)");
foreach (string s in re.Split(input))
{
Console.WriteLine ("'{0}'", s);
}

Console.WriteLine();

Console.WriteLine ("String.Split:");
foreach (string s in input.Split('/'))
{
Console.WriteLine ("'{0}'", s);
}
}
}

Jon
 
N

Nicholas Paldino [.NET/C# MVP]

Maarten,

If all you are doing is splitting on the "/", then a regex is overkill
here. Basically, you should use the Split method on the string class, like
so:

string[] s = strvalues.Split(new char[1]{'/'});

Hope this helps.
 
M

Maarten

Thanks
it works fine now

Regards Maarten.




Nicholas Paldino said:
Maarten,

If all you are doing is splitting on the "/", then a regex is overkill
here. Basically, you should use the Split method on the string class, like
so:

string[] s = strvalues.Split(new char[1]{'/'});

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Maarten said:
HI,

i want to split a variable containing this -----> prog/2
i want to split on the /

the value is entered in a datagrid, but when i declare the collumn fir
stringinputs
i get an error the the inputformat is wrong.
Regex r = new Regex("(/)");

string[] s = r.Split(strvalues);

MessageBox.Show(s[0]+s[1]);

What is wrong here ?

Regards Maarten
 
J

Jon Skeet [C# MVP]

Nicholas said:
If all you are doing is splitting on the "/", then a regex is overkill
here. Basically, you should use the Split method on the string class, like
so:

string[] s = strvalues.Split(new char[1]{'/'});

If you know all the characters you want to split on at compile time,
then specifying the creation of a new array is overkill. Basically, you
should use the fact that the Split method's char[] parameter is a
parameter array, like so:

string[] s = strvalues.Split('/');

:)

Jon
(Sorry, I'm in a silly mood right now.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top