WideString 子串处理

  1. 取子串位置
  2. 取指定位置的子串
  3. 拆分子串成数组

取子串位置

Pos(substr, srcstr):Integer
function WideStrPos(const substr,src:WideString;const start:Integer=1):Integer;
var
  s : WideString;
  p : Integer;
begin
  if start>1 then
  begin
    s := WideMidStr(src, start);
    p := Pos(substr, s);
    if p>0 then
      p := p + start-1;
  end
  else
    p := Pos(substr, src);

  Result := p;
end;

取指定位置的子串

LeftStr
MidStr
RightStr
function WideMidStr(const src:WideString; const start:Integer; len:Integer=-1):WideString;
var
  l : Integer;
begin
  if len=-1 then
    l := Length(src)-start+1
  else
    l := len;
  result := MidStr(src, start, l);
end;

拆分子串成数组

function WideStrSplit(const src, mid:WideString;const max:Integer=-1):TTntStrings;
var
  s, tmp : WideString;
  num : Integer;
  p : Integer;
begin
  Result := TTntStringList.Create;

  s := src;
  num := 0;
  while ((num<max-1) or (max=-1)) and (Length(s)>0) do
  begin
    p := Pos(mid, s);
    if p = 0 then
      break;
    tmp := LeftStr(s, p-1);
    Result.Add(tmp);
    num := num + 1;

    s := WideMidStr(s, p + Length(mid) );
  end;
  Result.Add(s);
end;