home *** CD-ROM | disk | FTP | other *** search
- Unit StrFunct;
- { A set of string functions placed in }
- { the public domain by: }
- { John Wood }
- { 14720 Karyl Drive }
- { Minnetonka, MN 55345 }
- { }
- { Please send any comments, suggestions }
- { or additions to my address. }
- { }
-
- Interface
- Uses Dos,Crt;
- {$V-}
- Type
- RowType = 1..25;
-
- Function Upper(S:String):String;
- Function Lower(S:String):String;
- Function Strip(S:String;Ch:Char):String;
- Function StripR(S:String;Ch:Char):String;
- Function Copies(Ch:Char;N:Integer):String;
- Procedure CtrStr(S:String;Row:RowType);
- Function Left(S:String;Width:Integer;Pad:Char):String;
- Function Right(S:String;Width:Integer;Pad:Char):String;
- Function Normalize(S:String):String;
- Function Words(S:String):Integer;
- Function FirstCap(S:String):String;
- Function OneCap(S:String):String;
- Function AllCap(S:String):String;
- Function Comma(Form:Char;Number:Real;Field:Integer;Dec:Integer):String;
-
- Implementation
-
-
- Function Upper(S:String):String;
- Var
- I : Integer;
- Begin
- For I:= 1 TO Length(S) do S[I]:= UpCase(S[I]);
- Upper:= S;
- End;
-
- Function Lower(S:String):String;
- Var
- I : Integer;
- Begin
- For I := 1 TO Length(S) do
- If S[I] In ['A'..'Z'] Then S[I]:= Chr(Ord(S[I])+$20);
- Lower:= S;
- End;
-
- Function Strip(S:String;Ch:Char):String;
- Var
- I,J : ShortInt;
- Begin
- I:= Length(S);
- J:= 1;
- While (S[I] = Ch) and (I > 0) do I:= I-1;
- While (S[J] = Ch) and (J < I) do J:= J+1;
- S:= Copy(S,J,I-J+1);
- S[0]:= Chr(I-J+1);
- Strip:= S;
- End;
-
- Function StripR(S:String;Ch:Char):String;
- Var
- I : ShortInt;
- Begin
- I:= Length(S);
- While (S[I] = Ch) and (I > 0) do I:= I-1;
- S[0]:= Chr(I);
- StripR:= S;
- End;
-
- Function Copies(Ch:Char;N:Integer):String;
- Var
- I : Integer;
- S : String;
- Begin
- S:='';
- FillChar(S[1],N,Ch);
- S[0]:= Chr(N);
- Copies:= S;
- End;
-
- Procedure CtrStr(S:String;Row:RowType);
- Var
- I : ShortInt;
- Begin
- I:= (80 - Length(S)) div 2;
- GotoXY(I,Row);Write(S);
- End;
-
- Function Left(S:String;Width:Integer;Pad:Char):String;
- Begin
- S:= Strip(S,#32);
- While Length(S) < Width do S:= S+Pad; {fill out str length}
- While Length(S) > Width do Delete(S,Length(S),1); {chop str down to size}
- Left:= S;
- End;
-
- Function Right(S:String;Width:Integer;Pad:Char):String;
- Begin
- S:= Strip(S,#32); {left justify the string}
- While Length(S) < Width do Insert(Pad,S,1); {pad str on the left}
- While Length(S) > Width do Delete(S,Length(S),1); {chop str down to size}
- Right:= S;
- End;
-
- Function Normalize(S:String):String;
- Var
- Len, I : Integer;
- Begin
- S:= Strip(S,#32); {left justify the string}
- If Length(S) > 1 Then
- Begin
- I:= 2;
- While I < Length(S) do
- Begin
- While (S[I]=#32) and (S[I+1]=#32) do Delete(S,I+1,1);
- Inc(I);
- End;
- End;
- Normalize:= S;
- End;
-
- Function Words(S:String):Integer;
- Var
- Len, I, J : Integer;
- Begin
- S:= Normalize(S);
- J:= 1;
- Len:= Length(S);
- If Len > 0 Then
- Begin
- For I:= 2 to Len do If S[I] = #32 Then Inc(J);
- End
- Else J:= 0;
- Words:= J;
- End;
-
- Function FirstCap(S:String):String;
- Var
- Str : String;
- LtrPos,I,NumWord : Integer;
- Begin
- S:= Normalize(S); {left justify & normalize the string}
- S:= Lower(S); { put entire string in lowercase }
- NumWord:= Words(S); { determine no. of words in string }
- S[1]:= UpCase(S[1]);
- LtrPos:= 2;
- For I:= 2 to NumWord do
- Begin
- While S[LtrPos] <> #32 do Inc(LtrPos);
- S[LtrPos+1]:= UpCase(S[LtrPos+1]);
- Inc(LtrPos);
- End;
- FirstCap:= S;
- End {FirstCap};
-
-
- Function OneCap(S:String):String;
- Begin
- S:= Normalize(S);
- S:= Lower(S);
- S[1]:= UpCase(S[1]);
- OneCap:= S;
- End;
-
- Function AllCap(S:String):String;
- Begin
- S:= Normalize(S);
- S:= Upper(S);
- AllCap:= S;
- End;
-
- Function Comma(Form:Char;Number:Real;Field:Integer;Dec:Integer):String;
- Var
- Hold : String;
- I : Integer;
- Begin
- Str (Number:Field:Dec, Hold);
- If Dec > 0 Then Dec := Dec + 1;
- For I := 1 to (Field - Dec - 3) do
- If ((Field - Dec - I) Mod 3 = 0) And (Hold[I] <> ' ')
- And (Hold[I] <> '-') Then
- Begin
- Delete (Hold,1,1); Insert (',', Hold, I);
- End;
- Case Form of
- '$' : Comma:= '$'+ Strip(Hold,' ');
- '#' : Comma:= Strip(Hold,' ');
- '%' : Comma:= Strip(Hold,' ')+' %';
- End;
- End;
-
-
- End{Unit}.
-