Gehe zu deutscher Webseite

ViaThinkSoft CodeLib

This article is in:
CodeLibProgramming aidsDelphi

Hallo!

Ich habe soeben die Funktionen fertiggeschrieben, mit denen man einen String in seine Einzelteile splitten kann (getrennt durch Leerzeichen). Damit die Einzelteile des Strings auch Leerzeichen enthalten können, kann man einen Teil mit Anführungsszeichen einklammern, so dass die Leerzeichen im Teilstring nicht als Trenner identifiziert werden.
Achtung *!*
- Das Benutzen der Anführungszeichen funktioniert erst ab dem 2. Teilstring.
- Die Einzelnen Teilstrings dürfen nicht mit einem Leerzeichen anfangen bzw. enden (Bsp.: '... " 123" ...' oder '... "123 " ...')

In Zukunft werden die Funktionen den Umgang mit den eben beschriebenden Problemfällen jedoch ebenfalls beherrschen.

Hier die Funktionen:

function CleanString(source:string):string;
var i,quot:integer;
begin
//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
// (c) Copyright 2004                                                           //
// ViaThinkSoft - intelligent software for everyone                             //
//                                                                              //
// Funktion: CleanString                                                        //
//                                                                              //
// Parameter:                                                                   //
// source   -   Quellstring                                                     //
//                                                                              //
// Beschreibung: Entfernt doppelte Leerzeichen aus einem String                 //
//                                                                              //
// Version: 1.0.0                                                               //
//                                                                              //
// Autor: Victor-Phillip Negoescu                                               //
//                                                                              //
// Changelog:                                                                   //
//                                                                              //
// 29.08.2004  -  v1.0   -  victor  -  Release                                  //
//                                                                              //
//                                                                              //
// Eigentum von ViaThinkSoft  -  Property of ViaThinkSoft                       //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

  for i := 0 to length(source)-1 do
  begin
    if source[i] = '"' then Inc(quot);
    if ( (source[i] = ' ') and (source[i-1] = ' ') and (quot mod 2 = 0) ) then source := copy(source,1,i-1) + copy(source,i+1,length(source));
  end;
  result := source;
end;

function SplitString(source:string; part:integer): string;
var i,quot:integer;
nextspace,backup:string;
begin
//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
// (c) Copyright 2004                                                           //
// ViaThinkSoft - intelligent software for everyone                             //
//                                                                              //
// Funktion: SplitString                                                        //
//                                                                              //
// Parameter:                                                                   //
// source   -   Quellstring                                                     //
// part     -   Teil des Strings, der extrahiert werden soll                    //
//                                                                              //
// Beschreibung: Funktion zum Aufsplitten eines Strings in seine Einzelteile    //
//                                                                              //
// Version: 1.0.0                                                               //
//                                                                              //
// Autor: Victor-Phillip Negoescu                                               //
//                                                                              //
// Changelog:                                                                   //
//                                                                              //
// 29.08.2004  -  v1.0   -  victor  -  Release                                  //
//                                                                              //
//                                                                              //
// Eigentum von ViaThinkSoft  -  Property of ViaThinkSoft                       //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

backup := source;
quot := 0;
  for i := 0 to part - 2 do
  begin
    nextspace := '';
    if copy(source,1,1) = '"' then begin source := copy(source,2,length(source)); Inc(quot); end;

    if ( (pos('"',source) = 0) and (pos(' ',source) > 0) ) then nextspace := ' ';
    if ( (pos('"',source) > pos(' ',source)+1) and (pos(' ',source) > 0) ) then nextspace := ' ';
    if ( (pos('"',source) > 0) and (pos(' ',source) > 0) and (copy(source,pos('"',source)-1,1) = ' ') ) then
    begin
      if quot mod 2 = 0 then nextspace := ' "';
      Inc(quot);
    end;
    if ( (pos('"',source) > 0) and (pos(' ',source) > 0) and (copy(source,pos('"',source)+1,1) = ' ') ) then
    begin
      if quot mod 2 > 0 then nextspace := '" ';
      Inc(quot);
    end;

    if nextspace = '' then else source := copy(source,pos(nextspace,source)+length(nextspace),length(source));
  end;
  nextspace := '';
  if copy(source,1,1) = '"' then begin source := copy(source,2,length(source)); Inc(quot); end;

  if ( (pos('"',source) > 0) or (pos(' ',source) > 0) ) then
  begin
    if ( (pos('"',source) = 0) and (pos(' ',source) > 0) ) then nextspace := ' ';
    if ( (pos('"',source) > 0) and (pos(' ',source) > 0) and (copy(source,pos('"',source)-1,1) = ' ') ) then
    begin
      if quot mod 2 = 0 then nextspace := ' "';
      Inc(quot);
    end;
    if ( (pos('"',source) > 0) and (pos(' ',source) > 0) and (copy(source,pos('"',source)+1,1) = ' ') ) then
    begin
      if quot mod 2 > 0 then nextspace := '" ';
      Inc(quot);
    end;

    if nextspace = '' then else source := copy(source,1,pos(nextspace,source)-1);
    if copy(source,length(source),1) = '"' then source := copy(source,1,length(source)-1);
    if source = '' then source := SplitString(backup,part+1);
  end;
  result := source;
end;

Erst den String mit der Funktion CleanString reinigen und dann durch die Funktion SplitString schicken.

Die Funktionen stehen unter der GPL.
Victor-Phillip Negoescu
ViaThinkSoft Gründer