var
btof: boolean;
const
allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
begin
btof:= false;
for i := 1 to length(sfilename) do
begin
if (sfilename[i] in [allowed[1] .. allowed[length(allowed)]])
then
btof := true;
end;
end;
I've been staring at this for the past hour trying to see the problem, but I just can't find it...
btof is always false, no matter the input.
3 Réponses :
La notation d'ensemble [allowed[1] .. allowed[length(allowed)] se résume à un ensemble de caractères ['a'..'9'] . Comme le littéral '9' a une valeur moindre que le littéral 'a' , l'ensemble résultant est juste vide.
Merci pour la clarification!
Ce n'est pas ainsi que vous vérifiez si un caractère est dans une chaîne. En fait, vous vérifiez si le caractère est entre «a» et «9». Ce n'est pas vraiment ce que vous avez l'intention de faire.
Voici une implémentation qui fonctionne:
// Check if a filename contain any illegal character
function CheckIllegal(const SFilename : String) : Boolean;
var
C : Char;
const
Allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
begin
Result := FALSE;
for C in SFilename do begin
if Pos(C, Allowed) < 1 then begin
Result := TRUE;
break;
end;
end;
end;
À propos, votre liste de caractères autorisés dans un nom de fichier est incomplète, du moins si vous pensez au système d'exploitation Windows.
Merci! Cela a aidé une tonne: D
Pos(C, Allowed) est un moyen très inefficace de le faire
var
bAllowed: boolean;
sfilename: string;
i, x: Integer;
C: Integer;
const
allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
begin
bAllowed := true;
sfilename := edtInput.Text;
i := Length(sfilename);
x := 1;
while ((x <= i) and (bAllowed)) do
begin
bAllowed := Pos(sfilename[x], allowed) > 0;
Inc(x);
end;
if bAllowed then
ShowMessage('The string is valid')
else
ShowMessage('The string isn''t valid');
This is a more optimized version of the code. I take it that the purpose of the code that you gave was to determine if the string is valid and return a bool. Thus it only has to check up and till the point that it finds an illegal char in the string.Note that I have changed it that if the string is valid it returns a true and invalid a false.
S'il s'agit d'un nom de fichier, ce code est erroné, car un nom de fichier vide doit également être invalide. (Et selon le système de fichiers cible, une longueur maximale s'applique également, ce qui n'est pas vérifié.)
Ce n'est pas ainsi que vous vérifiez si un caractère est dans une chaîne. En fait, vous vérifiez si le caractère est entre «a» et «9». Ce n'est pas vraiment ce que vous avez l'intention de faire.
Comme vous l'avez écrit, autorisé est une constante de chaîne. Pour en faire un ensemble, vous pouvez l'écrire comme "autorisé = ['a' .. 'z', 'A' .. 'Z', '0' .. '9];" Et puis vous pouvez simplement vérifier "si sfilename [i] est autorisé alors"
Si votre tâche consiste à vérifier le nom de fichier, vous pouvez également consulter
TPath.IsValidPathChar,TPath.IsValidFileNameChar, peut-êtreTPath.HasValidFileNameCharset / ou autres.