问题描述
我需要将包含First Last"格式名称的字符串更改为Last, First"格式.问题是,字符串可能像John Smith",也可能是Mr. John Smith"甚至Mr. John A. Smith".我怎样才能消除先生"?部分和中间名/首字母部分,然后使其成为史密斯,约翰"?顺便说一句,我讨厌字符串......我预见到很多修剪(右),修剪(左)废话......请让它更容易!
I need to change a string that contains a name in the "First Last" format to a "Last, First" format. The problem is, the string may be like "John Smith", or it may be "Mr. John Smith" or even "Mr. John A. Smith". How can I eliminate the "Mr." part and the middle name/initial part, and then make it "Smith, John"? I hate strings btw...I foresee a lot of Trim(Right), Trim(Left) crap... please make it easier!
我的目标是通过姓氏的第一个字母从数据库字段 NAME 中选择记录.字段 NAME 包含诸如John Smith"、Roger A. Smith 先生"、Jones 先生"等名称.我只需要一个返回 Smith 而不是 Jones 的搜索函数.
My goal is to select records from the database field NAME by the first letter of the last name. The field NAME contains names like "John Smith" "Mr. Roger A. Smith" "Mr. Jones" etc. I just need a search function that returns the Smith's but not the Jones.
推荐答案
这是否满足您的要求?
Dim samples As String() = {
"John Smith",
"John A. Smith",
"Mr. John Smith",
"Mr. John A. Smith",
"Jean-Claude Van Damme"
}
Dim regex As New Regex("(?:(?:mr\.|miss|mrs|ms)\s+)?(\S+).*(?<=\s)(\S+)$", RegexOptions.IgnoreCase)
For Each sample In samples
Dim converted As String = regex.Replace(sample, "$2, $1")
Console.WriteLine("{0} => {1}", sample, converted)
Next
结果:
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
这篇关于字符串问题.格式“First Last"格式化“最后一个,第一个"有一点扭曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!