2

I have this simple operation in Java, where the string is split by new line and break.

String i= "Holidays 
    
           Great. 
           Bye";
String []linesArray = i.split("\\r?\\n");

I would like to obtain the same result in Delphi 2006.

Is it valid to use the following steps?

charArray[0] := '\\r';
charArray[1] := '\\n';

strArray     := strA.Split(charArray);
LU RD
  • 34,438
  • 5
  • 88
  • 296
OutOfMemoryError
  • 391
  • 2
  • 4
  • 16
  • You should specify your Delphi version. Also, you might want to clarify the question. I interpret the Q like this: "Split a string at both CR and LF." which implies that CRLF gives an empty string element. For instance, 'alpha'#13'beta'#10'gamma'#13#10'delta' yields 'alpha', 'beta', 'gamma', '', and 'delta'. Recall that many Delphi experts don't know Java. – Andreas Rejbrand Apr 08 '21 at 09:02
  • @AndreasRejbrand: Delphi 10 – OutOfMemoryError Apr 08 '21 at 09:08
  • Thanks, then it is really easy. – Andreas Rejbrand Apr 08 '21 at 09:08
  • Delphi 10 was released in 2015. – Andreas Rejbrand Apr 08 '21 at 09:11
  • @AndreasRejbrand: by wiki In late 2005 Delphi 2006 (Delphi 10, also Borland Developer Studio 4.0) – OutOfMemoryError Apr 08 '21 at 09:17
  • I think most people think of Delphi 10 Seattle released in 2015 when you say "Delphi 10". The current version of Delphi is 10.4. – Andreas Rejbrand Apr 08 '21 at 09:19
  • @AndreasRejbrand your solution can work as well for a version of delphi 2006 – OutOfMemoryError Apr 08 '21 at 09:19
  • I don't think `TStringHelper` is present in Delphi 2006. – Andreas Rejbrand Apr 08 '21 at 09:20
  • @AndreasRejbrand: Can work your code for an old version of Delphi? – OutOfMemoryError Apr 08 '21 at 09:23
  • 1
    I'll add a version for old Delphi. – Andreas Rejbrand Apr 08 '21 at 09:24
  • @OutOfMemoryError Why are you using such an old Delphi version? Converting Java to Delphi will be much more easier using latest Delphi version. Delphi has really been enhanced during all those days. You can try the latest version (30 days trial) or use the Community Edition (Free of charge for personal use or very small business). Community Edition is not the latest version (10.3 vs 10.4.2). Delphi 10.4.2 is actually version number 27! – fpiette Apr 08 '21 at 09:36
  • The Java code does not split at delphi #13#10 - it splits at #10 and ignores #13 if it is at the end of the string. A RegEx will do exactly this for you, I don't know if that's in the version of Delphi you are using. – Rob Lambden Apr 08 '21 at 09:36

1 Answers1

5

I interpret your request like this: "Split a string at both CR and LF." which implies that CR+LF gives an empty string element. For instance, 'alpha'#13'beta'#10'gamma'#13#10'delta' yields the five elements 'alpha', 'beta', 'gamma', '', and 'delta'.

If so, and if you are using a non-ancient version of Delphi, this is really simple:

var S := 'alpha'#13'beta'#10'gamma'#13#10'delta';

var Parts := S.Split([#13, #10]);
for var Part in Parts do
  ShowMessage(Part);

For old Delphi versions

The code above requires TStringHelper (crucially) and also makes use of inline variable declarations, for in loops, and generics.

For old Delphi versions, you can do it manually:

type
  TStringArray = array of string;

function Split(const S: string): TStringArray;
var
  Count: Integer;
const
  Delta = 512;

  procedure Add(const Part: string);
  begin
    if Length(Result) = Count then
      SetLength(Result, Length(Result) + Delta);
    Result[Count] := Part;
    Inc(Count);
  end;

var
  p, i: Integer;

begin

  Result := nil;
  Count := 0;

  p := 0; // previous delim
  for i := 1 to Length(S) do
    if S[i] in [#13, #10] then
    begin
      Add(Copy(S, Succ(p), i - p - 1));
      p := i;
    end;

  Add(Copy(S, Succ(p)));

  SetLength(Result, Count);

end;

procedure TForm1.FormCreate(Sender: TObject);
var
  S: string;
  Parts: TStringArray;
  i: Integer;
begin

  S := 'alpha'#13'beta'#10'gamma'#13#10'delta';

  Parts := Split(S);
  for i := 0 to High(Parts) do
    ShowMessage(Parts[i]);

end;
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384