Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Is there a way/lib/solution to mapp a string into a dictionary whatever in order to have a direct access to the parts of the string I mean:

string a = "NAME#####SURNAME####TEL#######ADDRESS######SSN#########WHATEVER#####";

var x = map(a);
print x.Name;
print x.Surname;
print x.Address;
...
Posted
Comments
Sergey Alexandrovich Kryukov 4-Oct-12 15:12pm    
Why doing such an obscure things? What did you try?
--SA

1 solution

I would create a class to encapsulate it - you could call it "Map" if you wanted.
Actually breaking it into it's constituent parts is simple:
C#
string a = "NAME#####SURNAME####TEL#######ADDRESS######SSN#########WHATEVER#####";
string[] parts = a.Split(new char[] {'#'}, StringSplitOptions.RemoveEmptyEntries);

You can then define constants to access each part by it's index into the parts array.
 
Share this answer
 
Comments
ozwaldo 4-Oct-12 15:53pm    
Hmm, not sure as solution because I didn´t mentioned that the # is just a filler and may or may not appear.
This string usually commes from a mainframe response and is known as fixed length text fields so there is no delimiters.
OriginalGriff 5-Oct-12 3:28am    
If it's fixed fields, it's even easier - just use string.Substring to extract the parts, then string.Trim to remove trailing junk (such as the '#' characters if they exist). It has an overload which allows you to specify the characters to remove: http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx
ozwaldo 4-Oct-12 15:56pm    
I've found a good solution here http://layoutstr.codeplex.com/ and seems to attend my needs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900