Substring / Split String help
-
Hi I have text as an output that is in the following format. Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 What im trying to do is to substring/split this text so i can put it in a datagrid or list view. Idealy I need the text in the following format so I can do this but I am having trouble trying to figure out the c# behind this. Ideally the text would output as, Example install cdb 000 Firstname LastName Not Started Example install cdb 000 Firstname LastName Not Started etc, for each entry in the source text. (2)Normal02/09/2013 18:0002/09/2013 23:00426 (remove this text from the string) The problem is that I would have to loop through all the text from the source to get this output. But the source text above is not always that length and could contain more entries. Any ideas on this one? I would apreciate any help anyone can give. Thanks
-
Hi I have text as an output that is in the following format. Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 What im trying to do is to substring/split this text so i can put it in a datagrid or list view. Idealy I need the text in the following format so I can do this but I am having trouble trying to figure out the c# behind this. Ideally the text would output as, Example install cdb 000 Firstname LastName Not Started Example install cdb 000 Firstname LastName Not Started etc, for each entry in the source text. (2)Normal02/09/2013 18:0002/09/2013 23:00426 (remove this text from the string) The problem is that I would have to loop through all the text from the source to get this output. But the source text above is not always that length and could contain more entries. Any ideas on this one? I would apreciate any help anyone can give. Thanks
Probably the best way to parse this (you did't specify if there are seperators between fields!) would be a rather complex regular expression. This expression would have to take into account the possible values in certain columns, such as that "Not Started" column, to server as boundaries that the RegEx parser can use to find other fields, like first and last name.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Probably the best way to parse this (you did't specify if there are seperators between fields!) would be a rather complex regular expression. This expression would have to take into account the possible values in certain columns, such as that "Not Started" column, to server as boundaries that the RegEx parser can use to find other fields, like first and last name.
A guide to posting questions on CodeProject[^]
Dave KreskowiakThanks dave, And I didnt supply if there were seperators as the source text is the format it is supplied in. Also the firstname/lastname is an example this would actually have someones name. thanks
-
Thanks dave, And I didnt supply if there were seperators as the source text is the format it is supplied in. Also the firstname/lastname is an example this would actually have someones name. thanks
Member 10119837 wrote:
And I didnt supply if there were seperators as the source text is the format it is supplied in.
OK, then the answer doesn't change. If there is no seperator between fields and no distinct column width for each field, String.Split and String.SubString will not help you. RegEx is about the only way you're going to get this done.
Member 10119837 wrote:
Also the firstname/lastname is an example this would actually have someones name.
Says Mr. Obvious. What may not be so ovious is that some people go by one name and others go by more than two, so your going to have to make sure you test those possibilities.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi I have text as an output that is in the following format. Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 What im trying to do is to substring/split this text so i can put it in a datagrid or list view. Idealy I need the text in the following format so I can do this but I am having trouble trying to figure out the c# behind this. Ideally the text would output as, Example install cdb 000 Firstname LastName Not Started Example install cdb 000 Firstname LastName Not Started etc, for each entry in the source text. (2)Normal02/09/2013 18:0002/09/2013 23:00426 (remove this text from the string) The problem is that I would have to loop through all the text from the source to get this output. But the source text above is not always that length and could contain more entries. Any ideas on this one? I would apreciate any help anyone can give. Thanks
I'm going to assume, based on what you describe, that you have no control over the "batch" of "entries" to be parsed here. Is that correct ? The second issue is: to what extent is there any possible variation in the content of each "entry" you need to parse, and re-format ? As Dave pointed out to you, is it possible that the "name field" in each entry could vary, as in: Robert Smith; Robert T. Smith; Robert Thomas Smith; R. T. Smith III; Robert Smith 3rd. ... and so forth. The extent to which you can establish the "variation" possible in the entries imho is going to determine the complexity of either: 1. creating a RegEx as Dave suggested. 2. taking your "batch" and transforming it into another "batch" in which you have inserted de-limiters so that you can, then, use string.Split. I would likely write code to transform the original data (file, stream, whatever) and insert delimiters, simply because I am not expert in RegEx, and the C# code I'd write, imho, might be more maintainable, and re-usable. However, I would not doubt that someone like Dave could write a documented RegEx expression of great internal complexity that would be equally maintainable, and re-usable. So your choice may depend on what you are most expert in using now, and, given a need, for other people in the future to update, maintain, re-use, your code what those people are likely to be expert in. Now, if the data was in XML format, you might be looking at defining an xsd to handle transformation. good luck, Bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
-
Hi I have text as an output that is in the following format. Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 Example install cdb 000 Firstname LastNameNot Started(2) Normal02/09/2013 18:0002/09/2013 23:00426 What im trying to do is to substring/split this text so i can put it in a datagrid or list view. Idealy I need the text in the following format so I can do this but I am having trouble trying to figure out the c# behind this. Ideally the text would output as, Example install cdb 000 Firstname LastName Not Started Example install cdb 000 Firstname LastName Not Started etc, for each entry in the source text. (2)Normal02/09/2013 18:0002/09/2013 23:00426 (remove this text from the string) The problem is that I would have to loop through all the text from the source to get this output. But the source text above is not always that length and could contain more entries. Any ideas on this one? I would apreciate any help anyone can give. Thanks
if there are separators then you typical use split. split on value, split on line count ,typical of csv file type. Now, you also can have a file that is fixed length fields.. Split then on field count then you just substing(x,y) out and prob split on line feed. Now, if your trying to sort data out of something that follows no standard then now your looking at some sort of parsing method. either reg expressions or loops.
=)