Difference between revisions of "Manual:DIL Manual/textformat()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="bftextformat"></span>
+
= textformat() =
−
string textformat(s : string)
+
'''Function''' (string). Turns the escapes <code>\n</code> and <code>\r</code> in a text into real line breaks.
−
    s : The text string to format. The string is formatted according to
 
−
        the escape format codes as defined in one of the other documents.
 
  
−
    Returns: The formatted string which will automatically adapt to each
+
textformat(s : string)
−
            individual player's settings.
 
  
−
    Example:
+
Returns a copy of <code>s</code> in which each two-character sequence <code>\n</code> becomes a
−
      ts := note.extra.descr;
+
newline and each <code>\r</code> a carriage return. It is the same translation vmc makes in the
−
      rs := textformat(ts);
+
strings of a zone file, so it only matters for text that comes from elsewhere at run time: a
 +
file read with [[Manual:DIL Manual/loadstr()|loadstr()]], an extra, or what a player typed.
  
 +
== Parameters ==
 +
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| s || string || The text.
 +
|}
  
−
---~---~---~---~---~---~---~---~---
+
== Returns ==
−
<span id="bftextformat"></span>
+
String: the translated copy. A string variable that was never set fails.
−
string textformat(s : string)
 
−
    s : The text string to format. The string is formatted according to
 
−
        the escape format codes as defined in one of the other documents.
 
  
−
    Returns: The formatted string which will automatically adapt to each
+
== Notes ==
−
            individual player's settings.
+
* Nothing else changes. The <code>&amp;</code> codes of old DikuMUD versions (<code>&amp;n</code>,
 +
  <code>&amp;s</code> and the like) and the player's settings play no part any more; text in the
 +
  game is HTML, with <code>&lt;br/&gt;</code> for a new line.
 +
* Any other backslash pair is kept as it is, and a doubled backslash is no escape:
 +
  <code>\\n</code> still gives a backslash and a newline.
  
−
    Example:
+
== Example ==
−
      ts := note.extra.descr;
+
<pre>
−
      rs := textformat(ts);
+
dilbegin show_motd(pc : unitptr);
 +
var
 +
  err : integer;
 +
  motd : string;
 +
code
 +
{
 +
  err := loadstr("motd", motd);
 +
  if (err > 0)
 +
      sendtext(textformat(motd) + "<br/>", pc);
 +
  quit;
 +
}
 +
dilend
 +
</pre>
 +
Reads the message of the day from its file and shows it to a player, as the login in basis.zon
 +
does.
  
−
 
+
== See also ==
−
---~---~---~---~---~---~---~---~---
+
* [[Manual:DIL Manual/loadstr()|loadstr()]]
 +
* [[Manual:DIL Manual/sendtext()|sendtext()]]
 +
* [[Manual:DIL Manual/replace()|replace()]]

Latest revision as of 23:20, 14 November 2026

textformat()

Function (string). Turns the escapes \n and \r in a text into real line breaks.

textformat(s : string)

Returns a copy of s in which each two-character sequence \n becomes a newline and each \r a carriage return. It is the same translation vmc makes in the strings of a zone file, so it only matters for text that comes from elsewhere at run time: a file read with loadstr(), an extra, or what a player typed.

Parameters

Name Type Meaning
s string The text.

Returns

String: the translated copy. A string variable that was never set fails.

Notes

  • Nothing else changes. The & codes of old DikuMUD versions (&n,
 &s and the like) and the player's settings play no part any more; text in the
 game is HTML, with <br/> for a new line.
  • Any other backslash pair is kept as it is, and a doubled backslash is no escape:
 \\n still gives a backslash and a newline.

Example

dilbegin show_motd(pc : unitptr);
var
   err : integer;
   motd : string;
code
{
   err := loadstr("motd", motd);
   if (err > 0)
      sendtext(textformat(motd) + "<br/>", pc);
   quit;
}
dilend

Reads the message of the day from its file and shows it to a player, as the login in basis.zon does.

See also