Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by PlausiblyDamp
i'm not lazy i'm just resting before i get tired.
  • Leaders
Posted

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 = {':'};

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

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

  • *Experts*
Posted

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

"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
Posted

thanks for the help guys

i'm switching to Regex i think

it seems more fit to do the job i want

 

thanks

brand0on

i'm not lazy i'm just resting before i get tired.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...