PDA

Ver la Versión Completa : Buscar Mayusculas dentro de un string


ZayDun
19-04-2012, 15:55:32
Hola, tengo un string que recibe muchos datos, pero los que me interesan estan en mayuscula y son de 5 caracteres, el texto del string no siempre es el mismo por lo que no puedo buscar ninguna relacion en las palabras. mi idea es buscar por mayuscula o por numero de caracteres ya que siempre son 5. alguna posible solucion? Gracias.

Ejemplo: caracteresaleatorios FGHJK continuaeltexto

Casimiro Notevi
19-04-2012, 16:46:45
¿Y siempre hay un espacio antes de los 5 caracteres en mayúsculas?

ZayDun
19-04-2012, 16:57:13
si,siempre hay un espacio tanto al empezar como al terminar.

Caro
19-04-2012, 16:58:44
Hola ZayDun, si esta entre espacios puedes pasarlo a un StringList y preguntar si el primer caracter es mayuscula.


var
sl :TStringList;
begin
cad := 'caracteresaleatorios FGHJK continuaeltexto KJGTU';
sl := TStringList.Create;
sl.Delimiter := #32;
sl.DelimitedText := cad;
for i:=0 to sl.Count-1 do
begin
if sl[i][1] in [#65..#90] then
showmessage(sl[i]);
end;
end;


Saluditos

ZayDun
19-04-2012, 17:08:06
Funciona como queria el unico "problema" que encuentro es que si la palabra empieza por un numero no la detecta, (1GHRT) pero aun asi gracias por la ayuda!.

MartinS
19-04-2012, 17:11:39
Hola: No se si te sirve o es lo que andas buscando pero lo siguiente te muestra que es lo que esta en mayuscula

function Mayuscula(Cad: string): string;
var
i: Integer;
begin
for i:= 1 to Length(Cad) do
if (Cad[i] in ['A'..'Z']) or (Cad[i] in ['0'..'9']) then
Result:= Result + Cad[i];
end;

procedure TForm1.Button1Click(Sender: TObject);
Var Texto : String;
begin
Texto := MAyuscula(Edit1.Text);
ShowMessage(Texto);
end;

Saludos

Caro
19-04-2012, 17:52:35
Hola: No se si te sirve o es lo que andas buscando pero lo siguiente te muestra que es lo que esta en mayuscula

Código Delphi [-] (http://www.clubdelphi.com/foros/#)function Mayuscula(Cad: string): string; var i: Integer; begin for i:= 1 to Length(Cad) do if (Cad[i] in ['A'..'Z']) or (Cad[i] in ['0'..'9']) then Result:= Result + Cad[i]; end;


Hola Martins, en caso de que una palabra sin mayúsculas tenga algún número, también lo tomaría en cuenta.

ZayDu, todo depende de las condiciones que tengas en tu cadena para hacer los controles, tomando en cuenta que dices que son de 5 caracteres, podríamos tomar en cuenta solo esas palabras:


sl := TStringList.Create;
sl.Delimiter := #32;
sl.DelimitedText := cad;
for i:=0 to sl.Count-1 do
begin
if (Length(sl[i])=5) then
begin
if (sl[i][1] in ['A'..'Z']) then
showmessage(sl[i])
else
for j:=1 to 5 do
begin
if (sl[i][j] in ['A'..'Z']) then
begin
showmessage(sl[i]);
break; //si ya ha encontrado una mayuscula nos salimos del for
end;
end;
end;
end;


Saluditos

Caro
19-04-2012, 17:57:25
Hola de nuevo y si es caso es de que tienes que tomar solo los de 5 caracteres y no habrá ninguno que este mezclado con minúsculas:


sl := TStringList.Create;
sl.Delimiter := #32;
sl.DelimitedText := cad;
for i:=0 to sl.Count-1 do
begin
if Length(sl[i])=5 then
showmessage(sl[i])
end;


Saluditos

ecfisa
19-04-2012, 18:05:32
Hola.

Otra opción:

function CincoMayusculas(Cad: string): TStrings;
var
c, p: Integer;
begin
c:= 1;
Result:= TStringList.Create;
while c < Length(Cad) do
begin
if Cad[c] in ['A'..'Z','Ñ','Á','É','Í','Ó','Ú'] then
begin
p:= 1;
while (Cad[c+p] in ['A'..'Z','Ñ','Á','É','Í','Ó','Ú'])and(p < 5) do
Inc(p);
if p = 5 then
begin
Result.Add(Copy(Cad,c,5));
c:= c + p;
end;
end;
Inc(c);
end;
end;


Ejemplo de llamada:

var
Cad: string;
begin
Cad:= 'XBTCD carUVUAactENTEReresÑÁUTÍaleatorios FGHJK continuaeltexto UITXL';
ListBox1.Items:= CincoMayusculas(Cad);
end;


Saludos.

BrunoBsso
19-04-2012, 18:06:54
Hola ZayDun. La solución que te traigo podrá no ser la mejor, pero está bastante modularizada y se entiende, así que la podés modificar para que se ajuste a lo que precisás.
No tengo un compilador acá en el trabajo así que no pude probarlo.
function OnlyNums(Str: String): Boolean;
const
NUMS = [#48 .. #57]; //'0' .. '9'
var
I: Integer;
begin
Result := True;
for I := 1 to Length(Str) do
begin
if not(Str[I] IN NUMS) then
begin
Result := False;
Exit;
end,
end;
end;

function OnlyCaps(Str: String): Boolean;
const
CAPS = [#65 .. #90]; //'A' .. 'Z'
var
I: Integer;
begin
Result := True;
for I := 1 to Length(Str) do
begin
if not(Str[I] IN Caps) then
begin
Result := False;
Exit;
end,
end;
end;


function OnlyNumsOrCaps(Str: String): Boolean;
begin
Result := OnlyNums(Str) or OnlyCaps(Str);
end;

procedure ParseStr(MyStr: String);
//MyStr := 'abc DEF ghi JKL MNÑ oPq RS1 2TUV xzc2 hn2d AJ7J kekgnwo'
var
SList: TStringList;
I: Integer;
begin
SList := TStringList.Create;
SList.Delimiter := #32;
SList.DelimitedText := MyStr;
for I := 0 to SList.Count-1 do
if (OnlyNumsOrCaps(SList[I]) and Length(SList[I]) = 5) then
ShowMessage(SList[I]);
end;

Saludos!!

BrunoBsso
19-04-2012, 19:36:47
Conseguí acceso a un compilador y vi la asquerosidad de código que escribí :(
En fin, acá tenés algo que te puede servir.
function OnlyNumsCaps(Str: String): Boolean;
const
VALID = [#48 .. #57, #65 .. #90, 'Ñ']; //'0' .. '9' + 'A' .. 'Z' + 'Ñ'
var
I: Integer;
begin
Result := True;
for I := 1 to Length(Str) do
begin
if not(Str[i] IN VALID) then
begin
Result := False;
Exit;
end;
end;
end;

procedure ParseStr(MyStr: String);
//MyStr := 'abc DE32F ghi JKL MNÑAS oPq RS1 2TFFU xzc2 hn2d AJwd7J kekgnwo'
var
SList: TStringList;
I: Integer;
begin
SList := TStringList.Create;
SList.DelimitedText := MyStr;
SList.Delimiter := ' ';
for I := 0 to SList.Count-1 do
if (OnlyNumsCaps(SList[i])) and (Length(SList[i]) = 5) then
ShowMessage(SList[i]);
SList.Free;
end;

Saludos.

ecfisa
20-04-2012, 22:17:28
Hola ZayDun (http://www.clubdelphi.com/foros/member.php?u=17300)

Por favor, recuerda leer el punto 13 (http://www.clubdelphi.com/foros/guiaestilo.php#concluye) de la guía de estilo.

Saludos y gracias por tu colaboración. :)

ZayDun
21-04-2012, 12:20:07
Perdonar que no haya escrito nada antes, tengo que decir que me he quedado con la solución que me dio Caro que funciona sin problemas, y agradeceros a todos la ayuda tan rapida que dais en cuanto tenemos algunda duda. un saludo!