Add more than 1 delimiter in TStringList
Asked Answered
A

4

5

This is my delimited text: $HEHDT,10.17,T*28$HEHDT,10.18,T*2A and so on...

The comma is my sentence delimiter. However, I want to use the asterisk as my delimiter as well.

Output I want to achieve is:

$HEHDT 10.17 T 28 $HEHDT 10.18 T 2A

How do I specify more than 1 sentence delimiter in delphi? This is the code I have so far.

var
  MyStringList: TStringList;
  i: Integer;
begin
  MyStringList:= TStringList.Create;

  MyStringList.Delimiter := ','
  MyStringList.DelimitedText := '$HEHDT,10.17,T*28'+#13#10 +'$HEHDT,10.18,T*2A' +#13#10;

  for i= 0 to MyStringList.Count-1 do
    ShowMessage(MyStringList[i]);

  MyStringList.Free;
end;

For the above code, it only takes the comma as delimiter. How do I include 2 delimiters, both the comma and the asterisk?

Many thanks in advance! =)

Abnormality answered 27/2, 2012 at 2:31 Comment(5)
What's your Delphi version, please ?Folkway
You'd better off use ExtractStrings for more capable string splittingTichonn
@user539484: Provided he has the appropriate Delphi version.Folkway
@menjaraz, why? ExtractStrings dates back to Ramesses age.Tichonn
@user539484: You are right. I was mislead, I thought of SplitString which was introduced in StrUtils.Folkway
N
8

Delphi stringlist is nice enough to give you the ability to parse text on one delimiter "for free". If you want a set of delimiters - then you need to use StrUtils.SplitString:

http://docwiki.embarcadero.com/VCL/en/StrUtils.SplitString

Nicks answered 27/2, 2012 at 2:36 Comment(0)
H
0

You could use ReplaceString, changing all asterisks to commas. Change your assignment line to this:

 String.DelimitedText := ReplaceString('$HEHDT,10.17,T*28'+#13#10 +'$HEHDT,10.18,T*2A', '*',
                         ',', [rfReplaceAll]);

Your code above should then work.

Note: the DelimitedText property, I believe, considers spaces as delimiters too. So if you string has spaces, your code might fail to behave the way you might expect...

Hemichordate answered 27/2, 2012 at 2:53 Comment(1)
If you set StrictDelimeter to True, it won't give you any grief with spaces.Tint
F
0
  1. The short answer is NO.
  2. But you can use a custom TStringList.

TStringList accepts one and only one Delimiter.

Excerpt from Embarcadero RAD Studio Help:

Classes.TStrings.DelimitedText

Description

Represents all the strings in the TStrings object as a single delimited string.

Use DelimitedText to get or set all the strings in the TStrings object in a single string, separated by the character specified by the Delimiter property.


To be able to use more than one Delimiters you should "extend" the capabilities of TStringList class by deriving a new class from it.

You can find here a sample source code of a TStringListEx class having a similar behavior that you can easily adapt to your requirement.

Excerpt

TStringListEx = class(TStringList)
private
  FDelimiter: String;
  function GetDelimitedText: String;
  procedure SetDelimitedText(const Value: String);
published
  property Delimiter: String read FDelimiter write FDelimiter;
  property DelimitedText: String read GetDelimitedText write SetDelimitedText;
end;

The workaround consisting of preprocessing the string to set to TStringList.DelimitedText property with the rest of delimiters (the first one being set to TStringList.Delimeter property) can achieve your goal and apart from solutions pointed in the posts of paulsm4 and Robert Frank you can also use Regular Expression.

Folkway answered 27/2, 2012 at 4:35 Comment(0)
A
0

You could use two string lists, one using comma, the other using asterisk as delimiter. Then assign the text property of the first to the delimited text property of the second.

Aguiar answered 27/2, 2012 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.