DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: forget66
今日帖子: 62
在线用户: 11
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2020/3/20 7:03:00
标题:
How to works with Class Helper to Strings to divide it in "before" and "after" Delimiter char - Easy 浏览:834
加入我的收藏
楼主: Here my sample to works with Class Helper to Strings to divide it in "before" and "after" Delimiter char!

Very easy!

----------

unit uMainForm;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btnUsinSPLIT_function: TButton;
    ListBox1: TListBox;
    btnUsingListBox: TButton;
    ListBox2: TListBox;
    Memo1: TMemo;
    btnMyNewSplitStrings: TButton;
    procedure btnUsinSPLIT_functionClick(Sender: TObject);
    procedure btnUsingListBoxClick(Sender: TObject);
    procedure btnMyNewSplitStringsClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnUsinSPLIT_functionClick(Sender: TObject);
var
  lMyArraySpplited: TArray<string>;
  lMyRegKey       : string;
  lNewRegKey      : string;
  i          : Integer;
  lLastDelimiter  : integer;
begin
  memo1.Lines.Clear;
  //
  // if lMyRegKey = ''  or '\'  -> it's necessary verify too!
  // But DONT problem if lLastDelimiter <= 0
  // on the end, your "string" resulted can be EMPTY!
  // None exception will be raised!
  //
  lMyRegKey := 'part1\part2\part3\value';
  //
  lMyArraySpplited := lMyRegKey.Split(['\']);
  //
  lLastDelimiter := lMyRegKey.LastDelimiter('\') + 1; // +1 here or below (Rigth substring)
  //
  memo1.Lines.Add('SubString Left  = ' + lMyRegKey.Substring(-lLastDelimiter, lLastDelimiter));
  memo1.Lines.Add('SubString Right = ' + lMyRegKey.Substring(lLastDelimiter));
  //
  memo1.Lines.Add('LastDelimiter = ' + lMyRegKey.LastDelimiter('\').ToString);
  //
  memo1.Lines.Add('');
  //
  lNewRegKey := lMyRegKey.Join('\', lMyArraySpplited, 0, 2); // "part1\part2"
  //
  memo1.Lines.Add('Join 0 to 2 = ' + lNewRegKey);
  //
  lNewRegKey := lMyRegKey.Join('\', lMyArraySpplited, 3, 4); // "value"
  //
  memo1.Lines.Add('Join 3 to 4 = ' + lNewRegKey);
  //
  memo1.Lines.Add('');
  //
  for i := 0 to high(lMyArraySpplited) do
  begin
    if lMyArraySpplited[i] <> '' then
      memo1.Lines.Add(lMyArraySpplited[i]);
  end;
end;

procedure TForm1.btnMyNewSplitStringsClick(Sender: TObject);
var
  lMyRegKey       : string;
  lMyArraySpplited: TArray<string>;
begin
  Memo1.Lines.Clear;
  //
  lMyRegKey := 'part1\part2\part3\value';
  //
  // start from "1" and ending on "3" chars!
  // using "\" like my "Delimiter" to split my string!
  //
  lMyArraySpplited := lMyRegKey.Split(['\'], '1', '3', Length(lMyRegKey));
  //
  Memo1.Lines.Add('How many items = ' + Length(lMyArraySpplited).ToString);
  Memo1.Lines.Add('');
  Memo1.Lines.Add('my items:');
  //
a  Memo1.Lines.AddStrings(lMyArraySpplited); // working with my items splitted (my array)
end;

procedure TForm1.btnUsingListBoxClick(Sender: TObject);
var
  lMyRegKey: string;
begin
  lMyRegKey := 'part1\part2\part3\value';
  //
  ListBox1.Items.Delimiter     := '\';
  ListBox1.Items.DelimitedText := lMyRegKey;
  //
  ListBox2.Items.Clear;
  ListBox2.Items.Add(ListBox1.Items[3]);
  //
  // another ways can help too, when using a "List"
  //
  // ListBox1.Items.IndexOf('first position to start')
  // ListBox1.Items.IndexOf('last position to end')
  // ShowMessage(ListBox1.Items.KeyNames[2]);
end;

end.
此帖子包含附件:
PNG 图像
大小:102.7K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2020/3/20 7:06:05
1楼: screen02
此帖子包含附件:
PNG 图像
大小:97.3K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 kaida (kaida) ★☆☆☆☆ -
盒子活跃会员
2020/3/20 10:47:27
2楼: 谢谢 emailx45 (emailx45)!
----------------------------------------------
http://down.desei.com.cn/down/1041485/MyWeb/VCLs.html
作者:
男 he_19_79 (he) ▲▲▲▲▲ -
普通会员
2020/3/20 20:37:13
3楼: 我这里有更全面的System.SysUtils.TStringHelper 的说明:

其中跟楼主相似的有:

分割:
function Split(const Separator: array of Char): TArray<string>;
function Split(const Separator: array of Char; Count: Integer): TArray<string>;
function Split(const Separator: array of Char; Options: TStringSplitOptions): TArray<string>;
function Split(const Separator: array of string; Options: TStringSplitOptions): TArray<string>;
function Split(const Separator: array of Char; Count: Integer; Options: TStringSplitOptions): TArray<string>;
function Split(const Separator: array of string; Count: Integer; Options: TStringSplitOptions): TArray<string>;
//----------
var
  str: string;
  arr: TArray<string>;
begin
  str := 'A-1,B-2,,,C-3,D-4';

  arr := str.Split([',']);          // arr[0] = A-1; Length(arr) = 6
  arr := str.Split([','], TStringSplitOptions.ExcludeEmpty); // 忽略空项; Length(arr) = 4
  arr := str.Split([','], 2);          // 只提取前 2

  arr := str.Split([',', '-'], ExcludeEmpty); //arr[0] = A; Length(arr) = 8

  arr := str.Split([',,,'], None);          // 分隔符可以是一个字符串数组
end;

连接:
class function Join(const Separator: string; const values: array of const): string;
class function Join(const Separator: string; const Values: array of string): string;
class function Join(const Separator: string; const Values: IEnumerator<string>): string;
class function Join(const Separator: string; const Values: IEnumerable<string>): string;
class function Join(const Separator: string; const value: array of string; StartIndex: Integer; Count: Integer): string;
//----------
var
  S: string;
  str: string;
  strArr: TArray<string>;
begin
  str := 'A1,B2,C3,,,,D4,E5,F6,G7';
  strArr := str.Split([','], ExcludeEmpty);

  str := S.Join('-', strArr);          // A1-B2-C3-D4-E5-F6-G7

  str := S.Join('; ', [1,2,3,4,5]);       // 1; 2; 3; 4; 5

  str := S.Join(',', ['abc', 123, true]); // abc,123,True
end;
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2020/3/20 21:35:03
4楼: good discution!

thanks for all
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行757.8125毫秒 RSS