Hi, thanks for your reply. I see that your solution is dependent on the fact that there is a repeating pattern in each record and that is there is exactly one white space between each property in any given record, exactly one white space between each record,
and each property appears in the same spot in each record.
What if the number of white space between each property and each record is random like when the string is too long and it must wrap to the next line or if the user put too many white spaces in by accident? Also what if each property was put in a random location
within the record by the user instead of always being in the same spot in the record, how can I compensate for that?
Hi, thanks for your reply. I see that your solution is dependent on the fact that there is a repeating pattern in each record and that is there is exactly one white space between each property in any given record, exactly one white space between each record,
and each property appears in the same spot in each record.
What if the number of white space between each property and each record is random like when the string is too long and it must wrap to the next line or if the user put too many white spaces in by accident? Also what if each property was put in a random location
within the record by the user instead of always being in the same spot in the record, how can I compensate for that?
This was not part of the original requirement. You should be able to take the code above and modify it according to the new white space requirements.
Frankly, I would have approached the problem much differently and tokenized the stream/buffer rather than using strings. Or use a standard serialized format.
Some more context could help. It looks like serialized data. You could perhaps consider using json and working on deserialized data rather than directly on the string format in which data are persisted. You won't need at all other records? It seems you are
telling a user would directly enter that??
Hi, I am trying to see if I could convert XML, HTML, etc to JSON or something like it and back to their original format. I know there are tools out there for this but I just wanted to see how such tools could work behind the scene. So when you said users
will be entering data in, you are right in so far as the syntax for HTML and XML are created by their authors. I would imagine the solution given above can give me a head start.
I don't quite understand what you are trying to do but would suggest that wanting to know the index of 'd' is the wrong way to go about it. It will be better to have a 'parser' of some form (as you note, these are available) which converts the input string
to 'data' and then other routines which can convert that 'data' to the output format that you want.
Each 'record' seems to be a set of name/value pairs. A good way to represent name/value pairs in .Net is a Dictionary - the name becomes the index.
A series of records would commonly be represented as some sort of enumerable data type.
A basic 'parser' could be written in a single statement if you want to
var data = input.Split(';') \\ Divide the records
.Select(i => i.Replace('[',' ')
.Replace(']',' ')
.Trim()) \\ Remove the noise characters
.Select(d => d.Split(',') \\ Divide each record into the fields
.Select(e => e.Trim() \\ Remove the noise
.Split('=')) \\ Splits the name from the value
.ToDictionary(e => e[0], e=>e[1])); \\ Make the dictionary
Now you have an Enumerable where each element represents one record. Each record is a Dictionary with index being the key and value being the data.
Now you have the data in a logical format you can convert it to whatever result you want to create.
As pointed already this kind of tool doesn't try to locate something but are based on parsers ie the string is analyzed character by character and keep tracks on what is currently analyzed (ie an identifier, a literal value etc).
You do have support for handling XML and JSON. HTML is not about data so I'm not sure how it relates.
If working with 3rd party it seems easier for everyone to stick to known formats such as CSV, XML or JSON rather than inventing again some other format.
Member
5 Points
23 Posts
Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 13, 2018 11:05 AM|gapi555|LINK
Suppose I have the following string:
Data in each square bracket represent a record and each record is separated from the next by a semicolon.
Using C#, what is the most efficient way to get the index of the letter "d" in the word duration for the Test2 record?
All-Star
53131 Points
23682 Posts
Re: Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 13, 2018 12:24 PM|mgebhard|LINK
The index as compared to the start of the string?
Member
5 Points
23 Posts
Re: Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 13, 2018 05:53 PM|gapi555|LINK
Hi, thanks for your reply. I see that your solution is dependent on the fact that there is a repeating pattern in each record and that is there is exactly one white space between each property in any given record, exactly one white space between each record, and each property appears in the same spot in each record.
What if the number of white space between each property and each record is random like when the string is too long and it must wrap to the next line or if the user put too many white spaces in by accident? Also what if each property was put in a random location within the record by the user instead of always being in the same spot in the record, how can I compensate for that?
All-Star
53131 Points
23682 Posts
Re: Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 13, 2018 06:22 PM|mgebhard|LINK
This was not part of the original requirement. You should be able to take the code above and modify it according to the new white space requirements.
Frankly, I would have approached the problem much differently and tokenized the stream/buffer rather than using strings. Or use a standard serialized format.
All-Star
48570 Points
18086 Posts
Re: Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 13, 2018 06:29 PM|PatriceSc|LINK
Hi,
Some more context could help. It looks like serialized data. You could perhaps consider using json and working on deserialized data rather than directly on the string format in which data are persisted. You won't need at all other records? It seems you are telling a user would directly enter that??
Member
5 Points
23 Posts
Re: Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 13, 2018 06:52 PM|gapi555|LINK
Hi, I am trying to see if I could convert XML, HTML, etc to JSON or something like it and back to their original format. I know there are tools out there for this but I just wanted to see how such tools could work behind the scene. So when you said users will be entering data in, you are right in so far as the syntax for HTML and XML are created by their authors. I would imagine the solution given above can give me a head start.
Participant
1630 Points
935 Posts
Re: Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 14, 2018 12:21 AM|PaulTheSmith|LINK
I don't quite understand what you are trying to do but would suggest that wanting to know the index of 'd' is the wrong way to go about it. It will be better to have a 'parser' of some form (as you note, these are available) which converts the input string to 'data' and then other routines which can convert that 'data' to the output format that you want.
Each 'record' seems to be a set of name/value pairs. A good way to represent name/value pairs in .Net is a Dictionary - the name becomes the index.
A series of records would commonly be represented as some sort of enumerable data type.
A basic 'parser' could be written in a single statement if you want to
Now you have an Enumerable where each element represents one record. Each record is a Dictionary with index being the key and value being the data.
Now you have the data in a logical format you can convert it to whatever result you want to create.
All-Star
48570 Points
18086 Posts
Re: Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 14, 2018 07:32 AM|PatriceSc|LINK
As pointed already this kind of tool doesn't try to locate something but are based on parsers ie the string is analyzed character by character and keep tracks on what is currently analyzed (ie an identifier, a literal value etc).
You do have support for handling XML and JSON. HTML is not about data so I'm not sure how it relates.
If working with 3rd party it seems easier for everyone to stick to known formats such as CSV, XML or JSON rather than inventing again some other format.
Member
5 Points
23 Posts
Re: Most Efficient Method to Get Index of a Character Only if it Follows a Particular string
May 15, 2018 11:28 AM|gapi555|LINK
Thank you all for your input. This is really helpful for understanding how parsers work behind the scene.