471,337 Members | 843 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Var type

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
Dec 2 '05 #1
4 1412
Maarten wrote:
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

Dec 2 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Maarten" <gu******@hotmail.com> wrote in message
news:43***********************@news.skynet.be...
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

Dec 2 '05 #3
Thanks
it works fine now

Regards Maarten.


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uo*************@TK2MSFTNGP09.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Maarten" <gu******@hotmail.com> wrote in message
news:43***********************@news.skynet.be...
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


Dec 2 '05 #4
Nicholas Paldino [.NET/C# MVP] wrote:
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.)

Dec 2 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Batista, Facundo | last post: by
6 posts views Thread by S.Tobias | last post: by
reply views Thread by Chris Fink | last post: by
1 post views Thread by Rob Griffiths | last post: by
669 posts views Thread by Xah Lee | last post: by
5 posts views Thread by JH | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.