GARANTIR DESCONTO

Fórum Arquivo texto #196183

20/11/2003

0

Bom dia!!!
Existe algum comando em Delphi similar ao ´split´ do ASP ou ao ´explode´ do PHP? O que ele faz é o seguinte: a partir de uma string, [b:a56553bc27]que usa vírgulas como separador [/b:a56553bc27]ele joga esses conteúdos numa variável array.

Ex.:

10253,JOÃO ANTONIO DA SILVA,R. DAS PEDRAS,PORTO ALEGRE

ficaria um array:
(10253|JOÃO ANTONIO DA SILVA|R. DAS PEDRAS|PORTO ALEGRE)


Samucacb

Samucacb

Responder

Posts

20/11/2003

Airto

simples logica, faça um for na sua string, quando vc encontar a virgula, vc substitui pela barra e conctena com sua string


for aux2 := 1 to length(Edit1.text) do
begin
ch := copy(Edit1.text, aux2, 1);
if ch = ´,´ then
begin
inc(aux3);
case aux3 of
1: variavel := aux;
end;
aux := ´´;
end
else
aux := aux + ch;
end;


Abraços


Responder

Gostei + 0

20/11/2003

Samucacb

Obrigado, porém, isso é extremamente lente pois é um arquivo texto com muitas linhas e cada linha tem uns 500 caracteres...
Gostaria de insistir: no ASP e PHP existe uma função que faz isto.... alguém viu isto em Delphi?


Responder

Gostei + 0

20/11/2003

Anorex

Use assim: stringreplace(sLinha, ´,´, ´|´, [rfReplaceAll, rfIgnoreCase]);


Responder

Gostei + 0

20/11/2003

Marcelo Saviski

Não tem ou eu pelo menos nuca vi

mas tanto iria fazer se vc usar uma função do Delphi, do PHP ou uma que vc fizer que qualquer uma dela iria ficar um pouco lenta com um arquivo enorme

talvez não de para perceber isso na Web, mas um código em Delphi iria ser tão ou mais rápido quanto um código em PHP

poderia tentar algo assim:

function SeparaPalavras(Texto: string): TStrings;
begin
  Result := TStringList.Create;
  Result.Text := Texto;
  StringReplace(Result.Text, ´,´, #1013, [rfReplaceAll]);
end;


só isso :)

e pode usar ela assim:

Coloque 2 Memos no form e um Botão
insire este código no Botão:

Memo2.Text := SeparaPalavras(Memo1.Text);


coloque as palavras no Memo1 separadas por vírgula, clique no botão e elas deverão aparecer separadas no Memo2

funcionou? era isso que você queria?


Responder

Gostei + 0

20/11/2003

Aroldo Zanela

Colega,

Uma dica de Peter Below (TeamB)
> I´m fairly new to Delphi and would like to find some string manipulation > utilities that will do things like parse a string based on a token > separator, or split a string into multiple strings (like in an array of > strings or such) based on end-of-line characters (dos uses cr/lf; unix uses > lf), etc. TStringlist can do much of what you want.


Var
   sl: TStringlist;
   
  sl:= TStringlist.Create;
  try
    sl.Text := SomeStringWithLinebreaks;
    
This would dissect the string into individual lines, It can handle both DOS 
and Unix-style lineends. 

For Delphi 6 you can use the following to split a string on a delimiter:

   sl.Delimiter := someChar;
   sl.DelimitedText := someTextWithDelimiters;
   
For older Delphi versions you have to write your own routine to do that but 
that is not really *that* much work, since you do it only once. Here are a 
couple of routines you may find useful.

Unit Utils;

interface
  Uses Classes;

  {: Return the position of the first instance of ch in S, or 0 if
     ch was not found. }
  Function Scan( ch: Char; Const S: String ): Integer;

  {: Return the position of the first instance of ch in S after
     position fromPos, or 0 if ch was not found. }
  Function IScan( ch: Char; Const S: String; fromPos: Integer ): Integer;

  {: Return the position of the last instance of ch in S, or 0 if
     ch was not found. }
  Function RScan( ch: Char; Const S: String ): Integer;

  {: Return the position of the last instance of ch in S before fromPos, 
     or 0 if ch was not found. }
  Function RIScan( ch: Char; Const S: String; fromPos: Integer ): Integer;

  {: Split the passed string into substrings at the position of the
     separator character and add the substrings to the passed list.
     The list is not cleared first! }
  Procedure SplitString( Const S: String; separator: Char; 
                         substrings: TStringList );

  {: Split the passed string into substrings at the position of the
     separator string and add the substrings to the passed list.
     The list is not cleared first! }
  Procedure SplitString2( S: String; const separator: String; 
                         substrings: TStringList );

  {: Concatenate the items of the passed list into a single string,
     separating the items with the separator string. }
  Function ConcatSubstrings( const separator: String;
                              substrings: TStringList ): String;

  {: Concatenate the items of the passed list into a single string,
     separating the items with the separator string and enclosing
     each item with the quote characters. Instances of the quote
     character inside an item will be doubled. }
  Function ConcatQuotedSubstrings( separator, quote : Char; 
                              substrings: TStringList ): String;

  {: Pad a string to a given length by adding characters on the left. }
  Function PadLeft( Const S: String; toLength: Integer;
                    withChar: Char ): String;

  {: Pad a string to a given length by adding characters on the right. }
  Function PadRight( Const S: String; toLength: Integer;
                     withChar: Char ): String;

implementation

Uses SysUtils;

Function IScan( ch: Char; Const S: String; fromPos: Integer ): Integer;
  Var
    i: Integer;
  Begin
    Result := 0;
    For i := fromPos To Length(S) Do Begin
      If S[i] = ch Then Begin
        Result := i;
        Break;
      End; { If }
    End; { For }
  End; { IScan }

Function Scan( ch: Char; Const S: String): Integer;
  Begin
    Result := IScan( ch, S, 1 );
  End; { Scan }

Function RIScan( ch: Char; Const S: String; fromPos: Integer ): Integer;
  Var
    i: Integer;
  Begin
    Result := 0;
    For i := fromPos DownTo 1 Do Begin
      If S[i] = ch Then Begin
        Result := i;
        Break;
      End; { If }
    End; { For }
  End; { RIScan }

Function RScan( ch: Char; Const S: String ): Integer;
  Begin
    Result := RIScan( ch, S, Length(S) );
  End; { RScan }

Procedure SplitString( Const S: String; separator: Char; substrings: 
TStringList );
  Var
    i, n: Integer;
  Begin
    If Assigned( substrings ) and ( Length( S )>0 ) Then Begin
      i:= 1;
      Repeat 
        n := IScan( separator, S, i );
        If n = 0 Then 
          n:= Length( S )+1;
        substrings.Add( Copy( S,i,n-i ));
        i:= n+1;
      Until i>Length( S );
    End; { If }
  End; { SplitString }

Procedure SplitString2( S: String; const separator: String; 
                         substrings: TStringList );
  Var
    i: Integer;
  Begin 
    If Assigned( substrings ) and ( Length( S )>0 ) Then Begin
      Repeat 
        i:= Pos( separator, S );
        If i > 0 Then Begin 
          substrings.Add( Copy( S, 1, i-1 ));
          Delete( S, 1, i + Length( separator ) -1 );
        End { If }
        Else Begin 
          substrings.Add( S );
          Break; 
        End; { Else }
      Until Length( S ) = 0;
    End; 
  End; { SplitString2 }

Function PadLeft( Const S: String; toLength: Integer;
                  withChar: Char ): String;
  Begin { PadLeft }
    If Length(S) < toLength Then
      Result := StringOfChar( withChar, toLength - Length(S)) + S
    Else
      Result := S;
  End; { PadLeft }

Function PadRight( Const S: String; toLength: Integer; 
                   withChar: Char ): String; 
  Begin { PadRight }
    If Length(S) < toLength Then 
      Result := S + StringOfChar( withChar, toLength - Length(S))
    Else
      Result := S;
  End; { PadRight }

Function ConcatSubstrings( const separator: String;
   substrings: TStringList ): String;
  Var
    i: Integer;
  Begin
    Result := EmptyStr;
    For i := 0 To substrings.count-1 Do Begin
      If i > 0 Then
        Result:= Result + separator;
      Result:= Result + substrings[i];
    End; { For }
  End; { ConcatSubstrings }

Function ConcatQuotedSubstrings( separator, quote : Char; 
    substrings: TStringList ): String;
  Var
    i: Integer;
  Begin
    Result := EmptyStr;
    For i := 0 To substrings.count-1 Do Begin
      If i > 0 Then 
        Result:= Result + separator;
      Result:= Result + AnsiQuotedStr( substrings[i], quote );
    End; { For }
  End; { ConcatQuotedSubstrings }

end.



Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar