字符串问题。格式化"First Last"以扭曲格式化"Last, First"



我需要将包含"First,Last"格式名称的字符串更改为"Last,First"格式。问题是,这个字符串可能像"John Smith",也可能是"John Smith先生",甚至是"John A.Smith先生"。如何去掉"先生"部分和中间名/首字母部分,然后改为"Smith,John"?我讨厌字符串顺便说一句…我预见到很多修剪(右),修剪(左)的垃圾。。。请让它更容易!

我的目标是从数据库字段NAME中按姓氏的第一个字母选择记录。字段NAME包含诸如"John Smith"、"Roger A.Smith"one_answers"Mr.Jones"等名称。我只需要一个返回Smith的搜索函数,但不返回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

.Net Fiddle

相关内容

  • 没有找到相关文章

最新更新