简体   繁体   English

Delphi 在字符串中查找下划线

[英]Delphi find underline in string

I have two strings named "ver1" and "ver2" the input in the strings looks like this:我有两个名为“ver1”和“ver2”的字符串,字符串中的输入如下所示:

Ver1: B2CD1 Ver2: B3A10 Ver1: B2CD1 Ver2: B3A10

Sometimes it looks like this:有时它看起来像这样:

Ver1: B2CD1_A1 Ver2: B3A10_DE Ver1: B2CD1_A1 Ver2: B3A10_DE

My question: How can I search in both strings after the "_" in it and return a value from it?我的问题:如何在“_”后面的两个字符串中进行搜索并从中返回一个值?

By the way the function is for software version comparison.顺便说一下,该功能用于软件版本比较。 Please just give me advice in my question not my entire function.请在我的问题中给我建议,而不是我的全部职能。

Thanks谢谢

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;
 

You can search a string using the Pos() function .您可以使用Pos() 函数搜索字符串。 It returns the index of the found character or 0 if not found.它返回找到的字符的索引,如果没有找到则返回 0。

var
    Index     : Integer;
    SubString : String;
begin
    Index := Pos('_', Ver1);
    if Index > 0 then
        SubString := Copy(Ver1, Index + 1, MAXINT)
    else
        SubString := '';
end;

If you want to extract text between two markers, you have to search for the first as shown above and then search for the second, starting right after the first one.如果要提取两个标记之间的文本,则必须按上图所示搜索第一个,然后从第一个之后开始搜索第二个。 Pos() has an optional 3rd argument to specify where to start search. Pos() 有一个可选的第三个参数来指定从哪里开始搜索。 This gives:这给出:

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;

You can use ContainsStr() to check if the strings has '_' and then use SubString() and IndexOf()您可以使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM