Leaders dynamic_sysop Posted June 3, 2003 Leaders Posted June 3, 2003 hi does anyone know if there's an equivalent to Split in c# ? Quote
*Experts* mutant Posted June 3, 2003 *Experts* Posted June 3, 2003 Use the .Split method of a string. Quote
Leaders dynamic_sysop Posted June 3, 2003 Author Leaders Posted June 3, 2003 i'm on that thanks , although i am getting an error like this... The best overloaded method match for 'string.Split(params char[])' has some invalid arguments the way i'm trying is this... string str = "test 123"; char buff = Convert.ToChar( " "); MessageBox.Show(str.Split(buff,1)); any ideas? Quote
*Gurus* divil Posted June 3, 2003 *Gurus* Posted June 3, 2003 Try using Regex.Split instead. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted June 3, 2003 *Experts* Posted June 3, 2003 The seperator list must be a Char array, not just one Char. I belive you can just give it a standard string like this:String str = "Test 123"; MessageBox.Show(str.Split(" ", 1));and it will convert the string into a char array. Alternatively, you can do it like this: MessageBox.Show(str.Split(new Char[] { ' ' }), 1));to use more than one delimeter, you do it like this:MessageBox.Show(str.Split(new Char[] { ' ', '|', '\n' }));That will split on spaces, pipes, and line-feed characters. Remember that split can only split using characters, and not whole strings (i.e. more than one character), so it you try to do that, it will use each individual character as a delimeter, rather than the whole word. Quote
Leaders dynamic_sysop Posted June 3, 2003 Author Leaders Posted June 3, 2003 all of the above return errors:-\ this 1: String str = "Test 123"; MessageBox.Show(str.Split(new Char[] { ' ' }), 1)); highlights at the end and says "invalid expression ')'" Quote
Leaders dynamic_sysop Posted June 3, 2003 Author Leaders Posted June 3, 2003 this did it... string word = "hello i'm a test".Split(' ')[1]; MessageBox.Show(word); i got a little bit of help at the codeproject:) cheers for the quick replies though Quote
*Experts* Volte Posted June 3, 2003 *Experts* Posted June 3, 2003 The problem with the first bit of code is too many brackets on the end. Oops. :p Quote
Leaders dynamic_sysop Posted June 3, 2003 Author Leaders Posted June 3, 2003 cheers :) i thought i'd experiment with an api:-\ [DllImport("User32.Dll")] public static extern int GetWindowText(int h, StringBuilder s, int nMaxCount); //// under the form's designer area. //// private void button5_Click(object sender, System.EventArgs e) { StringBuilder strString = new StringBuilder(256); //buffer for the text we want to receive. int i; i = GetWindowText(this.button1.Handle.ToInt32(),strString,256); MessageBox.Show (strString.ToString()); } and it works:D this is cool:p Quote
*Experts* Nerseus Posted June 5, 2003 *Experts* Posted June 5, 2003 There's a params overload for Split which means you can pass a comma-delimited list of chars (or just one). As in: string s1 = "Hello World This Is Dan"; string[] s2 = s1.Split(' '); // or string[] s3 = s1.Split(' ', '.', ';'); You could also create the array, as in: string s1 = "Hello World This Is Dan"; string[] s2 = s1.Split(new char[] {' '}); // or string[] s3 = s1.Split(new char[] {' ', '.', ';'}); -Ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.