Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

Biblioteca com funções para Strings

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    Delphi Biblioteca com funções para Strings

    Código PHP:
    unit Strings;
     
    interface
     
    uses
    Windows
    DialogsMessagesSysUtilsClassesControlsStdCtrlsMask;
     
    function 
    ArredontaFloat(Real): Real;
    function 
    RoundNum(Valor:Extended;Decimais:Integer):Extended;
    function 
    Gerapercentual(valor:real;Percent:Real):real;
    function 
    IntToBin(ValueLongInt;SizeInteger): String;
    function 
    BinToInt(ValueString): LongInt;
    function 
    DecToBaseDecimalLongInt; const BaseByte): String;
    function 
    Base10(Base2:Integer):Integerassembler;
    function 
    DecToBinStr(ninteger): string;
    function 
    DecToRomanDecimalLongInt ): String;
    function 
    NumToHex(NumWord): String;
    function 
    Min(ABInteger): Integer;
    function 
    Max(ABInteger): Integer;
    function 
    IntPrime(ValueInteger): Boolean;
    function 
    strByteSize(ValueLongint): String;
    Function 
    StrToReal(inString :String): Real;
    function 
    BuscaDireita(Busca,Text string) : integer;
    function 
    BuscaTroca(Text,Busca,Troca string) : string;
    Function 
    Codifica( const Str1string): String;
    function 
    ContaCaracs(Edit:String): integer;
    function Empty(
    inString:String): Boolean;
    function 
    LTrim(Texto:String):String;
    function 
    Maiuscula(Texto:String): String;
    function 
    Padr(s:string;n:integer):string;
    function 
    RemoveAcentos(Str:String): String;
    function 
    ReplicateCaracter:StringQuant:Integer ): String;
    function 
    RTrim(Texto:String):String;
    function 
    StringToFloat(string) : Extended;
    Function 
    Strs(I:longint):string ;
    function 
    StrToPChar(const Strstring): PChar;
    function 
    Alltrim(const Searchstring): string;
    function 
    StrZero(Zeros:string;Quant:integer):String;
    function 
    StrZerodec(NumeroDoubleTotalDecimalinteger): string;
    function 
    Padl(s:string;n:integer):string;
    Function 
    wordcount(str string) : integer;
    function 
    LineIsEmpty(Text:string):boolean;
    function 
    PadC(S:string;Len:byte):string;
    function 
    FullFill(Str:String;FLen:byte;symb:char):string;
    function 
    Before(const SearchFindstring): string;
    function 
    after(const SearchFindstring): string;
    function 
    MaskString(ValorString) : String;
    function 
    EncryptSenha:String ): String;
    function 
    ExisteInt(Texto:String): Boolean;
     
    implementation
     
    function ArredontaFloat(Real): Real;
    {
    Arredonda um número float para convertê-lo em String}
    Begin
      
    if 0 Then
      begin
      
    if Frac(x) > 0.5 Then
      begin
      x 
    := Frac(x);
      
    end
      
    else
      
    begin
      x 
    := Frac(x);
      
    end;
      
    end
      
    else
      
    begin
      x 
    := Frac(x);
      
    end;
      
    result := x
    end
    ;
     
     
    function 
    RoundNum(Valor:Extended;Decimais:Integer):Extended;
    {
    Quando houver,Arredonda uma possivel terceira casa decimal em uma variável}
    var
      
    I:Integer;
      
    Multiplicador:Integer;
    begin
      
    if Decimais 15 then
      begin
      Decimais 
    := 15;
      
    end
      
    else if Decimais 0 then
      begin
      Decimais 
    := 0;
      
    end;
      
    Multiplicador := 1;
      for 
    I:=1 to Decimais do
      
    begin
      Multiplicador 
    := Multiplicador*10;
      
    end;
    Result := round(Valor*Multiplicador)/Multiplicador;
    end;
     
     
    function 
    Gerapercentual(valor:real;Percent:Real):real;
    // Retorna a porcentagem de um valor
    begin
    percent 
    := percent 100;
    try
      
    valor := valor Percent;
    finally
      
    result := valor;
    end;
    end;
     
    // Integer
    function IntToBin(ValueLongInt;SizeInteger): String;
    {
    Converte uma string em binário}
    var
      
    iInteger;
    begin
    Result
    :='';
    for 
    i:=Size downto 0 do
      
    begin
      
    if Value and (1 shl i)<>0 then
      begin
      Result
    :=Result+'1';
      
    end
      
    else
      
    begin
      Result
    :=Result+'0';
      
    end;
      
    end;
    end;
     
     
    function 
    BinToInt(ValueString): LongInt;
    {
    Converte um numero binário em Inteiro}
    var
    i,SizeInteger;
    begin
    Result 
    := 0;
    Size := Length(Value);
    for 
    i:=Size downto 0 do
      
    begin
      
    if Copy(Value,i,1)='1' then
      begin
      Result 
    := Result+(1 shl i);
      
    end;
      
    end;
    end;
     
     
    function 
    DecToBaseDecimalLongInt; const BaseByte): String;
    {
    converte um número decimal na base especificada}
    const
      
    SymbolsString[16] = '0123456789ABCDEF';
    var
      
    scratchString;
      
    remainderByte;
    begin
      scratch 
    := '';
      
    repeat
      remainder 
    := Decimal mod Base;
      
    scratch := Symbols[remainder 1] + scratch;
      
    Decimal := Decimal div Base;
      
    until Decimal );
      
    Result := scratch;
    end;
     
    function 
    Base10(Base2:Integer):Integerassembler;
    {
    Converte uma string em Base 10}
    asm
      cmp eax
    ,100000000 // check upper limit
      
    jb @// ok
      
    mov eax,-// error flag
      
    jmp @exit // exit with -1
    @1:
      
    push ebx // save registers
      
    push esi
      
    xor esi,esi // result = 0
      
    mov ebx,10 // diveder base 10
      
    mov ecx,// 8 nibbles (10^8-1)
    @2:
      
    mov edx,// clear remainder
      
    div ebx // eax DIV 10, edx mod 10
      
    add esi,edx // result = result + remainder[I]
      
    ror esi,// shift nibble
      
    loop @// loop for all 8 nibbles
      
    mov eax,esi // function result
      
    pop esi // restore registers
      
    pop ebx
    @exit:
    end;
     
     
    function 
    DecToBinStr(ninteger): string;
    {
    Converte um numero decimal em binário}
    var
    Sstring;
    iinteger;
    Negativeboolean;
    begin
    if 0 then
      begin
      Negative 
    := true;
      
    end;
    := Abs(n);
    for 
    := 1 to SizeOf(n) * do
      
    begin
      
    if 0 then
      begin
      S 
    := '1';
      
    end
      
    else
      
    begin
      S 
    := '0';
      
    end;
      
    := n shl 1;
      
    end;
    Delete(S,1,Pos('1',S) - 1);//remove leading zeros
    if Negative then
      begin
      S 
    := '-' S;
      
    end;
    Result := S;
    end;
     
     
    function 
    DecToRomanDecimalLongInt ): String;
    {
    Converte um numero decimal em algarismos romanos}
    const
    Romans: Array[1..13of String = ( 'I''IV''V''IX''X''XL''L''XC''C''CD''D''CM''M' );
    Arabics: Array[1..13of Integer =( 1459104050901004005009001000);
    var
    iInteger;
    scratchString;
    begin
      scratch 
    := '';
      for 
    := 13 downto 1 do
      while ( 
    Decimal >= Arabics[i] ) do
      
    begin
      Decimal 
    := Decimal Arabics[i];
      
    scratch := scratch Romans[i];
      
    end;
      
    Result := scratch;
    end;
     
     
    function 
    NumToHex(NumWord): String;
    //Converte um numero em Hexadecimal
    Var
    string[16];
    BHi,BLo byte;
    Begin
    := '0123456789ABCDEF';
    BHi := Hi(Num);
    BLo := Lo(Num);
    result := copy(L,succ(BHi shr 4),1) +
      
    copy(L,succ(BHi and 15),1) +
      
    copy(L,succ(BLo shr 4),1) +
      
    copy(L,succ(BLo and 15),1);
    end;
     
     
    function 
    Min(ABInteger): Integer;
    {
    Compara dois valores Retornando o maior deles}
    begin
      
    if B then
      Result 
    := A
      
    else
      
    Result := B;
    end;
     
     
    function 
    Max(ABInteger): Integer;
    {
    Compara dois valores Retornando o maior deles}
    begin
      
    if B then
      Result 
    := A
      
    else
      
    Result := B;
    end;
     
     
    function 
    IntPrime(ValueInteger): Boolean;
    {
    Testa se um numero é primo ou não}
    var
    integer;
    begin
    Result 
    := False;
    Value := Abs(Value);
    if 
    Value mod 2 <> 0 then
      begin
      i 
    := 1;
      
    repeat
      i 
    := 2;
      
    Result:= Value mod i 0
      until Result 
    or ( Trunc(sqrt(Value)) );
      
    Result:= not Result;
      
    end;
    end;
     
    function 
    strByteSize(ValueLongint): String;
    {
    Retorna uma conversão de Bytes para integer}
    Const
    KBYTE Sizeof(Byteshl 10;
    MBYTE KBYTE shl 10;
    GBYTE MBYTE shl 10;
    begin
      
    if Value GBYTE then
      begin
      Result 
    := FloatToStrF(Round(Value GBYTE),ffNumber,6,0)+' GB';
      
    end
      
    else if Value MBYTE then
      begin
      Result 
    := FloatToStrF(Round(Value MBYTE),ffNumber,6,0)+' MB';
      
    end
      
    else if Value KBYTE then
      begin
      Result 
    := FloatToStrF(Round(Value KBYTE),ffNumber,6,0)+' KB';
      
    end
      
    else
      
    begin
      Result 
    := FloatToStrF(Round(Value),ffNumber,6,0)+' Byte';
      
    end;
    end;
     
    // Strings
    Function StrToReal(inString :String): Real;
    {
    converte um número em Float}
    Var
      
    Real;
      
    Integer;
    Begin
      Val
    (inString,i,k);
      
    StrToReal := i;
    End;
     
    function 
    BuscaDireita(Busca,Text string) : integer;
    {
    Pesquisa um caractere à direita da string,
     
    retornando sua posição}
    var 
    n,retorno integer;
    begin
    retorno 
    := 0;
    for 
    := length(Textdownto 1 do
      
    begin
      
    if Copy(Text,n,1) = Busca then
      begin
      retorno 
    := n;
      break;
      
    end;
      
    end;
    Result := retorno;
    end;
     
    function 
    BuscaTroca(Text,Busca,Troca string) : string;
    Substitui um caractere dentro da string}
    var 
    integer;
    begin
    for := 1 to length(Text) do
      
    begin
      
    if Copy(Text,n,1) = Busca then
      begin
      Delete
    (Text,n,1);
      
    Insert(Troca,Text,n);
      
    end;
      
    end;
    Result := Text;
    end;
     
     
    Function 
    Codifica( const Str1string): String;
    {
    Encripta uma string}
    var
    Mascara,Str2String;
    PonMPonSByte;
    begin
    Mascara 
    := '#$%$'#13#12;
    Str2 := '';
    PonM := 1;
    for 
    PonS:=1 to length(Str1) do
      
    begin
      AppendStr
    Str2ChrOrd(Str1[PonS]) Xor Ord(Mascara[PonM])));
      
    IncPonM);
      if 
    PonM>Length(Mascarathen
      begin
      PonM
    :=1;
      
    end;
      
    Result := Str2;
      
    end;
    end;
     
     
    function 
    ContaCaracs(Edit:String): integer;
    {
    Retorna quantos caracteres tem um Edit especificado}
    begin
    Result 
    := Length(Edit);
    end;
     
     
    function Empty(
    inString:String): Boolean;
    {
    Testa se a variavel está vazia ou não}
    Var
      
    index Byte;
    Begin
      index 
    := 1;
      Empty := 
    True;
      while (
    index <= Length(inString))and (index <> 0) do
      
    begin
      
    if inString[index] = ' ' Then
      begin
    inc
    (index)
      
    end
      
    else
    Begin
      
    Empty := False;
      
    index := 0
    end
    ;
      
    end;
    end;
     
     
    function 
    LTrim(Texto:String):String;
    {
    Remove os Espaços em branco à direita da string}
    var
      
    Integer;
    begin
      I 
    := 0;
      while 
    True do
      
    begin
      inc
    (I);
      if 
    length(Textothen
      
    break;
      if 
    Texto[I] <> #32 then
      
    break;
      
    end;
      
    Result := Copy(Texto,I,length(Texto));
    end;
     
     
    function 
    Maiuscula(Texto:String): String;
    {
    Converte a primeira letra do texto especificado para 
    maiuscula e 
    as restantes para minuscula}
    var
    OldStartInteger;
    begin
    if Texto <> '' then
      begin
      Texto 
    := UpperCase(Copy(Texto,1,1))+LowerCase(Copy(Texto,2,Length(Texto)));
      
    Result := Texto;
      
    end;
    end;
     
    function 
    Padr(s:string;n:integer):string;
    {
    alinha uma string à direita}
    begin
    Result
    :=Format('%'+IntToStr(n)+'.'+IntToStr(n)+'s',[s]);
    end;
     
     
    function 
    RemoveAcentos(Str:String): String;
    {
    Remove caracteres acentuados de uma string}
    Const 
    ComAcento 'àâêôûãõáéíóúçüÀÂÊÔÛÃÕÁÉÍÓÚÇÜ';
      
    SemAcento 'aaeouaoaeioucuAAEOUAOAEIOUCU';
    Var
    Integer;
    Begin
    For := 1 to Length(Str) do
      
    Begin
      
    if Pos(Str[x],ComAcento)<>0 Then
      begin
      Str
    [x] := SemAcento[Pos(Str[x],ComAcento)];
      
    end;
      
    end;
    Result := Str;
    end;
     
     
    function 
    ReplicateCaracter:StringQuant:Integer ): String;
    {
    Repete o mesmo caractere várias vezes}
    var 
    Integer;
    begin
    Result 
    := '';
      for 
    := 1 to Quant do
      
    Result := Result Caracter;
    end;
     
     
    function 
    RTrim(Texto:String):String;
    {
    Remove os Espaços em branco à esquerda da string}
    var
      
    Integer;
    begin
      I 
    := length(Texto)+1;
      while 
    True do
      
    begin
      Dec
    (I);
      if 
    <= 0 then
      
    break;
      if 
    Texto[I] <> #32 then
      
    break;
      
    end;
      
    Result := Copy(Texto,1,I);
    end;
     
     
    function 
    StringToFloat(string) : Extended;
    Filtra uma string qualquerconvertendo as suas partes
      numéricas para sua representação decimal
    por exemplo:
      
    'R$ 1.200,00' para 1200,00 '1AB34TZ' para 134}
    var
      
    :Integer;
      
    string;
      
    SeenDecimal,SeenSgn Boolean;
    begin
      t 
    := '';
      
    SeenDecimal := False;
      
    SeenSgn := False;
      {
    Percorre os caracteres da string:}
      for 
    := Length(sdownto 0 do
      {
    Filtra a stringaceitando somente números e separador decimal:}
      if (
    s[iin ['0'..'9''-','+',DecimalSeparator]) then
      begin
      
    if (s[i] = DecimalSeparator) and (not SeenDecimalthen
      begin
      t 
    := s[i] + t;
      
    SeenDecimal := True;
      
    end
      
    else if (s[iin ['+','-']) and (not SeenSgn) and (1then
      begin
      t 
    := s[i] + t;
      
    SeenSgn := True;
      
    end
      
    else if s[iin ['0'..'9'then
      begin
      t 
    := s[i] + t;
      
    end;
      
    end;
      
    Result := StrToFloat(t);
      
    end;
     
     
    Function 
    Strs(I:longint):string ;
    {
    Converte uma variavel numérica em string}
    Var
     
    string[16] ;
    begin
      STR 
    (I,X) ;
      
    Strs := ;
    end ;
     
     
    function 
    StrToPChar(const Strstring): PChar;
    {
    Converte String em Pchar}
    type
      TRingIndex 
    0..7;
    var
      
    Ring: array[TRingIndexof PChar;
      
    RingIndexTRingIndex;
      
    PtrPChar;
    begin
      Ptr 
    := @Str[Length(Str)];
      
    Inc(Ptr);
      if 
    Ptr^ = #0 then
      
    begin
      Result 
    := @Str[1];
      
    end
      
    else
      
    begin
      Result 
    := StrAlloc(Length(Str)+1);
      
    RingIndex := (RingIndex 1mod (High(TRingIndex) + 1);
      
    StrPCopy(Result,Str);
      
    StrDispose(Ring[RingIndex]);
      
    Ring[RingIndex]:= Result;
      
    end;
    end;
     
     
    function 
    Alltrim(const Searchstring): string;
    {
    Remove os espaços em branco de ambos os lados da string}
    const
    BlackSpace = [#33..#126];
    var
    Indexbyte;
    begin
    Index
    :=1;
    while (
    Index <= Length(Search)) and not (Search[Indexin BlackSpace) do
      
    begin
      Index
    :=Index 1;
      
    end;
    Result:=Copy(SearchIndex255);
    Index := Length(Result);
    while (
    Index 0) and not (Result[Indexin BlackSpace) do
      
    begin
      Index
    :=Index 1;
      
    end;
    Result := Copy(Result1Index);
    end;
     
     
    function 
    StrZero(Zeros:string;Quant:integer):String;
    {
    Insere Zeros à frente de uma string}
    var
    I,Tamanho:integer;
    auxstring;
    begin
      aux 
    := zeros;
      
    Tamanho := length(ZEROS);
      
    ZEROS:='';
      for 
    I:=1 to quant-tamanho do
      
    ZEROS:=ZEROS '0';
      
    aux := zeros aux;
      
    StrZero := aux;
    end;
     
     
    function 
    StrZerodec(NumeroDoubleTotalDecimalinteger): string;
    {
    Insere Zeros e decimais à frente de uma string}
    var
     
    TempStrstring;
    begin
     Str
    (Numero:0:Decimal,TempStr);
     while 
    length(TempStr) < Total do
      
    begin 
      Insert
    ('0',TempStr,1);
      
    end;
    Result := TempStr;
     
    end;
     
     
     
    function 
    Padl(s:string;n:integer):string// Alinhamento pela esquerda
    {alinha uma string à esquerda}
    begin
      Result
    :=Format('%-'+IntToStr(n)+'.'+IntToStr(n)+'s',[s]);
    end;
     
     
    Function 
    wordcount(str string) : integer;
    // Retorna o número de palavras que contem em uma string
    var
    count integer;
    integer;
    len integer;
    begin
    len 
    := length(str);
    count := 0;
    := 1;
    while 
    <= len do
      
    begin
      
    while ((<= len) and ((str[i] = #32) or (str[i] = #9) or (Str[i] = ';'))) do
      
    inc(i);
      if 
    <= len then
      inc
    (count);
      while ((
    <= len) and ((str[i] <> #32) and (str[i] <> #9) and (Str[i] <> ';'))) do
      
    inc(i);
      
    end;
      
    wordcount := count;
    end;
     
     
    function 
    LineIsEmpty(Text:string):boolean;
    // Testa se uma linha de texto está vazia ou não
    var
      
    i:byte;
    begin
    for i:=1 to length(Text) do
      
    begin
      
    if Text[i]<>' ' then
      begin
      result 
    := False;
      exit;
      
    end;
      
    end;
    Result := True;
    end;
     
     
    function 
    PadC(S:string;Len:byte):string;
    // Centraliza uma string em um espaço determinado
    var
     
    Str:String;
     
    L:byte;
    begin
    str 
    :='';
    if 
    len Length(sthen
      begin
      Result 
    := '';
      Exit;
      
    end;
    l:=(Len-Length(S)) div 2;
    while 
    do
      
    begin
      str
    :=str+' ';
      
    dec(l);
      
    end;
    for 
    l:=1 to length(S) do
      
    begin
      str 
    := str+s[L];
      
    end;
     
    Result := str;
    end;
     
     
    function 
    FullFill(Str:String;FLen:byte;symb:char):string;
    // Preenche o restante da string com um caractere especificado
    var
     
    S:String;
     
    i:byte;
    begin
    s
    :=Str;
    if 
    Length(s) >= FLen then
      begin
      Result 
    := s;
      Exit;
      
    end;
    for 
    := Length(sto FLen do
      
    begin
      S 
    := symb;
      
    end;
    Result := s;
    end;
     
     
    function 
    Before(const SearchFindstring): string;
    {
    Retorna uma cadeia de caracteres antecedentes a uma parte da string selecionada}
    const
      
    BlackSpace = [#33..#126];
    var
      
    indexbyte;
    begin
      index
    :=Pos(FindSearch);
      if 
    index 0 then
      Result
    :=Search
      
    else
      
    Result:=Copy(Search1index 1);
    end;
     
     
    function 
    after(const SearchFindstring): string;
    {
    Retorna uma cadeia de caracteres após a parte da string selecionada}
    var
    indexbyte;
    begin
    index 
    := Pos(FindSearch);
    if 
    index 0 then
      begin
      Result 
    := '';
      
    end
    else
      
    begin
      Result 
    := Copy(Searchindex Length(Find), 255);
      
    end;
    end;
     
     
    function 
    MaskString(ValorString) : String;
    begin
    Result 
    := FormatMaskText('!aaaaaaaaaaaaaa;0; ',(FormatFloat('#,##0.00',StrToFloat(valor))));
    end;
     
    function 
    EncryptSenha:String ): String;
    {
    Encripta uma String}
    Const
      
    Chave String 'Jesus';
    Var
      
    x,Integer;
      
    NovaSenha String;
    begin
      
    for := 1 to LengthChave ) do begin
      NovaSenha 
    := '';
      for 
    := 1 to LengthSenha ) do
      
    NovaSenha := NovaSenha chr( (Ord(Chave[x]) xor Ord(Senha[y])));
      
    Senha := NovaSenha;
      
    end;
      
    result := Senha;
    end;
     
    function 
    ExisteInt(Texto:String): Boolean;
    {
    Testa se em uma string existe um numero inteiro valido ou não}
    var
    i:integer;
    begin
    try
      
    := StrToInt(Texto);
      
    Result := True;
    except
      Result 
    := False;
    end;
    end;
    end
    ~# Criado pela [IN]Segurança #~
X
Working...
X