Re: Delphi: Concatenating stringsPosted by webmaster Guido on November 15, 2002 at 03:39:03: In Reply to: Delphi: Concatenating strings posted by becky r. on November 13, 2002 at 19:43:17: : I have one string that I want to be a fixed length of 5 characters and another character string I want to be a fixed length of 10 numbers, so when I concatenate them the result string will be 15 characters. How can this be done? : Example : I want the output to be "a bcd ". For this purpose, you use a technique called "padding": add spaces to the right of each string until it has the desired length. In code: String1 := 'a'; String2 := 'bcd'; while Length(String1) < 5 do String1 := String1 + ' '; while Length(String2) < 10 do String2 := String2 + ' '; String3 := String1 + String2; // length of String3 will be 15 Or you can make your own PadRight-function: function PadRight(S: string; L: integer): string; begin Result := S; while Length(Result) < L do Result := Result + ' '; end; Later on in the program: ... String3 := PadRight(String1, 5) + PadRight(String2, 10); Or, if you prefer to use a procedure, passing the string "by reference" instead of "by value" (more efficient for longer strings): procedure PadRight(var S: string; L: integer); begin while Length(S) < L do S := S + ' '; end; Later on in the program: PadRight(String1, 5); PadRight(String2, 10); String3 := String1 + String2; |
|