zoukankan      html  css  js  c++  java
  • 根据条件把一个字符串拆分成几个字符串

    用到的函数:

    function pos(Substr:string; S: string):string;  //函数返回子字符串第一次出现在制定字符串中的索引值。

    copy()   // 复制字符串

    delete() //删除字符串中的一部分

    思路:利用Pos函数定一个范围,然后再COPY,最后删了这个已经复制的部分,再进行下一个索引,如此循环。

    例:

    分下列字符串,DELPHI的:

    ABCD           VAR           1234

    这是要拆分的一行字符串, 字符串之间有N个空格这样子.

    const
    
    S = 'ABCD VAR 1234';
    var
    p: integer;
    str,rs: string;
    begin
    rs = S;
    p := Pos(' ', rs);
    while(p <> 0)
    begin
    str := Copy(S,p,Length(rs));
    Delete(S,1,p);
    ListBox1.Items.Add(str);
    p := Pos(' ', rs);
    end;
    end;

    例2

    在要求用户输入GUID后,{E22ACEBC-DBFB-4F8C-BA76-D24151ECFD52},
    我想将它存入TGUID中,但StringToGUID却不能用,那我就想将它转换成几个字符串,“E22ACEBC","DBFB","BA76",

    uses StrUtils;

    procedure StrToStrings(S: AnsiString; Sep: AnsiString; const List: TStrings);
    var
      I, L: Integer;
      Left: AnsiString;
    begin
      Assert(List <> nil);
      List.Clear;
      L := Length(Sep);
      I := Pos(Sep, S);
      while (I > 0) do
      begin
        Left := LeftStr(S, I - 1);
        List.Add(Left);
        Delete(S, 1, I + L - 1);
        I := Pos(Sep, S);
      end;
      if S <> '' then
        List.Add(S);
    end;

  • 相关阅读:
    js原型对象与Java类的比较
    javascript特效--制作背景电子钟(整点时祝贺生日快乐)
    Java web MVC开发模式入门感悟
    重新审视面向对象编程
    10-排序4 统计工龄(20 分)
    基数排序
    表排序
    快速排序
    09-排序3 Insertion or Heap Sort(25 分)
    09-排序2 Insert or Merge(25 分)
  • 原文地址:https://www.cnblogs.com/linguoqiu/p/2815477.html
Copyright © 2011-2022 走看看