I have a string with the following structure:
Student Name________AgeAddress_______________________Bithday___Lvl
Example:
Jonh Smith 016Some place in NY, USA 01/01/2014L01
As you can see, there is no delimited character like |
or ,
Also, there is no space between fields (if you check, there is no space between Age/Address and Birthday/Level.
The size of each field is static so if data's length is less then it will contains white spaces.
I have a class that need to be filled with that information:
public class StudentData
{
public char[] _name = new char[20];
public string name;
public char[] _age = new char[3];
public string age;
public char[] _address = new char[30];
public string address;
public char[] _bday = new char[10];
public string bday;
public char[] _level = new char[3];
public string level;
}
Is there any way to do this automatically and dynamically?
I mean I really don't want to code like this:
myClass.name = stringLine.substring(0,19);
myClass.age = stringLine.substring(20,22);
That's because I have way more fields that the ones added in this example & way more string lines with other different data.
Update: There were supposed to be a lot of spaces between "Smith" and "016", but I don't know how to edit it.
Update2: If I use StringReader.Read() I can evade to use substring and indexes, but it isn't still so dynamically because I would need to repeat those 3 lines for each field.
StringReader reader = new StringReader(stringLine);
reader.Read(myClass._name, 0 myClass._name.Length);
myClass.name = new string(myClass._name);
Given your requirement I came up with an interesting solution. All be-it it may be more complex and longer than using the
String.SubString()
method as stated.However this solution is transferable to other types and other string. I used a concept of
Attributes
,Properties
, andReflection
to parse a string by a Fixed Length and setting the class Properties.Note I did change your
StudentData
class to follow a more conventional coding style. Following this handy guide on MSDN: http://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71).aspxHere is the new
StudentData
class. Note it uses the properties as opposed to fields. (Not discussed here).Note on each of the properties there is an Attribute called
FixedLengthDelimeter
that takes two parameters.OrderNumber
FixedLength
The
OrderNumber
parameter denotes the order in the string (not the position) but the order in which we process from the string. The second parameter denotes theLength
of the string when parsing the string. Here is the full attribute class.Now the attribute is simple enough. Accepts the two paramters we discussed eariler in the constructor.
Finally there is another method to parse the string into the object type such as.
The method above is what does the string parsing. It is a pretty basic utility that reads all the properties that have the
FixedLengthDelimeter
attribute applied aDictionary
. That dictionary is then enumerated (ordered byOrderNumber
) and then calling theSubString()
method twice on the input string.The first substring is to parse the next
Token
while the second substring resets theinputString
to start processing the next token.Finally as it is parsing the string it is then applying the parsed string to the property of the class
Type
provided to the method.Now this can be used simply like this:
What this does:
What this does not do: