PDA

Ver la Versión Completa : formato a una string


cmm07
22-12-2007, 16:41:58
hola, doy las gracias por aver contestado anteriormente, pero lamentablemente tengo otro problemilla, ¿Como puedo poner un formato en una string?, por ej.

Esto lo que tengo:
214KGDHJHENB123BAD
y me gustaría que se viera así:
214-KGD-HJH-ENB-123-BAD
como puedo darle ese formato para que aparesca las "-" cada tres digitos....


lo pudo hacer pero con puros números y para ello ocupo el FormatFloat, pero ¿como puedo hacer esto con caracteres?

Gracias..

*GRACIAS A TODO EL EQUIPO CLUBDELPHI Y A LOS USUARIOS..


he buscado por el famoso buscador google y no encuentro nada aver si vosotros podeis ayudarme

GRACIAS.

seoane
22-12-2007, 17:10:10
Voy a probar a escribirla directamente aquí:

function Separar(Str: String): String;
begin
Result:= EmptyStr;
while Length(Str) > 3 then
begin
Result:= Result + Copy(Str,1,3) + '-';
Delete(Str,1,3);
end;
Result:= Result + Str;
end;


Por ejemplo:

ShowMessage(Separar('214KGDHJHENB123BAD'));


¿Funciona? :confused:

cmm07
22-12-2007, 17:20:31
Gracias, muxas gracias de verdad gracias, si me funciono de maravilla ¿como te puedo agradecer? jjejee:D

*SALU2 A TODOS LOS DEL CLUBDELPHI

Solo un detallito, cuando cambio :
de esto:


function Separar(Str: String): String;
begin
Result:= EmptyStr;
while Length(Str) > 3 then
begin
Result:= Result + Copy(Str,1,3) + '-';
Delete(Str,1,3);
end;
Result:= Result + Str;
end;


a esto


function Separar(Str: String): String;
begin
Result:= EmptyStr;
while Length(Str) > 3 then
begin
Result:= Result + Copy(Str,1,6) + '-';
Delete(Str,1,6);
end;
Result:= Result + Str;
end;


el resultado es:
214KGD-HJHENB-123BAD-
coloca una "-" al final como puedo mejorar esto...

GRACIAS DE TODAS FORMAS...

dec
22-12-2007, 17:28:02
Hola,

Se me adelanto Domingo, pero, ya tenía escrito esto... :D


(**
* Put one or more separator into a string.
*
* Call example:
*
* PutStringSeparator('214KGDHJHENB123BAD', '-', 3)
*
* Example result:
*
* 214-KGD-HJH-ENB-123-BAD
*
* @param String aString String to put separators into it
* @param String separator Separator to put into the string
* @param Integer interval Interval characters to put the separator
* @result String Entry string with the appropiate separator(s)
*
*)
function PutStringSeparator(aString,
separator: string; interval: integer): string;
var
i, j, strLen: integer;
begin
j := 1;
strLen := Length(aString);
for i := 1 to strLen do begin
result := result + aString[i];
if (j = interval) then begin
if (i < strLen) then begin
result := result + separator;
end;
j := 0;
end;
Inc(j);
end;
end;

seoane
22-12-2007, 17:37:48
... como puedo mejorar esto...


Pues cambiando también este tres por un seis

while Length(Str) > 3 then


:)

cmm07
22-12-2007, 17:41:02
Los 2 estan super buenos Gracias, muxas gracias


me sirvió de maravilla los 2 agradezco por las respuestas

GRACIAS

*gracias a todos los de clubdelphi