Binary strings: Difference between revisions

Added Wren
(Added Wren)
Line 3,662:
 
End Module</lang>
 
=={{header|Wren}}==
In Wren, a string is simply an array of bytes. Although they are typically characters stored in UTF-8 format, they don't have to be interpreted in that way and, in recognition of this as well as for efficiency reasons, many of the built-in string functions operate at the byte level.
 
Despite this, there is no separate byte type - one just uses a single character string instead.
 
Strings are immutable and assignment creates a new copy rather than a reference to the original string.
 
All strings belong to the built-in String class which does not have an explicit constructor. To create one you either use a literal (a series of characters enclosed in double quotes) or assign an existing string.
 
When there are no longer any references to a string it is automatically garbage collected. You can't really destroy a string manually though you can request an immediate garbage collection be performed by calling the ''System.gc()'' method.
 
Setting a string variable to the special value ''null'' means that the variable currently has no value though it can still be assigned one later.
<lang ecmascript>// create string
var s = "abc"
 
// destroy string (not really see notes above)
s = null
 
// (re)assignment
s = "def"
 
// comparison (only == && != supported directly)
var b = (s == "abc") // false
 
// cloning/copying
var t = s
 
// check if empty
s = ""
b = (s != "") // false
b = s.isEmpty // true
 
// append a byte
s = s + "b"
 
// extract a substring
s = "ghijkl"
t = s[1..4] // "hijk"
 
// replace a byte or string
s = "abracadabra"
s = s.replace("a", "z") // "zbrzczdzbrz"
 
// join strings
s = "abc"
t = "def"
var u = s + t // "abcdef"</lang>
 
=={{header|zkl}}==
9,486

edits