Question:
Some useful string manipulation functions
Answer:
If you’re like me, you create a bunch of utility functions and store them in a separate unit or maybe even a DLL that you can either compile into your program or access at runtime. Whatever the case, having some generic routines available at your disposal not only saves time, but ensures that the basic functions included in your programs are consistent.
For this discussion, I’ve included four useful string-manipulating routines that I regularly use in my programs. Table1 describes each function’s capabilities:
Function Name | Params | Description |
TrimRightChar | const S : String const ch : Char | Trims occurence of ch at end of string |
---|---|---|
TrimLeftChar | const S : String const ch : Char | Trims occurence of ch at start of string |
TrimAllChar | const S : String const ch : Char | Trims all occurences of ch from string |
FillCharAligned | str : String fillChar : Char wid : Integer LeftAlign : Boolean | Attaches ch to start or end of string depending upon LeftAlign value |
{Removes trailing spaces from a string} function TrimRightChar(const S: string; const ch : Char): string; var I: integer; begin I := Length(S); while (I > 0) AND (S[i] = ch) do Dec(i); Result := Copy(s, 1, i); end; {Removes the leading spaces from a string.} function TrimLeftChar(const S : string; const ch : Char): string; var I, len : Integer; begin len := Length(S); I := 1; while (I <= len) AND (S[I] = ch) do Inc(I); Result := Copy(S, I, (len + 1) - I); end; function TrimAllChar(const S : String; const ch : Char) : String; var buf : String; begin buf := S; Result := ''; {while Pos finds a blank} while (Pos(ch, buf) > 0) do begin {copy the substrings before the blank in to Result} Result := Result + Copy(buf, 1, Pos(ch, buf) – 1); buf := Copy(buf, Pos(ch, buf) + 1, Length(buf) – Pos(ch, buf)); end; {There will still be a remainder in buf, so copy remainder into Result} Result := Result + buf; end; {====================================================================================== Fills a string with specified character to specified width and alignment. The alignment is as follows: If you want ‘0’ to be the filler, inputting the following would get: True – ‘0000123’ False – ‘1230000’ ======================================================================================} function FillCharAligned(str : String; {String to format} fillChar : Char; {Char to fill with} wid : Integer; {Width of return string} LeftAlign : Boolean) {See above} : String; {Returns type} var Pos(ch, buf)Pos(ch, buf) I : Integer; begin Result := str; if ((wid – Length(str)) > 0) AND (str <> ”) then if LeftAlign then for I := 1 to (wid – Length(str)) do Result := (Result + fillChar) else for I := 1 to (wid – Length(str)) do Result := (fillChar + Result); end;
Notice that the above four functions deal specifically with a certain class of manipulation: removing from or adding characters to a string. The first three remove chars, while the last function adds chars. With respect to FillCharAligned, I’ve found it very useful when outputting to fixed-format ASCII files that require numeric fields be zero-filled with either leading or trailing zeroes. I’ve also used it to append blanks to the end or beginning of a string to get the right width for a string field.