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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,907 questions

55,750 answers

573 users

How to split a string into words in Pascal

2 Answers

0 votes
program SplitStringProgram;

{$mode delphi}{$H+}

uses
  Classes;

function SplitString(const s, delims: string): TStringList;
var
  temp: string;
  i: Integer;
  delimChars: set of Char;
begin
  Result := TStringList.Create;
  Result.StrictDelimiter := True;

  { Build a set of delimiter characters from the regex-like string }
  delimChars := [];

  for i := 1 to Length(delims) do
    case delims[i] of
      ' ', ',', '.', ':', '-', '!', '\': delimChars := delimChars + [delims[i]];
    end;

  temp := s;

  { Replace each delimiter with a space }
  for i := 1 to Length(temp) do
    if temp[i] in delimChars then
      temp[i] := ' ';

  { Split on spaces }
  ExtractStrings([' '], [], PChar(temp), Result);
end;

var
  s: string;
  delims: string;
  tokens: TStringList;
  i: Integer;

begin
  s := '-c, c++.. c#:: -java ,php...python    pascal';
  delims := '[ ,.:\-!]+';

  tokens := SplitString(s, delims);

  for i := 0 to tokens.Count - 1 do
    Writeln(tokens[i]);

  tokens.Free;
end.



{
run:

c
c++
c#
java
php
python
pascal
    
}

 



answered 3 hours ago by avibootz
0 votes
program SplitStringProgram;

{$mode delphi}{$H+}

uses
  SysUtils;

function SplitString(const s, delims: string): TStringArray;
var
  delimSet: set of char;
  normalized: string;
  ch: char;
  parts: TStringArray;
  i, count: Integer;
begin
  delimSet := [];
  for ch in delims do
    Include(delimSet, ch);

  normalized := '';
  for ch in s do
    if ch in delimSet then
      normalized := normalized + ' '
    else
      normalized := normalized + ch;

  parts := normalized.Split(' ');
  
  count := 0;
  for i := 0 to High(parts) do
    if parts[i] <> '' then
    begin
      SetLength(Result, count + 1);
      Result[count] := parts[i];
      Inc(count);
    end;
end;

var
  s, delims: string;
  tokens: TStringArray;
  t: string;
begin
  s := '-c, c++.. c#:: -java! ,php...python:      pascal!';
  delims := ' ,.:\-!';

  tokens := SplitString(s, delims);

  for t in tokens do
    WriteLn(t);
end.



{
run:

c
c++
c#
java
php
python
pascal
    
}

 



answered 2 hours ago by avibootz
...