mcerk Posted January 31, 2005 Posted January 31, 2005 So, I have one string: NameSurname = "John Doe" how can I separate it to: Name = "John" Surname = "Doe" tx a lot matej Quote
Leaders dynamic_sysop Posted January 31, 2005 Leaders Posted January 31, 2005 use the Split function of the String Class , eg: Dim NameSurname As String = "John Doe" '/// use Split , the Space being the divider Dim FirstName As String = NameSurname.Split(" ")(0) Dim LastName As String = NameSurname.Split(" ")(1) MessageBox.Show("the first name is " & FirstName & Environment.Newline & "the last name is " & LastName) Quote
mhildner Posted January 31, 2005 Posted January 31, 2005 While dynamic_sysop's reply is correct, you may run into other troubles parsing free text names. For example, if the person's name is Oscar De LaHoya, or W. C. Fields - you get the idea. I've been parsing free text names and addresses, or trying to, for years, and can say with confidence there's no fool-proof algorithm. Not sure how fancy you need to get, but our current app gives its best attempt to parse, then displays the results to the user to see if it's ok and give a chance to correct. Quote
kejpa Posted February 1, 2005 Posted February 1, 2005 While dynamic_sysop's reply is correct, you may run into other troubles parsing free text names. For example, if the person's name is Oscar De LaHoya, or W. C. Fields - you get the idea. I've been parsing free text names and addresses, or trying to, for years, and can say with confidence there's no fool-proof algorithm. True! I tried it a couple of times too but then I told my manager that this wasn't polite and ever since I always use two fields for this. (Or three if the middle initials are important) /Kejpa Quote
twistedm1nd Posted February 1, 2005 Posted February 1, 2005 // C# code string name = "My really really long name"; int init; while((init=name.IndexOf(" ",0))!=-1) { MessageBox.Show(name.Substring(0,init)); name = name.Remove(0,init); name=name.Trim(); } MessageBox.Show(name); //should handle generic conditions where the names seperated by one or more spaces i guess //its not an optimal solution though //Insted of a messagebox an arraylist may be used to hold the names Quote
mhildner Posted February 1, 2005 Posted February 1, 2005 While it's easy enought to break up a string given a certain delimiter, my point (which may not even be applicable) is that it is difficult, if not impossible, to parse names correctly. For example, the second token within a space-delimited string is not always the last name. I agree with kejpa that one should seperate out the name parts, unless of course, you have no need to know the individual parts :p Lately I've been using the Department of Justice's Global Justice XML Data Model for name parts. This goes much further that just first name, last name, and includes name prefix, suffix etc. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.