gpt4 book ai didi

德尔菲字符串: Pull a last name from a full name

转载 作者:行者123 更新时间:2023-12-03 14:50:18 24 4
gpt4 key购买 nike

我正在尝试操作一个字符串并仅从中提取某些数据。我需要对从数据库中提取的记录执行此操作,该记录为我提供了一个人的全名。我只需要从字符串中提取姓氏并将其存储为变量。有什么办法可以做到这一点吗?

示例:SQL 查询提取完整字段“Mary Ellen Jones”,我只需从字符串中提取 Jones,以便可以将其存储在变量中以供进一步处理。

我认为 AnsiRightStr 可能会起作用,但问题是需要给它一个设置的整数以从右侧拉取。也许有一种方法可以计算最后一个空格后的字符数,允许我使用 AnsiRightStr(string,int) 来实现此目的?如有任何帮助,我们将不胜感激。

其他想法:是否可以用分隔符::替换空格,然后将该数据解析到字符串列表中,然后允许我提取字符串列表的最后一个索引?

到目前为止已经提出了几个有效的选项。如果说名字是“John St. James, Jr.”之类的东西,他们都不会解决这种情况。这不可能吗?

最佳答案

您可以使用LastDelimiter函数获取最后一个空格位置,然后使用 copy函数提取子字符串。

uses
SysUtils;


var
Name : string;
p : Integer;
ShortName : string;
begin
Name:='Mary Ellen Jones';
//You can call trim to avoid problems with ending spaces in this case is not necesary, just is a test
//Name:=Trim(Name);
//get the last space position
p:=LastDelimiter(' ',Name);
//get the name
ShortName:=Copy(Name,p+1,length(Name)-p);
end;

或使用函数

function GetLast(const Name:string) : string;
var
p : Integer;
begin
Result:=Trim(Name);
p:=LastDelimiter(' ',Result);
Result:=Copy(Result,p+1,length(Result)-p);
end;

关于德尔菲字符串: Pull a last name from a full name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6155849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com