Format: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
No edit summary
No edit summary
Line 14: Line 14:


<pre>
<pre>
Print Format("1{0}2","a")
guy = {first: "Eric", last: "Cartman"}
Print Format("My name is {0} and I live in {1}.", "Cartman", "South Park")
 
Print Format("hello {} and {}", "you", "bob")
Print Format("hello {0} And {1}", "you", "bob")
Print Format("hello {first} {last}",guy)
Print Format("hello {0} and {1} and {a}", "you", "bob", {a:"mary"})
Print Format("hello {0} and {1} and {a} and {2}", "you", "bob", "jill", {a:"mary"})
</pre>
</pre>


== Example (JavaScript) ==
== Example (JavaScript) ==
<pre>
<pre>
NSB.Print(Format("1{0}2","a"));
guy = {first: "Eric", last: "Cartman"};
NSB.Print(Format("My name is {0} and I live in {1}.", "Cartman", "South Park"));
 
NSB.Print("hello {} and {}".format("you", "bob"));
NSB.Print("hello {0} and {1}".format("you", "bob"));
NSB.Print("hello {first} {last}".format(guy));
NSB.Print("hello {0} and {1} and {a}".format("you", "bob", {a:"mary"}));
NSB.Print("hello {0} and {1} and {a} and {2}".format("you", "bob", "jill", {a:"mary"}));
</pre>
</pre>


== Output ==
== Output ==
<pre>
<pre>
1a2
hello you and bob
My name is Cartman and I live in South Park.
hello you and bob
hello Eric Cartman
hello you and bob and mary
hello you and bob and mary and jill
</pre>
</pre>



Revision as of 09:15, 22 December 2014

Format(string, replace0[,replace1...]) Format(string, {replace object})

Description

Replaces {n} placeholders with arguments. One or more arguments can be passed, in addition to the string itself, to insert into the string. Arguments can be three different style:

  1. {} Empty brackets. Placeholders are replaced with arguments in the same order as they appear in the function call.
  2. {1] Brackets with a number. Placeholders are replaced by the argument number as it appears in the function call.
  3. {name} Brackets with a name. Placeholders are replaced by the value of the name in the argument object.

Example (Basic)

guy = {first: "Eric", last: "Cartman"}

Print Format("hello {} and {}", "you", "bob")
Print Format("hello {0} And {1}", "you", "bob")
Print Format("hello {first} {last}",guy)
Print Format("hello {0} and {1} and {a}", "you", "bob", {a:"mary"})
Print Format("hello {0} and {1} and {a} and {2}", "you", "bob", "jill", {a:"mary"})

Example (JavaScript)

guy = {first: "Eric", last: "Cartman"};

NSB.Print("hello {} and {}".format("you", "bob"));
NSB.Print("hello {0} and {1}".format("you", "bob"));
NSB.Print("hello {first} {last}".format(guy));
NSB.Print("hello {0} and {1} and {a}".format("you", "bob", {a:"mary"}));
NSB.Print("hello {0} and {1} and {a} and {2}".format("you", "bob", "jill", {a:"mary"}));

Output

hello you and bob
hello you and bob
hello Eric Cartman
hello you and bob and mary
hello you and bob and mary and jill