Einzelnen Beitrag anzeigen

Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#4

AW: Text (Strings) Zeilenumbrüche einfügen aufgrund von Satzzeichen.!?

  Alt 30. Jun 2022, 13:46
Kleine Verbesserungsvorschläge für Jumpys Code, aber grundsätzlich ist sein Ansatz die beste Variante (die mir grad einfällt)
Delphi-Quellcode:
function SplitText(AText: String): TStrings;
var i: Integer;
    line: String;
    specialCharCount: Integer;
begin
  Result := TStringList.Create;

  line := '';
  specialCharCount := 2;
  for i:= 1 to Length(AText) do
  begin
    line := line + AText[i];
    if CharInSet(AText[i], ['!', '.', '?']) then
      dec(specialCharCount);
    
    if (specialCharCount = 0) then
    begin
      Result.Add(line);
      line := '';
      specialCharCount := 3;
    end;
  end;

  // Rest als eine Zeile hinzufügen
  if (line <> '') then
    Result.Add(line);
end;
Edit: Runterzählen von "specialCharCount" erspart einem eine Max Variable. Danke an Blup
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."

Geändert von Neutral General (30. Jun 2022 um 17:20 Uhr)
  Mit Zitat antworten Zitat