Binary strings: Difference between revisions

no edit summary
m (Automated syntax highlighting fixup (second round - minor fixes))
No edit summary
Line 1,522:
Sleep
</syntaxhighlight>
 
 
 
=={{header|FutureBasic}}==
FB offers 255-character Pascal strings, and CFStrings — Apple's Core Foundation strings which are toll-free bridged with Objective-C's NSStrings.
Pascal strings are an array of bytes with the string length stored in byte 0.
CFStrings are immutable objects that encode a Unicode-compliant text string, represented as a sequence of UTF–16 code units. All lengths, character indexes, and ranges are expressed in terms of 16-bit platform-endian values, with index values starting at 0. The private contents of the string object are accessed via a pointer to its address. CFStrings are technically unlimited in length. While basic CFString manipulation is shown below, FB's CocoaUI offers a host of sophisticated accessor functions. Immutable CFStrings have a mutable sister, CFMutableStrings.
<syntaxhighlight lang="futurebasic"
// Pascal Strings (limited to 255 characters)
print "----------------------"
print "Pascal String Examples"
print "----------------------"
 
// Dimension strings and iterator
Str255 s, a
short i
 
// Create string
s = "Hello, world!"
 
// Get length of string using length byte at 0 index
print @"Length of \"Hello, world!\" is "; s[0]; @" characters."
 
// String destruction
s = ""
 
// String comparison
if s == "Hello, world!" then print "Strings are equal"
 
// Copying string
a = s
 
// Check If empty
if s == "" then print "String is empty"
 
// Append a byte
s = s + chr$(65)
 
// Extract a substring
a = mid$( s, 1, 5 ) // bytes 1 -> 5
 
// Substitute string "world" with "universe"
a = "Hello, world!"
for i = 1 to len$(a)
if ( mid$( a, i, 5 ) == "world" )
a = left$( a, i -1 ) + "universe" + mid$( a, i + 5 )
end if
next
print a
 
// Join strings
s = "See " + "you " + "later."
print s
print : print
 
// CFStrings (tecnhically unlimited length)
// Pascal Strings (limited to 255 characters)
print @"----------------------"
print @" CFString Examples"
print @"----------------------"
 
// Dimension strings and iterator
CFStringRef c, b
NSUInteger j
 
// Create CFString as pointer to Core Foundation object
c = @"Hello, world!"
 
// Get length of string
print @"Length of \"Hello, world!\" is "; len(c); @" characters."
 
// String destruction
c = @""
 
// String comparison
if fn StringIsEqual( c, @"Hello, world!" ) then print @"Strings are equal"
 
// Copying string
b = c
 
// Check if empty
if len(c) == 0 then print @"String is empty"
 
// Append a byte
c = fn StringWithString( @"A" )
 
// Extract a substring
b = mid( c, 1, 5 ) // bytes 1 -> 5
 
// Substitute string "world" with "universe"
b = @"Hello, world!"
for j = 0 to len(b) - 1
if ( fn StringIsEqual( mid( b, j, 6 ), @"world!" ) )
b = fn StringWithFormat( @"%@%@", left( b, j ), @"universe!" )
exit for
end if
next
print b
 
// Join strings
c = fn StringWithFormat( @"%@%@%@", @"See ", @"you ", @"later." )
print c
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
----------------------
Pascal String Examples
----------------------
Length of "Hello, world!" is 13 characters.
String is empty
Hello, universe!
See you later.
 
 
----------------------
CFString Examples
----------------------
Length of "Hello, world!" is 13 characters.
String is empty
Hello, universe!
See you later.
</pre>
 
 
 
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
715

edits