Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,393 questions

52,502 answers

573 users

How to split a string on multiple single‑character delimiters (and keep them) in Pascal

2 Answers

0 votes
program MultiSplit;

type
  { Turbo Pascal strings are limited to 255 chars }
  CharSet = set of char;

procedure SplitAndKeepDelimiter(S: string; Delims: CharSet);
var
  i, start: integer;
  sub: string;
begin
  start := 1;
  for i := 1 to Length(S) do
  begin
    if S[i] in Delims then
    begin
      { 1. Extract text before delimiter }
      if i > start then
      begin
        sub := Copy(S, start, i - start);
        WriteLn('Token: ', sub);
      end;
      
      { 2. Extract the delimiter itself (the "Keep" part) }
      WriteLn('Delim: ', S[i]);
      
      start := i + 1;
    end;
  end;
  
  { 3. Extract final remaining part }
  if start <= Length(S) then
    WriteLn('Token: ', Copy(S, start, 255));
end;

var
  testStr: string;
  myDelims: CharSet;
begin
  testStr := 'aa,bbb;cccc.ddddd';
  myDelims := [',', ';', '.']; { Define multiple delimiters here }
  
  SplitAndKeepDelimiter(testStr, myDelims);
  ReadLn;
end.




(*
run:

Token: aa
Delim: ,
Token: bbb
Delim: ;
Token: cccc
Delim: .
Token: ddddd

*)

 



answered Mar 9 by avibootz
0 votes
program SplitAndKeep;

type
  TokenArray = array[1..100] of string;
  CharSet = set of char;

{ Function returns the count of tokens found }
function SplitKeepDelims(S: string; Delims: CharSet; var ResultList: TokenArray): integer;
var
  i, count: integer;
  buffer: string;
begin
  count := 0;
  buffer := '';
  
  for i := 1 to Length(S) do
  begin
    if S[i] in Delims then
    begin
      { If buffer has content, save it as a token before the delimiter }
      if Length(buffer) > 0 then
      begin
        Inc(count);
        ResultList[count] := buffer;
        buffer := '';
      end;
      
      { Save the delimiter itself as a token }
      Inc(count);
      ResultList[count] := S[i];
    end
    else
    begin
      { Append character to current buffer string }
      buffer := buffer + S[i];
    end;
  end;

  { Catch any remaining text at the end }
  if Length(buffer) > 0 then
  begin
    Inc(count);
    ResultList[count] := buffer;
  end;

  SplitKeepDelims := count;
end;

var
  S: string;
  Delims: CharSet;
  Parts: TokenArray;
  NumParts, i: integer;

begin
  S := 'aa,bbb;cccc|ddddd';
  Delims := [',', ';', '|']; { C's strchr becomes the 'in' set operator }

  NumParts := SplitKeepDelims(S, Delims, Parts);

  for i := 1 to NumParts do
  begin
    Write('[', Parts[i], '] ');
  end;
end.




(*
run:

[aa] [,] [bbb] [;] [cccc] [|] [ddddd] 

*)

 



answered Mar 9 by avibootz

Related questions

...