DELPHI STRING:从全名中提取姓氏
我正在尝试操作一个字符串并从中提取某些数据。我需要对从数据库中提取的记录执行此操作,该记录为我提供了一个人的全名。我只需要从字符串中提取姓氏并将其存储为变量。有什么办法可以做到这一点吗?
示例:SQL 查询提取完整字段“Mary Ellen Jones” 我需要从字符串中仅提取 Jones,以便可以将其存储在变量中以供进一步处理。
我想也许 AnsiRightStr 可以工作,但问题是需要给它一个设置的整数以从右侧拉取。也许有一种方法可以计算最后一个空格后的字符数,允许我使用 AnsiRightStr(string,int) 来实现此目的?任何帮助都值得赞赏。
附加想法:是否可以用分隔符 :: 替换空格,然后将该数据解析到字符串列表中,然后允许我提取字符串列表的最后一个索引?
到目前为止,已经提出了几种有效的选择。如果说名字是“John St. James, Jr.”之类的东西,他们都不会解决这种情况。这不可能吗?
I am trying to manipulate a string and pull only certain data from it. I need to do this on a record pulled from a database that gives me the full name of a person. I need to pull only the last name from the string and store it as a variable. Is there a way that I can do this?
Example: SQL query pulls the full field "Mary Ellen Jones" I need to extract only the Jones from the string so I can store it in a variable for further processing.
I thought maybe AnsiRightStr would work but the problem is needing to give it a set integer to pull from the right. Maybe a way to count the characters after the final space allowing me to use AnsiRightStr(string,int) for this? Any help at all is appreciated.
Additional thought: Would replacing the spaces with a delimiter say :: and then parsing that data into a Stringlist followed by allowing me to pull the last index of the string list be possible?
Several valid options have been presented so far. None of them address the situation if say the name is Something like "John St. James, Jr." Is this impossible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我的方法。
我没有正确处理大小写,但我希望你明白。
Here's my approach.
I haven't dealt with upper and lowercase properly, but I hope you get the idea.
这两个功能一起完成了我需要做的事情。还有 initlists 函数,它又笨重又丑陋,我需要改进,所以我没有将其发布在这里。
These two functions together pull off what I needed to do. There is also the initlists function which is clunky and ugly and I need to work on so I didn't post it here.
您可以使用
LastDelimiter
函数获取最后一个空格位置,然后使用copy
函数提取子串。或使用函数
you can use the
LastDelimiter
function to get the last space position and then with thecopy
function extract the substring.or using a function
如果字符串以(意外的)空格结尾,如“Andreas Rejbrand”,则此操作将会失败。这个更强大的版本也可以处理这种情况:
This will fail if the string ends with (an accidental) space, as 'Andreas Rejbrand '. This more robust version will handle this case too: