Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

word/name fillter! help

Can anyone help me I am trying to make a word fillter that will only allow the chars that are in the replaceChars section.

[code]
const
InvalidChars = '@|!$%*"[]()^';
ReplaceChars = '-_';
[/code]
[code]
function TfrmMain.RemoveInvals(myString: String): String;
var
s: String;
begin
myString := Replace(myString, InvalidChars, ReplaceChars);
for n := 1 to length(myString) do
if ((Integer(myString[n]) >= 48) and (Integer(myString[n]) <= 57)) or ((Integer(myString[n]) >= 65) and (Integer(myString[n]) <= 90)) or ((Integer(myString[n]) >= 97) and (Integer(myString[n]) <= 122)) or (Integer(myString[n]) = 32) then
s := s + myString[n];
RemoveInvals := s;
end;
[/code]
[code]
function TfrmMain._l(const sText: String): String;
begin
Result := Replace(lowercase(trim(sText)), InvalidChars, ReplaceChars);
end;
[/code]
Slewis

Comments

  • MasterijnMasterijn Member Posts: 188
    [b][red]This message was edited by Masterijn at 2004-1-31 16:20:15[/red][/b][hr]
    [b][red]This message was edited by Masterijn at 2004-1-31 16:19:34[/red][/b][hr]
    [b][red]This message was edited by Masterijn at 2004-1-31 16:15:46[/red][/b][hr]
    : Can anyone help me I am trying to make a word fillter that will only allow the chars that are in the replaceChars section.
    :
    : [code]
    : const
    : InvalidChars = '@|!$%*"[]()^';function TfrmMain.RemoveInvals(myString: String): String;
    var
    s: String;
    begin
    myString := Replace(myString, InvalidChars, ReplaceChars);
    for n := 1 to length(myString) do
    if ((Integer(myString[n]) >= 48) and (Integer(myString[n]) <= 57)) or ((Integer(myString[n]) >= 65) and (Integer(myString[n]) <= 90)) or ((Integer(myString[n]) >= 97) and (Integer(myString[n]) <= 122)) or (Integer(myString[n]) = 32) then
    s := s + myString[n];
    RemoveInvals := s;
    end;
    [/code]

    : ReplaceChars = '-_';
    : [/code]
    : [code]
    : function TfrmMain.RemoveInvals(myString: String): String;
    : var
    : s: String;
    : begin
    : myString := Replace(myString, InvalidChars, ReplaceChars);
    : for n := 1 to length(myString) do
    : if ((Integer(myString[n]) >= 48) and (Integer(myString[n]) <= 57)) or ((Integer(myString[n]) >= 65) and (Integer(myString[n]) <= 90)) or ((Integer(myString[n]) >= 97) and (Integer(myString[n]) <= 122)) or (Integer(myString[n]) = 32) then
    : s := s + myString[n];
    : RemoveInvals := s;
    : end;
    : [/code]
    : [code]
    : function TfrmMain._l(const sText: String): String;
    : begin
    : Result := Replace(lowercase(trim(sText)), InvalidChars, ReplaceChars);
    : end;
    : [/code]
    : Slewis
    :
    Uhmm Slewis, don't mind me saying that Pascal/Delphi has a more elegant way of coding with characters, I shall quickly rewrite the above code:
    [code]
    function TfrmMain.RemoveInvals(Value: string): string;
    var
    Index: Integer;
    AChar: Char;
    begin
    Index := 0;
    SetLength(Result, Length(Value)); // This will be the maximum length of the string
    for I := 1 to Length(Value) do
    begin
    AChar := Value[I];

    if AChar in ['0'..'9', 'A'..'Z', 'a'..'z', ' '] then // Use char sets!
    begin
    Inc(Index); // Set the character at the index
    Result[Index] := AChar;
    end;
    end;
    SetLength(Result, Index); // String should be as long as the last char index
    end;
    [/code]
    Maybe this will help you to restate the problem. In my opinion neat coding, leads to neat thinking, which of course leads to problem solving.
    For performance reasons: try to avoid lines that reallocate the complete string like Astr:= Astr + AChar; In the above code this is
    reduced to only twice (SetLength(.., ..)).








Sign In or Register to comment.