Jump to content

Literals/String: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics
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
 
<lang Phix>constant UPPERCASEJ = 'J' -- equivalent to 74</lang>
<!--<lang Phix>ts = """-->
<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>constant hw = "Hello World!",
<!--<lang Phix>-->
mt = "" -- empty string</lang>
<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>string s = "food"
<!--<lang Phix>-->
s[2..3] = 'e' -- s is now "feed" (replace all)
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"food"</span>
s[2..2] = "east" -- s is now "feasted" (replace substring)
<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>
s[2..5] = "" -- s is now "fed"</lang>
<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>-->
ts = """
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> ""<span style="color: #008000;">`
_____this
this
string\thing"""
string\thing"`</span>""
<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
string\thing`</span>
<span style="color: #000000;">ts</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"this\nstring\\thing"</span>
<!--</lang>-->
 
ts = `this
string\thing`
 
ts = "this\nstring\\thing"</lang>
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>?x"68 65 6c 6c 6f"; -- displays "hello"</lang>
<!--<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}}==
7,813

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.