Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 5th, 2007, 05:36 PM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default split method....


The MSDN library says this is the format. However my compiler does not like it. I keep getting all kinds (like 13 errors) of crap about missing ;, ), or ].


 string s = payrollStreamReader.ReadLine();
 string ss = s.Split([""[, 2]])

where "" is my seperator and 2 is the limit for the array the elements are stored into. (As I understand it)

Help? Easier way? Also these seperate string are to be stored into another array of structs. I dont know if that helps anyone...

Seymour/
 
Old April 5th, 2007, 09:01 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hmmm. Split() automatically returns an array and I am not quite sure why you are providing an array dimension??

In your case, I am not entirely sure how to do this. Normally if I want to split on a series of characters i would do

char[] c = {'\"', ';', ','} and then just place c as the value to split on. I don't know that you can place 2 characters as a single element in a char array, but you can try it. What would work would be something like

string s = payrollStreamReader.ReadLine();
s = s.Replace('\"\"', '\"');
string[] ss = s.Split(new char[]{'\"'});

BTW, the reason you were getting the missing ; error was because you didn't escape the " as i have done in my above example.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old April 5th, 2007, 09:21 PM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

I cant tell if what you wrote is what i meant, or vice versa.

My objective is to simply split that readline() into two parts. The line it is reading will consist of "FIRSTNAME LASTNAME" , which i want to split up and put into their respective label. i.e firstname goes to one label, while lastname goes to another. I bear in mind that my method is probably far from the smartest or most efficient, but i havent really learned to manipulate strings in c# that much.
 
Old April 6th, 2007, 02:54 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
string fullName = "Joe Fawcett";
string[] names = fullName.Split(' ', 2);
Console.WriteLine("Forename: {0} , Surname: {1}", names[0], names[1]);
--

Joe (Microsoft MVP - XML)
 
Old April 6th, 2007, 12:50 PM
Authorized User
 
Join Date: Jan 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to seymour_glass
Default

OK i got everything working good except that it randomly crashes having something to do with the array, it indexes everything except the last one, and sometimes(why?) crashes as soon as i compile it for the same reason...heres the error message...

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in PayrollAssgn.exe

Additional information: Index was outside the bounds of the array.

any ideas??? it is setting off my catch block even though everything seems to be running fine inside my try..except the crashing part of course...
 
Old April 10th, 2007, 11:24 AM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is straight out of the "Beginning Visual C#" book from wrox.

private string strValue = " ";
private string s = " ";

//Cuts string into seperate words (char ?)
char[] separator = { ' ' };
string[] s;
s = strValue.Split(separator);
foreach (string word in s)
{
Console.Write("{0}" + " ", word);


 
Old May 3rd, 2007, 07:37 AM
Authorized User
 
Join Date: Dec 2004
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DZukiewicz
Default

Its crashing as there is a possiblity when you are splitting the value, the ' ' is not present. Therefore, only 1 value is returned.

So (as in joefawcett's example) names[0] will be the original text and names[1] is out of the range (as only 1 value exists).

It might be worth checking the size of the split before outputting the values:

Code:
string fullName = "DominicZukiewicz";
string[] splitName = fullName.Split(' ');

if(splitName.Length == 1)
     Console.WriteLine("Name: " {0},splitName[0]);
else
     Console.WriteLine("First Name: {0} , Surname: {1}", splitName[0], splitName[1]);
IndexOutOfRangeException means (quite obviously) that you have referenced an index in the array which doesn't exist.

I hope this helps.

Dominic





Similar Threads
Thread Thread Starter Forum Replies Last Post
To Split or Not to Split darrenb Access 2 February 8th, 2008 12:28 PM
split() darkhalf Javascript 1 October 21st, 2005 11:34 AM
Split crmpicco VB How-To 6 May 17th, 2005 04:16 AM
split crmpicco Classic ASP Basics 2 February 14th, 2005 08:48 AM
Split database rylemer Access 1 August 30th, 2004 05:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.