/* The code below places three repetitions of the string "DRAFT ONLY" in front of the title of an article, and puts two spaces between each repetition. This example code uses many of the character functions discussed in this chapter: framed_string ('My Big Book', 50, 'Draft', 'Draft', '-') ==> 'Draft-Draft-Draft-My Big Book-Draft-Draft-Draft' */ FUNCTION framed_string (string_in IN VARCHAR2, total_length_in IN INTEGER := 80, prefix_in IN VARCHAR2 := NULL, suffix_in IN VARCHAR2 := NULL, separator_in IN VARCHAR2 := ' ') RETURN VARCHAR2 IS len_string INTEGER := LENGTH (string_in); expanded_prefix VARCHAR2(100); expanded_suffix VARCHAR2(100); /*------------------- Local Module ---------------------*/ PROCEDURE strip_partial (full_string_inout IN OUT VARCHAR2, dup_string_in IN VARCHAR2) /* || Strip any fragment of the dup_string_in text from the || right end of the full string. For example, if: || full_string_inout = 'Draft-Draft-Dr' || dup_string_in = 'Draft-" || then this procedure returns the full string as: || 'Draft-Draft-' */ IS /* Length of the full string */ len_full_string INTEGER := LENGTH (full_string_inout); /* Length of the duplicated string component */ len_dup INTEGER := LENGTH (dup_string_in); /* Location of last full instance of dup_string */ last_full_dup INTEGER := INSTR (full_string_inout, dup_string_in, -1); BEGIN /* || If the location of last full instance of dup string || is more than len_dup characters away from end of string, || there is a fragment of that duplicated string. */ IF len_full_string - last_full_dup > len_dup - 1 THEN /* Return up to the last full duplication */ full_string_inout := SUBSTR (full_string_inout, 1, len_full_string - last_full_dup + len_dup); END IF; END; BEGIN /* || Duplicate the prefix and separator using LPAD to fill up || half remaining blank space (total length - len of string). */ expanded_prefix := RTRIM (LPAD (' ', ROUND ((total_length_in - len_string)/2), prefix_in || separator_in)); /* Call local module to strip off partial dups of prefix */ strip_partial (expanded_prefix, prefix_in || separator_in); /* || Now do the same for the suffix. Notice I place the || separator to the right of the suffix. */ expanded_suffix := LTRIM (RPAD (' ', ROUND ((total_length_in - len_string)/2), separator_in || NVL (suffix_in, prefix_in))); strip_partial (expanded_suffix, separator_in || NVL (suffix_in, prefix_in)); /* Combine and return the three parts of the string */ RETURN expanded_prefix || string_in || expanded_suffix; END;