String to String Formatting

Prev Next

Introduction

Some powerful formatting features are available for aligning and truncating strings, and embedding strings into given text patterns. For this, a pattern string, called a formatting template needs to be provided and is described in the following table.

Theis formatting template applies to following functions:

  • str(), 2nd parameter

Formatting Templates

Formatting Template Description Condition
# Fixed width place-holder for 1 character See 1
X Fixed width place-holder for 1 character, force upper case See 1
x Fixed width place-holder for 1 character, force lower case See 1
* 1st occurrence of this character is placeholder for all remaining characters from the original string which could not be fit into the available placeholders. Further *-characters are treated as given text. See 1
' ... ' Contents inside quotation marks are treated as given text, including spaces See 2
" ... " See above See 3
0, 1, ... 9 Numbers (both positive single and multi-digit numbers) specify repetition count for next character including placeholder symbols like #, X and x.. Lone 0 indicates next character to be suppressed. See 1
All other characters Treated as given text

1 Applicable outside quotation marks, otherwise symbols are treated as given text
2 Applicable outside double quotation marks, otherwise symbols are treated as given text
3 Applicable outside single quotation marks, otherwise symbols are treated as given text

Formatting Options

Formatting Option Description
align left Align contents to left if string is shorter than number of fixed placeholders.
(Default if no alignment is specified. If multiple alignemnts are specified, then the last one counts)
align middle Align contents to the middle
align right Align contents to right
truncate left Truncates characters at the left end if string does not fit into given fixed placeholders and an asterisk is not specified to insert the remaining characters.
(If multiple alignemnts are specified, then the last one counts)
truncate right Truncates characters at the right end for same reason above (Default if no truncation is specified)

If not formatting option is specified (e.g. no parameter provided, or empty set or '' (blank in single quotation marks), then the 'align left' and 'truncate right' are assumed.

Program Examples

      echo ( "(", str( "Dog","######", align left  ), ")");
      echo ( "(", str( "Dog","######", align middle ), ")" );
      echo ( "(", str( "Dog","######", {align middle, align right} ), ")" ); // Last align option applies
      echo ( str( "Hello World","Result: [####-####]" ) );
      echo ( str( "Hello World","Result: [4#-4#]" ) );
      echo ( str( "Hello World","Result: [####-####]", truncate left ) );
      echo ( str( "Hello World","[###XXXxxx]") );
      echo ( str( "News", "##########", {}, "+" ) ); // '' also allowed, but not ""
      echo ( str( "News", "##########", align right, "+" ) );
      echo ( str( "Daily Newspapers", "###### # # # #" ) );
      echo ( str( "Daily Newspapers", "###### # # # # *" ) );
      echo ( str( "Daily Newspapers", "###### # # # # ****" ) );
(Dog   )
( Dog  )
(   Dog)
Result: [Hell-o Wo]
Result: [Hell-o Wo]
Result: [lo W-orld]
[HelLO wor]
News++++++
++++++News
Daily  N e w s
Daily  N e w s papers
Daily  N e w s papers***
Try it yourself: Open LIB_Features_string_to_string_conversion_and_formatting.b4p in B4P_Examples.zip. Decompress before use.