Delphi 查找字符串中的下划线
Delphi find underline in string
我有两个名为“ver1”和“ver2”的字符串,字符串中的输入如下所示:
Ver1: B2CD1
Ver2: B3A10
有时看起来像这样:
Ver1: B2CD1_A1
Ver2: B3A10_DE
我的问题:如何在其中的“_”后的两个字符串中搜索它的 return 值?
顺便说一句,该功能用于软件版本比较。请只在我的问题中给我建议,而不是我的全部功能。
谢谢
uses
Classes, SysUtils, StrUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Memo1Change(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
function isSoftwareGreater(ver1,ver2 : string) : byte;
var i : integer;
tmp1,tmp2 : string;
cver1,cver2 : int64;
begin
{
Return values:
0: versions are equal
1: ver1 is bigger then ver2
2: ver2 is bigger then ver1
253: ver1 is too big (could not be converted to int64)
254: ver2 is too big (could not be converted to int64)
}
tmp1 := '';
tmp2 := '';
for i := 1 to length(ver1) do
begin
if (ver1[i] in ['a'..'z']) or (ver1[i] in ['A'..'Z']) then
tmp1 := tmp1 + inttostr(Ord(ver1[i]));
if (ver1[i] in ['0'..'9']) then
tmp1 := tmp1 + ver1[i];
end;
for i := 1 to length(ver2) do
begin
if (ver2[i] in ['a'..'z']) or (ver2[i] in ['A'..'Z']) then
tmp2 := tmp2 + inttostr(Ord(ver2[i]));
if (ver2[i] in ['0'..'9']) then
tmp2 := tmp2 + ver2[i];
end;
if not TryStrToInt64(tmp2,cver2) then
begin
isSoftwareGreater := 254;
exit;
end;
if not TryStrToInt64(tmp1,cver1) then
begin
isSoftwareGreater := 253;
exit;
end;
if ver1 > ver2 then
isSoftwareGreater := 1
else if ver2 > ver1 then
isSoftwareGreater := 2
else if ver1 = ver2 then
isSoftwareGreater := 0;
end;
您可以使用 Pos() function 搜索字符串。它 returns 找到的字符的索引,如果没有找到则为 0。
var
Index : Integer;
SubString : String;
begin
Index := Pos('_', Ver1);
if Index > 0 then
SubString := Copy(Ver1, Index + 1, MAXINT)
else
SubString := '';
end;
如果您想提取两个标记之间的文本,您必须如上所示搜索第一个,然后搜索第二个,从第一个标记之后开始。 Pos() 有一个可选的第三个参数来指定从哪里开始搜索。这给出:
var
Index1 : Integer;
Index2 : Integer;
SubString : String;
begin
Index1 := Pos('_', Ver1);
if Index1 > 0 then begin
Index2 := Pos('_', Ver1, Index1 + 1);
if Index2 > 0 then
// We have two underscore, extract text between them
SubString := Copy(Ver1, Index1 + 1, Index2 - Index1 - 1)
else
// Only one underscore, extract text from the first to the end of string
SubString := Copy(Ver1, Index1 + 1, MAXINT);
end
else
SubString := '';
end;
您可以使用ContainsStr()
检查字符串是否有'_'
,然后使用SubString()
和IndexOf()
function GetString(const AString, ASubString: string): string;
begin
Result:= EmptyStr;
if ContainsStr(AString, ASubString) then
Result:= AString.Substring(Astring.IndexOf(ASubString)+1, AString.Length);
end;
我有两个名为“ver1”和“ver2”的字符串,字符串中的输入如下所示:
Ver1: B2CD1
Ver2: B3A10
有时看起来像这样:
Ver1: B2CD1_A1
Ver2: B3A10_DE
我的问题:如何在其中的“_”后的两个字符串中搜索它的 return 值?
顺便说一句,该功能用于软件版本比较。请只在我的问题中给我建议,而不是我的全部功能。
谢谢
uses
Classes, SysUtils, StrUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Memo1Change(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
function isSoftwareGreater(ver1,ver2 : string) : byte;
var i : integer;
tmp1,tmp2 : string;
cver1,cver2 : int64;
begin
{
Return values:
0: versions are equal
1: ver1 is bigger then ver2
2: ver2 is bigger then ver1
253: ver1 is too big (could not be converted to int64)
254: ver2 is too big (could not be converted to int64)
}
tmp1 := '';
tmp2 := '';
for i := 1 to length(ver1) do
begin
if (ver1[i] in ['a'..'z']) or (ver1[i] in ['A'..'Z']) then
tmp1 := tmp1 + inttostr(Ord(ver1[i]));
if (ver1[i] in ['0'..'9']) then
tmp1 := tmp1 + ver1[i];
end;
for i := 1 to length(ver2) do
begin
if (ver2[i] in ['a'..'z']) or (ver2[i] in ['A'..'Z']) then
tmp2 := tmp2 + inttostr(Ord(ver2[i]));
if (ver2[i] in ['0'..'9']) then
tmp2 := tmp2 + ver2[i];
end;
if not TryStrToInt64(tmp2,cver2) then
begin
isSoftwareGreater := 254;
exit;
end;
if not TryStrToInt64(tmp1,cver1) then
begin
isSoftwareGreater := 253;
exit;
end;
if ver1 > ver2 then
isSoftwareGreater := 1
else if ver2 > ver1 then
isSoftwareGreater := 2
else if ver1 = ver2 then
isSoftwareGreater := 0;
end;
您可以使用 Pos() function 搜索字符串。它 returns 找到的字符的索引,如果没有找到则为 0。
var
Index : Integer;
SubString : String;
begin
Index := Pos('_', Ver1);
if Index > 0 then
SubString := Copy(Ver1, Index + 1, MAXINT)
else
SubString := '';
end;
如果您想提取两个标记之间的文本,您必须如上所示搜索第一个,然后搜索第二个,从第一个标记之后开始。 Pos() 有一个可选的第三个参数来指定从哪里开始搜索。这给出:
var
Index1 : Integer;
Index2 : Integer;
SubString : String;
begin
Index1 := Pos('_', Ver1);
if Index1 > 0 then begin
Index2 := Pos('_', Ver1, Index1 + 1);
if Index2 > 0 then
// We have two underscore, extract text between them
SubString := Copy(Ver1, Index1 + 1, Index2 - Index1 - 1)
else
// Only one underscore, extract text from the first to the end of string
SubString := Copy(Ver1, Index1 + 1, MAXINT);
end
else
SubString := '';
end;
您可以使用ContainsStr()
检查字符串是否有'_'
,然后使用SubString()
和IndexOf()
function GetString(const AString, ASubString: string): string;
begin
Result:= EmptyStr;
if ContainsStr(AString, ASubString) then
Result:= AString.Substring(Astring.IndexOf(ASubString)+1, AString.Length);
end;