String formatting: Difference between revisions

From Adventure Game Studio | Wiki
Jump to navigation Jump to search
*>SSH
No edit summary
 
No edit summary
Line 1: Line 1:
Here are more format flags:  
*Inserting strings: %s
Display("Your name is %s.", player.Name);


  //add leading zeros (up to 4) if necessary:
*Inserting letters: %c
  cJack.Say("The new code is %04d", GetGlobalInt(99));
  Display("%c is a letter.", mychar);
or
  Display("The first letter of the alphabet is the %c", 'A');


  // (number) = Create a field (number) characters wide:
*Inserting floats: %f
  Display("%f is a floating point number.", myfloat);
 
*Inserting integers: %d
Display("%d is a number.", myint);
 
===Integer format flags===
 
*Creating field (number) characters wide: %(number)d
  Display("Here comes %4d a test", 22); //=> Here comes  22 a test
  Display("Here comes %4d a test", 22); //=> Here comes  22 a test


// - = Left justify:
*Left justify: -
  Display("Here comes %-4d a test", 22); //=> Here comes 22  a test
  Display("Here comes %-4d a test", 22); //=> Here comes 22  a test


// 0 = Field is padded with 0's instead of blanks:
*Padding field with 0's instead of blanks: 0
  Display("Here comes %04d a test", 22); //=> Here comes 0022 a test
  Display("Here comes %04d a test", 22); //=> Here comes 0022 a test


// + = Sign of number always shown:
*Sign of number always shown: +
  Display("Here comes %+d a test", 22); //=> Here comes +22 a test
  Display("Here comes %+d a test", 22); //=> Here comes +22 a test


// (blank) = Positive values begin with a blank:
*Positive values begin with a blank: (blank)
  Display("Here comes % d a test", 22); //=> Here comes  22 a test
  Display("Here comes % d a test", 22); //=> Here comes  22 a test


Character to mask % symbol in strings: %
==Displaying special characters==


*Character to mask % symbol in strings: %
  Display("The %% is the percent symbol.");
  Display("The %% is the percent symbol.");


Character to mask quotemarks in strings: \  
*Character to mask quotemarks in strings: \  
 
  Display("The \" is a quotemark.");
  Display("The \" is a quotemark.");


Character for line breaks in strings: [  
*Character for line breaks in strings: [  
 
  Display("The [ is a line break.");
  Display("The [ is a line break.");


Character to mask [ in strings: \  
*Character to mask [ in strings: \  
 
  Display("The \[ is a left brace.");
  Display("The \[ is a left brace.");


[[Category:Tidbits and Snippets]]
[[Category:Tidbits and Snippets]]

Revision as of 06:09, 10 December 2005

  • Inserting strings: %s
Display("Your name is %s.", player.Name);
  • Inserting letters: %c
Display("%c is a letter.", mychar);

or

Display("The first letter of the alphabet is the %c", 'A');
  • Inserting floats: %f
Display("%f is a floating point number.", myfloat);
  • Inserting integers: %d
Display("%d is a number.", myint);

Integer format flags

  • Creating field (number) characters wide: %(number)d
Display("Here comes %4d a test", 22); //=> Here comes   22 a test
  • Left justify: -
Display("Here comes %-4d a test", 22); //=> Here comes 22   a test
  • Padding field with 0's instead of blanks: 0
Display("Here comes %04d a test", 22); //=> Here comes 0022 a test
  • Sign of number always shown: +
Display("Here comes %+d a test", 22); //=> Here comes +22 a test
  • Positive values begin with a blank: (blank)
Display("Here comes % d a test", 22); //=> Here comes  22 a test

Displaying special characters

  • Character to mask % symbol in strings: %
Display("The %% is the percent symbol.");
  • Character to mask quotemarks in strings: \
Display("The \" is a quotemark.");
  • Character for line breaks in strings: [
Display("The [ is a line break.");
  • Character to mask [ in strings: \
Display("The \[ is a left brace.");