bpayne111 Posted April 28, 2004 Posted April 28, 2004 (edited) why won't this text split correctly? temp2 ="Description . . . . . . . . : Wireless USB Adapter"; if(temp2.StartsWith("Description")) { string[] splitText; char[] splitter = new char[':']; splitText = temp2.Split(splitter); Console.WriteLine(splitText[0]); } i'm stuck... any help? Edited April 28, 2004 by PlausiblyDamp Quote i'm not lazy i'm just resting before i get tired.
Leaders Iceplug Posted April 28, 2004 Leaders Posted April 28, 2004 new char[x]; Is supposed to create a new array with x number of elements... don't think a character there will do it for you. Try this: char[] splitter = {':'}; Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
PWNettle Posted April 28, 2004 Posted April 28, 2004 Another option that can work, depending on your circumstances, is to use a string literal for your split character with the .ToCharArray() method to coerce it to type Char. With this approach you don't have to declare and use an extra variable for the "splitter". For ex: splitText = temp2.Split(":".ToCharArray()); Paul Quote
*Experts* Nerseus Posted April 28, 2004 *Experts* Posted April 28, 2004 If you just have one char, just split on the char: splitText = temp2.Split(':'); The Split method has a "params" overload so you don't need to pass an array. If you had two chars to split on, you could use: splitText = temp2.Split(':', '.'); If you wanted to use an array, use this syntax: char[] splitter = new char[] {':'}; splitText = temp2.Split(splitter); NOTE: When you used: char[] splitter = new char[':']; The compiler converted the character colon (':') into it's numeric equivalent: ASCII 58. "splitter" is then initialized to be a 58 character array, with each character taking it's default value (ASCII 0, since this is a value type array). -Nerseus 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
bpayne111 Posted April 28, 2004 Author Posted April 28, 2004 thanks for the help guys i'm switching to Regex i think it seems more fit to do the job i want thanks brand0on Quote i'm not lazy i'm just resting before i get tired.
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.