vb.net - String issues. Format "First Last" to format "Last, First" with a twist -


i need change string contains name in "first last" format "last, first" format. problem is, string may "john smith", or may "mr. john smith" or "mr. john a. smith". how can eliminate "mr." part , middle name/initial part, , make "smith, john"? hate strings btw...i foresee lot of trim(right), trim(left) crap... please make easier!

my goal select records database field name first letter of last name. field name contains names "john smith" "mr. roger a. smith" "mr. jones" etc. need search function returns smith's not jones.

does fulfill requirements?

dim samples string() = {     "john smith",     "john a. smith",     "mr. john smith",     "mr. john a. smith",     "jean-claude van damme" }  dim regex new regex("(?:(?:mr\.|miss|mrs|ms)\s+)?(\s+).*(?<=\s)(\s+)$", regexoptions.ignorecase)  each sample in samples     dim converted string = regex.replace(sample, "$2, $1")     console.writeline("{0} => {1}", sample, converted) next 

results:

john smith => smith, john john a. smith => smith, john mr. john smith => smith, john mr. john a. smith => smith, john jean-claude van damme => damme, jean-claude 

.net fiddle


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -