Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Unwanted or Illegal Characters 1

Status
Not open for further replies.

tjcusick

Programmer
Dec 26, 2006
134
US
Ok I search around but didnt find anything that would help me.

What I have is this.

In our program we have a grouping tool that allows you to select any of the individual fields and display them on the screen.

What I am doing is creating the print button that creates the group in printed format.

The problem comes up when someone selects one of the Memo Fields. The Memo Field converts to a Text field and in place of line breaks there appears a box.

So i was trying to modify the below code. it works the way it is, but i would have to put every single character in.

Does anyone know a way to say don't allow the line return or new line characters? (I think they are #13 and #10)...

Thanks in advance.

Code:
  [b]function[/b] StripUnwantedChar(Text: string):string;
  [b]var[/b]
    Allowed: [b]Set[/b] [b]of[/b] Char;
    i, LeftOvers: Integer;
  [b]begin[/b]
    Allowed := [[teal]' '[/teal], [teal]'0'[/teal]..[teal]'9'[/teal], [teal]'a'[/teal]..[teal]'z'[/teal], [teal]'A'[/teal]..[teal]'Z'[/teal], [teal]'~'[/teal]..[teal]')'[/teal], [teal]'-'[/teal], [teal]'.'[/teal], [teal]'\'[/teal], [teal]':'[/teal], [teal]'`'[/teal], [teal]'/'[/teal], [teal]'<'[/teal], [teal]','[/teal], [teal]'>'[/teal], [teal]';'[/teal], [teal]'{'[/teal], [teal]'}'[/teal]];

    SetLength(Result, Length(Text));
    LeftOvers := [purple]1[/purple];
    [b]for[/b] i := [purple]1[/purple] [b]to[/b] Length(Text) [b]do[/b] [b]begin[/b]
      [b]if[/b] Text[i] [b]in[/b] Allowed [b]then[/b] [b]begin[/b]
        Result[LeftOvers]:= Text[i];
        Inc(LeftOvers);
      [b]end[/b]
    [b]end[/b];
    SetLength(Result, LeftOvers-[purple]1[/purple]);
  [b]end[/b];
 
delphi has a nice function called StringReplace

this piece of code will remove all newlines (#13#10 = CR LF):

Code:
interface 

uses SysUtils;

const CR = #13;
      LF = #10;
      CRLF=CR+LF;

implentation

function StripUnwantedText(Text : string): string;
begin
 Result:=StringReplace(Text, CRLF, '', [rfReplaceAll]);
end;

Life can be simple...

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Also note that The Functions Trim(), TrimRight() and TrimLeft() will remove All, Trailing and Leading edge control characters (and Spaces) from a string.


Steve [The sane]: Delphi a feersum engin indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top