parse a string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to parse through a string for each number and then run a command.
Each number only which represents a employee id.

string a = "AGENTS\n093333\n8883773\n3338373\nEND OF RESULTS"


When I press a submit button I want this to execute
for each employee id i will have it run a command or somthing.

Im not sure how to write a tokenizer or string split.
I am new to programming im not sure about a for loop either.

Thanks for any examples.
 
Well you're in luck. The .NET string object has a Split method that returns an array of strings. Simply call it likes this:
string[] empIDs = a.Split("\n");

empIDs[1] should contain your first employee ID based on the example you gave.
 
I'm not sure how to write this out and loop through the array.
I'm New sorry.....
Can you write me a short example?

Cheers,
Craig C Dallas,TX


C Addison Ritchie said:
Well you're in luck. The .NET string object has a Split method that returns an array of strings. Simply call it likes this:
string[] empIDs = a.Split("\n");

empIDs[1] should contain your first employee ID based on the example you gave.

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.


Craig C said:
I need to parse through a string for each number and then run a command.
Each number only which represents a employee id.

string a = "AGENTS\n093333\n8883773\n3338373\nEND OF RESULTS"


When I press a submit button I want this to execute
for each employee id i will have it run a command or somthing.

Im not sure how to write a tokenizer or string split.
I am new to programming im not sure about a for loop either.

Thanks for any examples.
 
Craig C said:
I need to parse through a string for each number and then run a command.
Each number only which represents a employee id.

string a = "AGENTS\n093333\n8883773\n3338373\nEND OF RESULTS"


When I press a submit button I want this to execute
for each employee id i will have it run a command or somthing.

Im not sure how to write a tokenizer or string split.
I am new to programming im not sure about a for loop either.

If you're that new to programming, I suggest you stop thinking about
what you need to do in the real world for a little while, and read a
decent tutorial or book on C#. Just approaching things one problem at a
time like this is going to take you a lot longer than getting a firm
foundation from a well-written source.
 
Craig said:
I'm not sure how to write this out and loop through the array.
I'm New sorry.....
Can you write me a short example?

Cheers,
Craig C Dallas,TX


:

Well you're in luck. The .NET string object has a Split method that returns an array of strings. Simply call it likes this:
string[] empIDs = a.Split("\n");

empIDs[1] should contain your first employee ID based on the example you gave.

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.


:

I need to parse through a string for each number and then run a command.
Each number only which represents a employee id.

string a = "AGENTS\n093333\n8883773\n3338373\nEND OF RESULTS"


When I press a submit button I want this to execute
for each employee id i will have it run a command or somthing.

Im not sure how to write a tokenizer or string split.
I am new to programming im not sure about a for loop either.

Thanks for any examples.

This might help. (untested))

string a = "AGENTS\n093333\n8883773\n3338373\nEND OF RESULTS";

string[] splitA = a.Split("\n");

foreach( string newA in splitA)
{
if(newA.Equals("AGENTS") || newA.Equals("END OF RESULTS"))
{
//do nothign
}
else
{
//do something with one of the ids
//newA is the current emploee id
//on next loop newA is going to be the next id
}
}

Hope this helps.
Nick Z.
 
string a = "AGENTS\n093333\n8883773\n3338373\nEND OF RESULTS";
string[] empIDs = a.Split('\n');

// you now have an array with the following elements
// empIDs[0] = "AGENTS"
// empIDs[1] = "093333"
// empIDs[2] = "8883773"
// empIDs[3] = "3338373"
// empIDs[4] = "END OF RESULTS"

// since you don't want to process "AGENTS" or "END OF RESULTS"
// start at element 1 and finish at (number of elements - 1)
for (int i = 1; i < (empIDs.Length - 1); i++)
{
// call your function here
MessageBox.Show(empIDs);
}

Might I suggest a good C# book. Sams Publishing "C# Unleashed" is a good one. Or this one http://www.amazon.com/exec/obidos/t...ding=UTF8&no=283155&me=ATVPDKIKX0DER&st=books which covers the new 2.0 features.

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.


Craig C said:
I'm not sure how to write this out and loop through the array.
I'm New sorry.....
Can you write me a short example?

Cheers,
Craig C Dallas,TX


C Addison Ritchie said:
Well you're in luck. The .NET string object has a Split method that returns an array of strings. Simply call it likes this:
string[] empIDs = a.Split("\n");

empIDs[1] should contain your first employee ID based on the example you gave.

--
C Addison Ritchie, MCSD
Ritch Consulting, Inc.


Craig C said:
I need to parse through a string for each number and then run a command.
Each number only which represents a employee id.

string a = "AGENTS\n093333\n8883773\n3338373\nEND OF RESULTS"


When I press a submit button I want this to execute
for each employee id i will have it run a command or somthing.

Im not sure how to write a tokenizer or string split.
I am new to programming im not sure about a for loop either.

Thanks for any examples.
 
Yeah well there arent very many begginner C# books that discuss the basics that I have found and ver few show how to use Split.


I wrote this but cant get it to work.

string[] args;
{
char[] delim = new char[] {Convert.ToChar("\n")};
string words = "AGENTS NOT CLOSED\n094831\n040695\n324489\n073731\n005096\n183500\n088876\n135349\n060488\n003145\n083096\n990440\n004329\n095137\n060556\n104842\n350996\n079586\n017815\n079420\n159858\n131284\noutput";

string[] splitArray = words.Split( delim );
for (int x = 1; x < splitArray.Length - 1 ; x++)
{
DoSomething( splitArray[x] );
}
Console.ReadLine();
}

DoSomething(string employeeId)
Console.WriteLine( employeeId );

Im sure its a formatting problem.


Cheers,
Craig C Dallas,TX
 
Craig C said:
Yeah well there arent very many begginner C# books that discuss the
basics that I have found and ver few show how to use Split.

There are plenty of tutorials which go from the start, I think. Don't
worry even about string.Split to start with - you should be learning
the real basics of syntax to start with.
I wrote this but cant get it to work.

string[] args;
{
char[] delim = new char[] {Convert.ToChar("\n")};
string words = "AGENTS NOT CLOSED\n094831\n040695\n324489\n073731\n
005096\n183500\n088876\n135349\n060488\n003145\n083096\n990440\n
004329\n095137\n060556\n104842\n350996\n079586\n017815\n079420\n
159858\n131284\noutput";

string[] splitArray = words.Split( delim );
for (int x = 1; x < splitArray.Length - 1 ; x++)
{
DoSomething( splitArray[x] );
}
Console.ReadLine();
}

DoSomething(string employeeId)
Console.WriteLine( employeeId );

Im sure its a formatting problem.

Well, your definition of DoSomething is missing various things - a
return type and braces, and possibly an accessibility level:

void DoSomething (string employeeId)
{
Console.WriteLine (employeeId);
}
 
Back
Top