Literals/String: Difference between revisions
m
→{{header|Phix}}: added syntax colouring the hard way, phix/basics
Drkameleon (talk | contribs) |
m (→{{header|Phix}}: added syntax colouring the hard way, phix/basics) |
||
Line 1,851:
=={{header|Phix}}==
{{libheader|Phix/basics}}
single character literals (incidentally entirely equivalient to their ascii value) require single quotes, eg
<span style="color: #008080;">constant</span> <span style="color: #000000;">UPPERCASEJ</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'J'</span> <span style="color: #000080;font-style:italic;">-- equivalent to 74</span>
<!--</lang>-->
string literals use double quotes, eg
<!--<lang Phix>-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">hw</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Hello World!"</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">mt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #000080;font-style:italic;">-- empty string</span>
<!--</lang>-->
Note that 'z' and "z" are quite different. In Phix there is a strong difference between a character and a string.
Line 1,864 ⟶ 1,873:
Phix strings can also be used to hold "raw binary", ie instead of a sequence of characters, a sequence of any bytes in the range 0 to 255.<br>
Strings are fully mutable: you can append, prepend, replace, substitute, and crop characters and slices (/substrings) any way you like, eg
<!--<lang Phix>-->
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"food"</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'e'</span> <span style="color: #000080;font-style:italic;">-- s is now "feed" (replace all)</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"east"</span> <span style="color: #000080;font-style:italic;">-- s is now "feasted" (replace substring)</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">5</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span> <span style="color: #000080;font-style:italic;">-- s is now "fed"</span>
<!--</lang>-->
Special characters may be entered (between quotes) using a back-slash:
<pre>
Line 1,888 ⟶ 1,901:
Strings can also be entered by using triple quotes or backticks intead of double quotes to include linebreaks and avoid any backslash interpretation.
If the literal begins with a newline, it is discarded and any immediately following leading underscores specify a (maximum) trimming that should be applied to all subsequent lines. Examples:
▲<lang Phix>ts = """
this▼
string\thing"""▼
<!--<lang Phix>-->
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> ""<span style="color: #008000;">`
_____this▼
▲ this
string\thing"""▼
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> ""<span style="color: #008000;">`
▲ _____this
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">`this
string\thing`</span>▼
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"this\nstring\\thing"</span>
<!--</lang>-->
▲string\thing`
which are all equivalent.
Line 1,907 ⟶ 1,924:
Hex string literals are also supported (mainly for compatibility with OpenEuphoria, x/u/U for 1/2/4 byte codes), eg:
<!--<lang Phix>-->
<span style="color: #0000FF;">?</span><span style="color: #000000;">x</span><span style="color: #008000;">"68 65 6c 6c 6f"</span><span style="color: #0000FF;">;</span> <span style="color: #000080;font-style:italic;">-- displays "hello"</span>
<!--</lang>-->
=={{header|PHP}}==
|