如何取字符串中的其中几位?
如:sum=main_Form.ADOQuery3.FieldByName("stock_id").AsString.SubString(7,6);
但是这样不行?请问是哪里错了?

解决方案 »

  1.   

    用copy函数
    copy('delphi is good!',1,6)
      

  2.   

    {=================================================================
      功  能: 功  能: 返回开始和截止字符之间的字符串,保存在TStringList中
      参  数: BeginStr  :开始字符
              EndStr    :截止字符
              SourceStr :原字符串
              RectList  :返回的字符串列表
      返回值: Integer   :开始字符在原字符串中出现的个数
      备 注:  查找字符串
      版 本:
         1.0  2006/09/14 09:55:00
    =================================================================}
    Function GetStr(BeginStr,EndStr:String;SourceStr:WideString;RectList:TStringList):integer;
    var ll_pos1,ll_pos2,ll_pos3,ll_pos4:Integer;
        ll_count,i,ll_count1:Integer;
        ls_tempstr,ls_str:String;
    begin
        ls_tempstr:= SourceStr;    ll_count  := StrNum(BeginStr,ls_tempstr);
        ll_pos1 := pos(BeginStr,ls_tempstr);
        ll_pos3 := 0;
        ll_pos4 := 0;
        for i:=1 to ll_count do
        begin
           ll_pos1  := Pos(BeginStr,ls_tempstr);
           ll_pos2  := Pos(EndStr  ,ls_tempstr);
           ls_str   := Copy(ls_tempstr,ll_pos1  + Length(BeginStr),ll_pos2 - ll_pos1 - Length(BeginStr));
           RectList.Add(ls_str);
           ls_tempstr := StringReplace(ls_tempstr,BeginStr,' #'+IntToStr(i)+'# ',[rfIgnoreCase]);
           ls_tempstr := StringReplace(ls_tempstr,EndStr  ,' @'+IntToStr(i)+'@ ',[rfIgnoreCase]);
        end;
        Result := ll_count;
    end;
      

  3.   

    copy(str,1,6)  取str 第一位后的6位(包括第一位)
      

  4.   

    copy没错,SubString是C++Builder中的函数