Jam*_*est 7 delphi string delphi-2009
我试图操纵一个字符串,只从中拉出某些数据.我需要在从数据库中提取的记录中执行此操作,该数据库为我提供了一个人的全名.我只需从字符串中提取姓氏并将其存储为变量.有没有办法可以做到这一点?
示例:SQL查询提取完整字段"Mary Ellen Jones"我需要仅从字符串中提取Jones,以便将其存储在变量中以供进一步处理.
我想也许AnsiRightStr可以工作,但问题是需要给它一个设置整数从右边拉.也许在最终空间之后计算字符的方法允许我为此使用AnsiRightStr(string,int)?任何帮助都表示赞赏.
额外的想法:用分隔符替换空格说::然后将该数据解析为Stringlist,然后允许我拉出字符串列表的最后一个索引?
到目前为止已经提出了几个有效的选项.如果说名字是"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;
Run Code Online (Sandbox Code Playgroud)
或使用功能
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;
Run Code Online (Sandbox Code Playgroud)
function GetLastWord(const Str: string): string;
var
p: integer;
i: Integer;
const
SPACE = #$20;
begin
p := 1;
for i := length(Str) downto 1 do
if Str[i] = SPACE then
begin
p := i + 1;
break;
end;
result := Copy(Str, p, MaxInt);
end;
Run Code Online (Sandbox Code Playgroud)
如果字符串以(意外的)空格结束,这将失败,如'Andreas Rejbrand'.这个更强大的版本也将处理这种情况:
function GetLastWord(const Str: string): string;
var
p: integer;
i: Integer;
FoundNonSpace: boolean;
const
SPACE = #$20;
begin
p := 1;
FoundNonSpace := false;
for i := length(Str) downto 1 do
if (Str[i] = SPACE) and FoundNonSpace then
begin
p := i + 1;
break
end
else if Str[i] <> SPACE then
FoundNonSpace := true;
result := TrimRight(Copy(Str, p, MaxInt));
end;
Run Code Online (Sandbox Code Playgroud)
如果姓氏是"圣詹姆斯"以任何方式解释该怎么办?
这是我的方法.
var LastNameMarkers: TStringList = nil; SuffixFix: TStringList = nil;
procedure InitLists;
begin
LastNameMarkers:= TStringList.Create;
//LastNameMarkers.LoadFromFile('c:\markers.txt');
LastNameMarkers.Add(' St.');
LastnameMarkers.Add(' Mc');
LastNameMarkers.Add(' '); //Marker of last resort.
SuffixFix:= TStringList.Create;
SuffixFix.Add(' Jr.');
SuffixFix.Add(' Sr.');
end;
function GetLastName(FullName: string): string;
var
i: integer;
start: integer;
found: boolean;
ReplaceWith: string;
begin
if LastNameMarkers = nil then InitLists;
//Fix suffixes
i:= 0;
found:= false;
while (i < SuffixFix.Count) and not found do begin
start:= pos(lower(LastNameMarkers[i]),lower(Fullname));
found:= Start > 0;
Inc(i);
end; {while}
if Found then begin
Dec(i);
ReplaceWith:= StringReplace(Suffix[i], ' ', '_',[]);
FullName:= StringReplace(FullName, SuffixFix[i], ReplaceWith,[]);
end; {if}
//Look for lastnames
i:= 0;
found:= false;
while (i < LastNameMarkers.Count) and not found do begin
start:= pos(LastNameMarkers[i],Fullname);
found:= Start > 0;
Inc(i);
end; {while}
if found then Result:= RightStr(FullName, Length(FullName)- Start + 2)
else Result:= '';
StringReplace(Result, '_', ' ',[]);
end;
Run Code Online (Sandbox Code Playgroud)
我没有正确处理大写和小写,但我希望你能得到这个想法.
归档时间: |
|
查看次数: |
2505 次 |
最近记录: |