Binary strings: Difference between revisions

125,304 bytes added ,  24 days ago
m
→‎{{header|FutureBasic}}: Fix typo "CFSString" -> "CFString"
(added factor example)
m (→‎{{header|FutureBasic}}: Fix typo "CFSString" -> "CFString")
 
(180 intermediate revisions by 84 users not shown)
Line 1:
{{task|String manipulation}}
 
Many languages have powerful and useful ('''binary safe''') [[wp:String (computer science)|string]] [[wp:Comparison of programming languages (string functions)|manipulation functions]], while others don't, making it harder for these languages to accomplish some tasks.
 
This task is about creating functions to handle ''binary'' strings (strings made of arbitrary bytes, i.e. ''byte strings'' according to Wikipedia) for those languages that don't have built-in support for them. If your language of choice does have this built-in support, show a possible alternative implementation for the ''functions'' or ''abilities'' already provided by the language.
This task is about creating functions to handle ''binary'' strings (strings made of arbitrary bytes, i.e. ''byte strings'' according to Wikipedia) for those languages that don't have built-in support for them.
 
If your language of choice does have this built-in support, show a possible alternative implementation for the ''functions'' or ''abilities'' already provided by the language.
 
In particular the functions you need to create are:
* String creation and destruction (when needed and if there's no [[garbage collection]] or similar mechanism)
Line 13 ⟶ 18:
* Join strings
 
<br>
Possible contexts of use: compression algorithms (like [[LZW compression]]), L-systems (manipulation of symbols), many more.
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">V x = Bytes(‘abc’)
print(x[0])</syntaxhighlight>
 
{{out}}
<pre>
97
</pre>
=={{header|8086 Assembly}}==
The 8086 has built-in support for handling byte and word strings, using <code>DS:SI</code> and <code>ES:DI</code> as the source and destination pointers, respectively. String functions can either auto-increment or auto-decrement the pointers held in these registers; the '''direction flag''' determines which one takes place. (<code>CLD</code> for auto-inc, <code>STD</code> for auto-dec.)
 
===Copying strings===
This is a "deep copy," i.e. after this you will have a duplicate of the string "Hello" in two separate memory locations, not just a pointer to the original stored elsewhere.
<syntaxhighlight lang="asm">;this code assumes that both DS and ES point to the correct segments.
cld
mov si,offset TestMessage
mov di,offset EmptyRam
mov cx,5 ;length of the source string, you'll need to either know this
;ahead of time or calculate it.
rep movsb
ret
 
;there is no buffer overflow protection built into these functions so be careful!
TestMessage byte "Hello"
EmptyRam byte 0,0,0,0,0</syntaxhighlight>
 
===Checking if a particular byte exists===
<syntaxhighlight lang="asm">;this code assumes that ES points to the correct segment.
cld
mov di,offset TestMessage
mov al,'o' ;the byte we wish to check
mov cx,5 ;len(Test)
 
repnz scasb ;scan through the string and stop if a match is found.
;flags will be set accordingly
ret
 
TestMessage byte "Hello"</syntaxhighlight>
 
===Compare two strings===
<syntaxhighlight lang="asm">;this code assumes that both DS and ES point to the correct segments.
cld
mov si,offset foo
mov di,offset bar
mov cx,4 ;length of the shorter of the two.
 
repz cmpsb
; this will continue until the strings are different or CX = 0,
; whichever occurs first. If, after this, the zero flag is set and CX=0,
; the strings were the same
ret
 
foo byte "test"
bar byte "test"</syntaxhighlight>
=={{header|Ada}}==
Ada has native support for single dimensioned arrays, which provide all specified operations. String is a case of array. The array of bytes is predefined in Ada in the package System.Storage_Elements ([http://www.adaic.org/standards/05rm/html/RM-13-7-1.html LRM 13.7.1]). Storage_Element is substitute for byte.
 
<langsyntaxhighlight Adalang="ada">declare
Data : Storage_Array (1..20); -- Data created
begin
Line 33 ⟶ 94:
... Data & (1,2,3,4) ... -- The result is Data with bytes 1,2,3,4 appended
... Data (3..5) ... -- The result the substring of Data from 3 to 5
end; -- Data destructed</langsyntaxhighlight>
Storage_Array is "binary string" used for memory representation. For stream-oriented I/O communication Ada provides alternative "binary string" called Stream_Element_Array ([http://www.adaic.org/standards/05rm/html/RM-13-13-1.html LRM 13.13.1]). When dealing with octets of bits, programmers are encouraged to provide a data type of their own to ensure that the byte is exactly 8 bits length. For example:
<langsyntaxhighlight Adalang="ada">type Octet is mod 2**8;
for Octet'Size use 8;
type Octet_String is array (Positive range <>) of Octet;</langsyntaxhighlight>
Alternatively:
<langsyntaxhighlight Adalang="ada">with Interfaces; use Interfaces;
...
type Octet is new Interfaces.Unsigned_8;
type Octet_String is array (Positive range <>) of Octet;</langsyntaxhighlight>
Note that all of these types will have all operations described above.
 
=={{header|ALGOL 68}}==
{{trans|Tcl}}
Line 51 ⟶ 111:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
<!-- {{does not work with|ELLA ALGOL 68|Any (with appropriate job cards AND formatted transput statements removed) - tested with release 1.8.8d.fc9.i386 - ELLA has no FORMATted transput}} -->
<langsyntaxhighlight lang="algol68"># String creation #
STRING a,b,c,d,e,f,g,h,i,j,l,r;
a := "hello world";
Line 143 ⟶ 203:
 
# Extract a CHAR from a CPU word #
print(("7th byte in CPU word is: ", offset ELEM word, new line))</langsyntaxhighlight>
Output:
<pre>
Line 165 ⟶ 225:
7th byte in CPU word is: w
</pre>
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">; creation
=={{header|C}}==
x: "this is a string"
'''estrings.h'''
y: "this is another string"
<lang c>#ifndef ESTRINGS_H_
z: "this is a string"
#define ESTRINGS_H_
; comparison
if x = z -> print "x is z"
; assignment
z: "now this is another string too"
; copying reference
y: z
 
; copying value
#include <string.h>
y: new z
 
; check if empty
if? empty? x -> print "empty"
else -> print "not empty"
 
; append a string
'x ++ "!"
 
print x
 
; substrings
print slice x 5 8
 
; join strings
z: x ++ y
print z
 
; replace occurrences of substring
print replace z "t" "T"</syntaxhighlight>
 
{{out}}
 
<pre>x is z
not empty
this is a string!
is a
this is a string!now this is another string too
This is a sTring!now This is anoTher sTring Too</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">#!/usr/bin/awk -f
 
BEGIN {
# string creation
a="123\0 abc ";
b="456\x09";
c="789";
printf("abc=<%s><%s><%s>\n",a,b,c);
 
# string comparison
printf("(a==b) is %i\n",a==b)
 
# string copying
A = a;
B = b;
C = c;
printf("ABC=<%s><%s><%s>\n",A,B,C);
 
# check if string is empty
if (length(a)==0) {
printf("string a is empty\n");
} else {
printf("string a is not empty\n");
}
 
# append a byte to a string
a=a"\x40";
printf("abc=<%s><%s><%s>\n",a,b,c);
 
# substring
e = substr(a,1,6);
printf("substr(a,1,6)=<%s>\n",e);
 
# join strings
d=a""b""c;
printf("d=<%s>\n",d);
}</syntaxhighlight>
 
Output:
<pre>abc=<123 abc ><456 ><789>
(a==b) is 0
ABC=<123 abc ><456 ><789>
string a is not empty
abc=<123 abc @><456 ><789>
substr(a,1,6)=<123 a>
d=<123 abc @456 789>
</pre>
=={{header|BASIC}}==
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoftbasic">REM STRING CREATION AND DESTRUCTION (WHEN NEEDED AND IF THERE'S NO GARBAGE COLLECTION OR SIMILAR MECHANISM)
A$ = "STRING" : REM CREATION
A$ = "" : REM DESTRUCTION
PRINT FRE(0) : REM GARBAGE COLLECTION
 
REM STRING ASSIGNMENT
A$ = "STRING" : R$ = "DEUX"
 
REM STRING COMPARISON
PRINT A$ = B$; A$ <> B$; A$ < B$; A$ > B$; A$ <= B$; A$ >= B$
 
REM STRING CLONING AND COPYING
B$ = A$
 
REM CHECK IF A STRING IS EMPTY
PRINT LEN(A$) = 0
 
REM APPEND A BYTE TO A STRING
A$ = A$ + CHR$(0)
 
REM EXTRACT A SUBSTRING FROM A STRING
S$ = MID$(A$, 2, 3)
 
REM REPLACE EVERY OCCURRENCE OF A BYTE (OR A STRING) IN A STRING WITH ANOTHER STRING
S = LEN(S$) : R = LEN(R$) : A = LEN(A$) : IF A > S THEN B$ = "" : FOR I = 1 TO A : F = MID$(A$, I, S) = S$ : B$ = B$ + MID$(R$, 1, R * F) + MID$(A$, I, F = 0) : NEXT I : A$ = B$ : PRINT A$
 
REM JOIN STRINGS
J$ = A$ + STR$(42) + " PUDDLES " + B$ + CHR$(255) : REM USE +</syntaxhighlight>
 
==={{header|GW-BASIC}}===
Also works in QBASIC, QuickBASIC, VB-DOS and PDS 7.1
<syntaxhighlight lang="qbasic">
10 ' SAVE"BINSTR", A
20 ' This program does string manipulation
30 A$ = "One value" ' String creation
40 A$ = "": PRINT FRE("") ' String destruction
50 A$ = "One value": B$ = "Other value" ' String assignment
60 PRINT A$ = B$; A$ <> B$; A$ < B$; A$ > B$; A$ <= B$; A$ >= B$' String comparison
70 B$ = A$ ' String cloning and copying
80 PRINT A$ = ""' Check if a string is empty
90 A$ = A$ + "!": PRINT A$' Append a byte to a string
100 PRINT MID$(A$, 5, 5); MID$(A$, 4, 1); LEFT$(A$, 3); RIGHT$(A$, 1)' Extract a substring from a string
110 B$ = "e": WHILE INSTR(A$, B$) > 0: MID$(A$, INSTR(A$, B$), 1) = "x": WEND: PRINT A$' Replace every ocurrence of a byte in a string with another string
120 C$ = A$ + " and " + STRING$(10, "-"): PRINT C$' Join strings
130 END</syntaxhighlight>
 
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 RANDOMIZE
110 REM create two strings
120 LET S$="Hello":LET T$="Bob"
130 REM choose any random character
140 LET C=(RND(127)+32)
150 REM add the character to the string
160 LET S$=S$&CHR$(C)
170 REM check if the string is empty
180 IF S$="" THEN PRINT "String is empty"
190 REM compare two strings
200 IF S$=T$ THEN PRINT "Strings are the same."
210 REM print characters 2 to 4 of a string (a substring)
220 PRINT S$(2:4)</syntaxhighlight>
 
==={{header|OxygenBasic}}===
<syntaxhighlight lang="text">
'STRING CREATION AND DESTRUCTION
string A
A = "STRING" 'CREATION
A = "" 'EMPTY
del A 'DESTRUCTION
'STRING ASSIGNMENT
string A = "STRING"
string R = "DEUX"
 
'STRING COMPARISON
print A==R : print A<>R ': print A<=R : print A>=R : print A<R : print A>R
'STRING CLONING AND COPYING
string B = A
'CHECK IF A STRING IS EMPTY
if not A then print "A is empty"
'APPEND A BYTE TO A STRING
A = A + chr(0)
A += chr(0)
'EXTRACT A SUBSTRING FROM A STRING
string S = mid(A, 2, 3)
'REPLACE EVERY OCCURRENCE OF A STRING) IN A STRING WITH ANOTHER STRING
int I=1
do
I=instr(I,S,"abc")
if not I
exit do
endif
S=left(S,I-1)+"defg"+mid(S,I+3)
I+=4
loop
'JOIN STRINGS
A="DUCKS "
string J = A str(42) " PUDDLES " R chr(255) 'CAN ALSO USE '+' OR '&' BTWEEN STRINGS
print J
</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
<syntaxhighlight lang="basic">10 REM create two strings
20 LET s$ = "Hello"
30 LET t$ = "Bob"
40 REM choose any random character
50 LET c = INT(RND*256)
60 REM add the character to the string
70 LET s$ = s$ + CHR$(c)
80 REM check if the string is empty
90 IF s$ = "" THEN PRINT "String is empty"
100 REM compare two strings
110 IF s$ = t$ THEN PRINT "Strings are the same"
120 REM print characters 2 to 4 of a string (a substring)
130 PRINT s$(2 TO 4)</syntaxhighlight>
=={{header|BBC BASIC}}==
<syntaxhighlight lang="bbcbasic"> A$ = CHR$(0) + CHR$(1) + CHR$(254) + CHR$(255) : REM assignment
B$ = A$ : REM clone / copy
IF A$ = B$ THEN PRINT "Strings are equal" : REM comparison
IF A$ = "" THEN PRINT "String is empty" : REM Check if empty
A$ += CHR$(128) : REM Append a byte
S$ = MID$(A$, S%, L%) : REM Extract a substring
C$ = A$ + B$ : REM Join strings
REM To replace every occurrence of a byte:
old$ = CHR$(1)
new$ = CHR$(5)
REPEAT
I% = INSTR(A$, old$)
IF I% MID$(A$, I%, 1) = new$
UNTIL I% = 0
</syntaxhighlight>
=={{header|BQN}}==
 
'''Based on:''' [[J]]
 
BQN characters are Unicode code points, so that it has no dedicated support for byte strings. However, strings of code points less than 256 are stored with one byte per character in CBQN, and used to represent byte strings by functions such as <code>•file.Bytes</code>.
 
* Example binary string creation
<syntaxhighlight lang="bqn"> name ← ""</syntaxhighlight>
 
* Example binary string deletion: effectively replaces the data with an integer, removing it from an accessible name.
<syntaxhighlight lang="bqn"> name ↩ 0</syntaxhighlight>
 
* Example binary string assignment
<syntaxhighlight lang="bqn"> name ← "value"</syntaxhighlight>
 
* Example binary string comparison
<syntaxhighlight lang="bqn"> name1 ≡ name2</syntaxhighlight>
 
* Example binary string cloning and copying
<syntaxhighlight lang="bqn"> name1 ← "example"
name2 ← name1</syntaxhighlight>
 
* Example check if a binary string is empty
<syntaxhighlight lang="bqn"> 0=≠string</syntaxhighlight>
 
* Example apppend a byte to a binary string
<syntaxhighlight lang="bqn"> string ← "example"
byte ← @
string ∾↩ byte</syntaxhighlight>
 
* Extract a substring from a binary string
<syntaxhighlight lang="bqn"> 3↓¯5↓"The quick brown fox runs..."</syntaxhighlight>
 
* Join strings
<syntaxhighlight lang="bqn"> "string1"∾"string2"</syntaxhighlight>
 
Note also: given an integer n, the corresponding byte value may be added to the null character <code>@</code> to get the character at that codepoint. This works due to BQN's character arithmetic.
<syntaxhighlight lang="bqn"> n + @</syntaxhighlight>
 
Thus, the binary string containing bytes with numeric values 1 0 255 can be obtained this way:
<syntaxhighlight lang="bqn">1‿0‿255 + @</syntaxhighlight>
=={{header|C}}==
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdboolstring.h>
 
typedef struct str_t {
size_t len, alloc;
unsigned char *s;
} bstr_t, *bstr;
 
#define BYTES_PER_BLOCKstr_len(s) 128((s)->len)
bstr str_new(size_t len)
struct StringStruct
{
bstr s = malloc(sizeof(bstr_t));
char *bstring;
if (len < 8) len = 8;
size_t length;
s->alloc = len;
size_t blocks;
s->s = malloc(len);
};
s->len = 0;
typedef struct StringStruct *String;
return s;
}
 
void str_extend(bstr s)
{
size_t ns = s->alloc * 2;
if (ns - s->alloc > 1024) ns = s->alloc + 1024;
s->s = realloc(s->s, ns);
s->alloc = ns;
}
 
void str_del(bstr s)
String newString();
{
String setString(String s, const char *p, size_t len);
free(s->s), free(s);
String appendChar(String s, char c);
}
int compareStrings(String s1, String s2);
void destroyString(String s);
String copyString(String to, String from);
String cloneString(String s);
bool stringIsEmpty(String s);
String subString(String s, size_t from, size_t to);
String replaceWith(String str, String ch, String repl);
String joinStrings(String s1, String s2);
#endif</lang>
 
int str_cmp(bstr l, bstr r)
'''estrings.c'''
{
<lang c>#include "estrings.h"
int res, len = l->len;
if (len > r->len) len = r->len;
 
if ((res = memcmp(l->s, r->s, len))) return res;
#include <stdio.h>
return l->len > r->len ? 1 : -1;
#define NOT_IMPLEMENTED_YET fprintf(stderr, "not implemented yet\n")</lang>
}
 
bstr str_dup(bstr src)
<lang c>String newString()
{
bstr x = str_new(src->len);
String t;
memcpy(x->s, src->s, src->len);
t = malloc(sizeof(struct StringStruct));
x->len = src->len;
if ( t == NULL ) return NULL;
return x;
t->length = 0;
}
t->blocks = 1;
t->bstring = malloc(BYTES_PER_BLOCK * t->blocks);
if ( t->bstring == NULL ) { free(t); return NULL; }
return t;
}</lang>
 
bstr str_from_chars(const char *t)
<lang c>static bool _fitString(String s, size_t len)
{
if (!t) return str_new(0);
s->blocks = len/BYTES_PER_BLOCK + 1;
size_t l = strlen(t);
s->bstring = realloc(s->bstring, s->blocks * BYTES_PER_BLOCK);
bstr x = str_new(l + 1);
if ( s->bstring != NULL ) return true;
x->len = l;
return false;
memcpy(x->s, t, l);
}</lang>
return x;
}
 
<langvoid c>String setStringstr_append(Stringbstr s, constunsigned char *p, size_t lenb)
{
if ( s->len !>= NULLs->alloc) str_extend(s);
s->s[s->len++] = b;
{
}
if ( p == NULL ) { s->length = 0; return s; }
_fitString(s, len);
if ( s->bstring != NULL )
{
memcpy(s->bstring, p, len);
s->length = len;
}
}
return s;
}</lang>
 
bstr str_substr(bstr s, int from, int to)
<lang c>String appendChar(String s, char c)
{
if (!to) sto == NULL ) return NULLs->len;
if (from < 0) from += s->len;
_fitString(s, s->length + 1);
if (from < 0 || from >= s->len)
s->length++;
return 0;
if ( s->bstring != NULL )
if (to < from) to = from + 1;
{
bstr x = str_new(to - from);
s->bstring[s->length-1] = c;
x->len = to - from;
}
memcpy(x->s, s->s + from, x->len);
return s;
return x;
}</lang>
}
 
bstr str_cat(bstr s, bstr s2)
<lang c>int compareStrings(String s1, String s2)
{
if while ( s1s->lengthalloc < s->len + s2->lengthlen) str_extend(s) return -1;
memcpy(s->s + if ( s1s->lengthlen, s2->s, s2->length len) return 1;
s->len += s2->len;
return memcmp(s1->bstring, s2->bstring, s1->length);
return s;
}</lang>
}
 
void str_swap(bstr a, bstr b)
<lang c>void destroyString(String s)
{
size_t tz;
if ( s != NULL )
unsigned char *ts;
{
tz = a->alloc; a->alloc = b->alloc; b->alloc = tz;
if ( s->bstring != NULL ) free(s->bstring);
tz = a->len; a->len = b->len; b->len = tz;
free(s);
ts = a->s; a->s = b->s; b->s = ts;
}
}
}</lang>
 
bstr str_subst(bstr tgt, bstr pat, bstr repl)
<lang c>String copyString(String to, String from)
{
bstr tmp = str_new(0);
if ( (to->bstring != NULL) && (from->bstring != NULL) )
int i;
{
for (i = 0; i + topat->blockslen <= fromtgt->blockslen;) {
if (memcmp(tgt->s + i, pat->s, pat->len)) {
to->length = from->length;
str_append(tmp, tgt->s[i]);
to->bstring = realloc(to->bstring, to->blocks * BYTES_PER_BLOCK);
i++;
memcpy(to->bstring, from->bstring, to->length);
} else {
}
str_cat(tmp, repl);
return to;
i += pat->len;
}</lang>
if (!pat->len) str_append(tmp, tgt->s[i++]);
}
}
while (i < tgt->len) str_append(tmp, tgt->s[i++]);
str_swap(tmp, tgt);
str_del(tmp);
return tgt;
}
 
void str_set(bstr dest, bstr src)
<lang c>String cloneString(String s)
{
while (dest->len < src->len) str_extend(dest);
String ps = malloc(sizeof(struct StringStruct));
memcpy(dest->s, src->s, src->len);
if ( ps != NULL )
dest->len = src->len;
{
}
ps->length = s->length;
ps->blocks = s->blocks;
ps->bstring = malloc(s->blocks * BYTES_PER_BLOCK);
if ( ps->bstring != NULL )
{
memcpy(ps->bstring, s->bstring, s->length);
} else {
free(ps); return NULL;
}
}
return ps;
}</lang>
 
int main()
<lang c>bool stringIsEmpty(String s)
{
bstr s = str_from_chars("aaaaHaaaaaFaaaaHa");
if ( s == NULL ) return true;
bstr s2 = str_from_chars("___.");
if ( s->length == 0 ) return true;
bstr s3 = str_from_chars("");
return false;
}</lang>
 
str_subst(s, s3, s2);
<lang c>String subString(String s, size_t from, size_t to)
printf("%.*s\n", s->len, s->s);
{
 
String ss;
str_del(s);
str_del(s2);
str_del(s3);
 
return 0;
}</syntaxhighlight>
=={{header|C sharp|C#}}==
{{works with|C sharp|3.0}}
 
<syntaxhighlight lang="csharp">using System;
if ( stringIsEmpty(s) || (to < from) || ( from >= s->length )) return newString();
if ( (from == 0) && (to >= (s->length - 1) ) ) return cloneString(s);
ss = newString();
if ( ss == NULL ) return NULL;
if ( _fitString(ss, to - from) ) {
ss->length = to - from;
memcpy(ss->bstring, s->bstring+from, ss->length);
}
return ss;
}</lang>
 
class Program
<lang c>String replaceWith(String str, String ch, String repl)
{
static void Main()
String d = NULL;
{
int occ = 0, i, j;
//string creation
var x = "hello world";
 
//# mark string for garbage collection
if ( stringIsEmpty(str) ) return NULL;
x = null;
if ( stringIsEmpty(ch) ) return cloneString(str);
 
if ( ch->length > 1 ) {
//# string assignment with a null byte
NOT_IMPLEMENTED_YET;
return str x = "ab\0";
Console.WriteLine(x);
}
Console.WriteLine(x.Length); // 3
for(i=0; i < str->length; i++) {
 
if ( str->bstring[i] == ch->bstring[0] ) occ++;
//# string comparison
}
if ( occ == 0 ) returnif cloneString(strx == "hello");
Console.WriteLine("equal");
d = newString();
else
if ( _fitString(d, str->length + occ * (repl->length - 1)) ) {
Console.WriteLine("not equal");
d->length = str->length + occ * (repl->length - 1);
 
for(i=0, j=0; i < str->length; i++) {
if (x.CompareTo("bc") str->bstring[i] !== ch->bstring[0] 1) {
Console.WriteLine("x is lexicographically less than 'bc'");
d->bstring[j] = str->bstring[i];
 
j++;
} else {//# string cloning
memcpy(d->bstringvar +c j,= repl->bstring,new repl->length)char[3];
jx.CopyTo(0, +=c, repl->length0, 3);
object objecty = new string(c);
}
var y = new string(c);
 
Console.WriteLine(x == y); //same as string.equals
Console.WriteLine(x.Equals(y)); //it overrides object.Equals
 
Console.WriteLine(x == objecty); //uses object.Equals, return false
 
//# check if empty
var empty = "";
string nullString = null;
var whitespace = " ";
if (nullString == null && empty == string.Empty &&
string.IsNullOrEmpty(nullString) && string.IsNullOrEmpty(empty) &&
string.IsNullOrWhiteSpace(nullString) && string.IsNullOrWhiteSpace(empty) &&
string.IsNullOrWhiteSpace(whitespace))
Console.WriteLine("Strings are null, empty or whitespace");
 
//# append a byte
x = "helloworld";
x += (char)83;
Console.WriteLine(x);
 
//# substring
var slice = x.Substring(5, 5);
Console.WriteLine(slice);
 
//# replace bytes
var greeting = x.Replace("worldS", "");
Console.WriteLine(greeting);
 
//# join strings
var join = greeting + " " + slice;
Console.WriteLine(join);
}
}</syntaxhighlight>
}
=={{header|C++}}==
return d;
<syntaxhighlight lang="cpp">#include <iomanip>
}</lang>
#include <iostream>
 
using namespace std;
<lang c>String joinStrings(String s1, String s2)
 
{
string replaceFirst(string &s, const string &target, const string &replace) {
String d;
auto pos = s.find(target);
if (pos stringIsEmpty(s1)== string::npos) return cloneString(s2)s;
return s.replace(pos, target.length(), replace);
if ( stringIsEmpty(s2) ) return cloneString(s1);
}
d = newString();
 
if ( _fitString(d, s1->length + s2->length) ) {
int main() {
memcpy(d->bstring, s1->bstring, s1->length);
 
memcpy(d->bstring + s1->length, s2->bstring, s2->length);
// string creation
d->length = s1->length + s2->length;
string x = "hello world";
}
 
return d;
// reassign string (no garbage collection)
}</lang>
x = "";
 
// string assignment with a null byte
x = "ab\0";
cout << x << '\n';
cout << x.length() << '\n';
 
// string comparison
if (x == "hello") {
cout << "equal\n";
} else {
cout << "not equal\n";
}
if (x < "bc") {
cout << "x is lexigraphically less than 'bc'\n";
}
 
// string cloning
auto y = x;
cout << boolalpha << (x == y) << '\n';
cout << boolalpha << (&x == &y) << '\n';
 
// check if empty
string empty = "";
if (empty.empty()) {
cout << "String is empty\n";
}
 
// append a byte
x = "helloworld";
x += (char)83;
cout << x << '\n';
 
// substring
auto slice = x.substr(5, 5);
cout << slice << '\n';
 
// replace bytes
auto greeting = replaceFirst(x, "worldS", "");
cout << greeting << '\n';
 
// join strings
<lang c>#undef NOT_IMPLEMENTED_YET</lang>
auto join = greeting + ' ' + slice;
cout << join << '\n';
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>ab
2
not equal
x is lexigraphically less than 'bc'
true
false
String is empty
helloworldS
world
hello
hello world</pre>
=={{header|Common Lisp}}==
String creation (garbage collection will handle its destruction)
using the string as an atom and casting a character list to a string
<langsyntaxhighlight lang="lisp">
"string"
(coerce '(#\s #\t #\r #\i #\n #\g) 'string)
</syntaxhighlight>
</lang>
 
String assignment
<langsyntaxhighlight lang="lisp">
(defvar *string* "string")
</syntaxhighlight>
</lang>
 
comparing two string
<langsyntaxhighlight lang="lisp">
(equal "string" "string")
</syntaxhighlight>
</lang>
 
copy a string
<langsyntaxhighlight lang="lisp">
(copy-seq "string")
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang="lisp">
(defun string-empty-p (string)
(zerop (length string)))</syntaxhighlight>
(cond
((= 0 (length string))t)
(nil)))
</lang>
 
<langsyntaxhighlight lang="lisp">
(concatenate 'string "string" "b")
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang="lisp">
(subseq "string" 12 6)
"ring"
</syntaxhighlight>
</lang>
 
string replacement isn't covered by the ansi standard probably best to use (replace-all) or cl-ppcre
Line 410 ⟶ 825:
 
joining strings works in the same way as appending bytes
=={{header|Component Pascal}}==
BlackBox Component Builder
<syntaxhighlight lang="oberon2">
MODULE NpctBinaryString;
IMPORT StdLog,Strings;
 
PROCEDURE Do*;
VAR
str: ARRAY 256 OF CHAR;
pStr,pAux: POINTER TO ARRAY OF CHAR;
b: BYTE;
pIni: INTEGER;
BEGIN
(* String creation, on heap *)
NEW(pStr,256); (* Garbage collectable *)
NEW(pAux,256);
(* String assingment *)
pStr^ := "This is a string on a heap";
pAux^ := "This is a string on a heap";
str := "This is other string";
(* String comparision *)
StdLog.String("pStr = str:> ");StdLog.Bool(pStr$ = str$);StdLog.Ln;
StdLog.String("pStr = pAux:> ");StdLog.Bool(pStr$ = pAux$);StdLog.Ln;
(* String cloning and copying *)
NEW(pAux,LEN(pStr$) + 1);pAux^ := pStr$;
(* Check if a string is empty *)
(* version 1 *)
pAux^ := "";
StdLog.String("is empty pAux?(1):> ");StdLog.Bool(pAux$ = "");StdLog.Ln;
(* version 2 *)
pAux[0] := 0X;
StdLog.String("is empty pAux?(2):> ");StdLog.Bool(pAux$ = "");StdLog.Ln;
(* version 3 *)
pAux[0] := 0X;
StdLog.String("is empty pAux?(3):> ");StdLog.Bool(pAux[0] = 0X);StdLog.Ln;
(* version 4 *)
pAux^ := "";
StdLog.String("is empty pAux?(4):> ");StdLog.Bool(pAux[0] = 0X);StdLog.Ln;
(* Append a byte to a string *)
NEW(pAux,256);pAux^ := "BBBBBBBBBBBBBBBBBBBBB";
b := 65;pAux[LEN(pAux$)] := CHR(b);
StdLog.String("pAux:> ");StdLog.String(pAux);StdLog.Ln;
(* Extract a substring from a string *)
Strings.Extract(pStr,0,16,pAux);
StdLog.String("pAux:> ");StdLog.String(pAux);StdLog.Ln;
(* Replace a every ocurrence of a string with another string *)
pAux^ := "a"; (* Pattern *)
Strings.Find(pStr,pAux,0,pIni);
WHILE pIni > 0 DO
Strings.Replace(pStr,pIni,LEN(pAux$),"one");
Strings.Find(pStr,pAux,pIni + 1,pIni);
END;
StdLog.String("pStr:> ");StdLog.String(pStr);StdLog.Ln;
(* Join strings *)
pStr^ := "First string";pAux^ := "Second String";
str := pStr$ + "." + pAux$;
StdLog.String("pStr + '.' + pAux:>");StdLog.String(str);StdLog.Ln
END Do;
END NpctBinaryString.
</syntaxhighlight>
Execute: ^Q NpctBinaryString.Do<br/>
Output:
<pre>
pStr = str:> $FALSE
pStr = pAux:> $TRUE
is empty pAux?(1):> $TRUE
is empty pAux?(2):> $TRUE
is empty pAux?(3):> $TRUE
is empty pAux?(4):> $TRUE
pAux:> BBBBBBBBBBBBBBBBBBBBBA
pAux:> This is a string
pStr:> This is one string on one heonep
pStr + '.' + pAux:>First string.Second String
</pre>
=={{header|D}}==
<syntaxhighlight lang="d">void main() /*@safe*/ {
String creation (destruction is handled by the garbage collector)
import std.array: empty, replace;
<lang d>byte[]str;</lang>
import std.string: representation, assumeUTF;
 
// String creation (destruction is usually handled by
String assignment
// the garbage collector).
<lang d>byte[]str = cast(byte[])"blah";</lang>
ubyte[] str1;
 
// String comparisonassignments.
str1 = "blah".dup.representation;
<lang d>byte[]str1;
// Hex string, same as "\x00\xFB\xCD\x32\xFD\x0A"
byte[]str2;
// whitespace and newlines are ignored.
if (str1 == str2) // strings equal</lang>
str1 = cast(ubyte[])x"00 FBCD 32FD 0A";
 
// String comparison.
String cloning and copying
ubyte[] str2;
<lang d>byte[]str;
if (str1 == str2) {} // Strings equal.
byte[]str2 = str.dup; // copy entire array</lang>
 
// String cloning and copying.
Check if a string is empty
str2 = str1.dup; // Copy entire string or array.
<lang d>byte[]str = cast(byte[])"blah";
if (str.length) // string not empty
if (!str.length) // string empty</lang>
 
Append a byte to // Check if a string is empty
if (str1.empty) {} // String empty.
<lang d>byte[]str;
if (str1.length) {} // String not empty.
str ~= 'a';</lang>
if (!str1.length) {} // String empty.
 
Extract // Append a substringubyte fromto a string.
str1 ~= x"0A";
<lang d>byte[]str = "blork";
str1 ~= 'a';
byte[]substr = str[1..$-1]; // this takes off the first and last bytes and assigns them to the new byte string</lang>
 
Replace every occurrence of a// byte (orExtract a string)substring infrom a string with another string.
str1 = "blork".dup.representation;
<lang d>byte[]str = cast(byte)"blah";
// This takes off the first and last bytes and
replace(cast(char[])str,"la","al");</lang>
// assigns them to the new ubyte string.
// This is just a light slice, no string data copied.
ubyte[] substr = str1[1 .. $ - 1];
 
// Replace every occurrence of a ubyte (or a string)
Join strings
// in a string with another string.
<lang d>byte[]b1;
str1 = "blah".dup.representation;
byte[]b2;
replace(str1.assumeUTF, "la", "al");
byte[]b3 = b1~b2;</lang>
 
// Join two strings.
ubyte[] str3 = str1 ~ str2;
}</syntaxhighlight>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Binary_strings;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
var
x : string;
c : TArray<Byte>;
objecty,
y : string;
empty : string;
nullString : string;
whitespace,
slice,
greeting,
join : string;
begin
//string creation
x:= String.create(['1','2','3']);
x:= String.create('*',8);
x := 'hello world';
 
//# string assignment with a hex byte
x := 'ab'#10;
writeln(x);
writeln(x.Length); // 3
 
//# string comparison
if x = 'hello' then
writeln('equal')
else
writeln('not equal');
if x.CompareTo('bc') = -1 then
writeln('x is lexicographically less than "bc"');
 
//# string cloning
y := x; // string is not object is delphi (are imutables)
writeln(x = y); //same as string.equals
writeln(x.Equals(y)); //it overrides object.Equals
 
//# check if empty
// Strings can't be null (nil), just Pchar can be
// IsNullOrEmpty and IsNullOrWhiteSpace, check only for
// Empty and Whitespace respectively.
empty := '';
whitespace := ' ';
if (empty = string.Empty) and
string.IsNullOrEmpty(empty) and
string.IsNullOrWhiteSpace(empty) and
string.IsNullOrWhiteSpace(whitespace) then
writeln('Strings are empty or whitespace');
 
//# append a byte
x := 'helloworld';
x := x + Chr(83);
// x := x + #83; // the same of above line
writeln(x);
 
//# substring
slice := x.Substring(5, 5);
writeln(slice);
 
//# replace bytes
greeting := x.Replace('worldS', '');
writeln(greeting);
 
//# join strings
join := greeting + ' ' + slice;
writeln(join);
 
Readln;
end.</syntaxhighlight>
{{out}}
<pre>ab
 
3
not equal
x is lexicographically less than "bc"
TRUE
TRUE
Strings are empty or whitespace
helloworldS
world
hello
hello world</pre>
=={{header|Déjà Vu}}==
 
Déjà Vu has a <code>blob</code> type, which is much like Python 3's <code>bytearray</code>. They are used for dealing with binary data in the standard library, and works basically like a list, except it can only have integer numbers from 0 to 255 as elements, pushing and popping is not supported, and can be resized to any size in a single step.
 
<syntaxhighlight lang="dejavu">local :b make-blob 10 #ten bytes of initial size
set-to b 0 255
!. get-from b 0 #prints 255
!. b #prints (blob:ff000000000000000000)
local :b2 make-blob 3
set-to b2 0 97
set-to b2 1 98
set-to b2 2 99
!. b #prints (blob:"abc")
!. !encode!utf-8 b #prints "abc"
</syntaxhighlight>
=={{header|E}}==
 
Line 457 ⟶ 1,067:
To work with binary strings we must first have a byte type; this is a place where E shows its Java roots (to be fixed).
 
<langsyntaxhighlight lang="e">? def int8 := <type:java.lang.Byte>
# value: int8</langsyntaxhighlight>
 
<ol>
<li>There are several ways to create a FlexList; perhaps the simplest is:
<langsyntaxhighlight lang="e">? def bstr := [].diverge(int8)
# value: [].diverge()
 
Line 469 ⟶ 1,079:
 
? def bstr2 := [-0x7F,0x2,0x3].diverge(int8)
# value: [-127, 2, 3].diverge()</langsyntaxhighlight>
As E is a memory-safe garbage-collected language there is no explicit destruction. It is good practice to work with immutable ConstLists when reasonable, however; especially when passing strings around.
</li><li>There is no specific assignment between FlexLists; a reference may be passed in the usual manner, or the contents of one could be copied to another as shown below.
</li><li>There is no comparison operation between FlexLists (since it would not be a stable ordering <!-- XXX cite? -->), but there is between ConstLists.
<langsyntaxhighlight lang="e">? bstr1.snapshot() < bstr2.snapshot()
# value: false</langsyntaxhighlight>
</li><li>To make an independent copy of a FlexList, simply <code>.diverge()</code> it again.
</li><langli><syntaxhighlight lang="e">? bstr1.size().isZero()
# value: false
 
? bstr.size().isZero()
# value: true</langsyntaxhighlight>
</li><li>Appending a single element to a FlexList is done by <code>.push(<var>x</var>)</code>:
<langsyntaxhighlight lang="e">? bstr.push(0)
? bstr
# value: [0].diverge()</langsyntaxhighlight>
</li><li>Substrings, or ''runs'', are always immutable and specified as start-end indexes (as opposed to first-last or start-count). Or, one can copy an arbitrary portion of one list into another using <code>replace(<var>target range</var>, <var>source list</var>, <var>source range</var>)</code>.
<langsyntaxhighlight lang="e">? bstr1(1, 2)
# value: [2]
 
? bstr.replace(0, bstr.size(), bstr2, 1, 3)
? bstr
# value: [2, 3].diverge()</langsyntaxhighlight>
</li><li>Replacing must be written as an explicit loop; there is no built-in operation (though there is for character strings).
<langsyntaxhighlight lang="e">? for i => byte ? (byte == 2) in bstr2 { bstr2[i] := -1 }
? bstr2
# value: [-127, -1, 3].diverge()</langsyntaxhighlight>
</li><li>Two lists can be concatenated into a ConstList by <code>+</code>: <code>bstr1 + bstr2</code>. <code>append</code> appends on the end of a FlexList, and <code>replace</code> can be used to insert at the beginning or anywhere inside.
<langsyntaxhighlight lang="e">? bstr1.append(bstr2)
? bstr1
# value: [1, 2, 3, -127, 2, 3].diverge()</langsyntaxhighlight>
</li></ol>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module BinaryStrings {
@Inject Console console;
void run() {
Byte[] mutableBytes = new Byte[]; // growable and mutable string of bytes
Byte[] fixedLength = new Byte[10]; // fixed length string of bytes (all default to 0)
Byte[] literal = [0, 1, 7, 0xff]; // a "constant" string of bytes
console.print($|String creation and assignment:
| mutableBytes={mutableBytes}
| fixedLength={fixedLength}
| literal={literal}
|
);
 
console.print($|Check if a string is empty:
| mutableBytes.empty={mutableBytes.empty}
| fixedLength.empty={fixedLength.empty}
| literal.empty={literal.empty}
|
);
 
mutableBytes += 0; // add a byte (using an operator)
mutableBytes.add(1); // add a byte (using the underlying method)
mutableBytes.addAll(#07FF); // add multiple bytes (using the underlying method)
console.print($|Append a byte to a string:
| mutableBytes={mutableBytes}
|
);
 
console.print($|String comparison:
| mutableBytes==literal = {mutableBytes==literal}
| fixedLength==literal = {fixedLength==literal}
|
);
 
fixedLength = new Byte[4](i -> literal[i]); // create/copy from literal to fixedLength
val clone = fixedLength.duplicate(); // clone the array
console.print($|String cloning and copying:
| fixedLength={fixedLength}
| clone={clone}
|
);
 
console.print($|Extract a substring from a string:
| mutableBytes[1..2]={mutableBytes[1..2]}
| fixedLength[0..2]={fixedLength[0..2]}
| literal[2..3]={literal[2..3]}
|
);
 
for (Int start = 0; Int index := fixedLength.indexOf(0x01, start); start = index) {
fixedLength[index] = 0x04;
}
console.print($|Replace every occurrence of a byte in a string with another string:
| fixedLength={fixedLength}
|
);
 
for (Int start = 0; Int index := mutableBytes.indexOf(#0107, start); start = index) {
mutableBytes.replaceAll(index, #9876);
}
console.print($|Replace every occurrence of a string in a string with another string:
| mutableBytes={mutableBytes}
|
);
 
console.print($|Join strings:
| mutableBytes+fixedLength+literal={mutableBytes+fixedLength+literal}
|
);
}
}
</syntaxhighlight>
 
{{out}}
<pre>
String creation and assignment:
mutableBytes=0x
fixedLength=0x00000000000000000000
literal=0x000107FF
 
Check if a string is empty:
mutableBytes.empty=True
fixedLength.empty=False
literal.empty=False
 
Append a byte to a string:
mutableBytes=0x000107FF
 
String comparison:
mutableBytes==literal = True
fixedLength==literal = False
 
String cloning and copying:
fixedLength=0x000107FF
clone=0x000107FF
 
Extract a substring from a string:
mutableBytes[1..2]=0x0107
fixedLength[0..2]=0x000107
literal[2..3]=0x07FF
 
Replace every occurrence of a byte in a string with another string:
fixedLength=0x000407FF
 
Replace every occurrence of a string in a string with another string:
mutableBytes=0x009876FF
 
Join strings:
mutableBytes+fixedLength+literal=0x009876FF000407FF000107FF
</pre>
 
=={{header|Elixir}}==
Note: Elixir data types are immutable.
<syntaxhighlight lang="elixir"># String creation
x = "hello world"
 
# String destruction
x = nil
 
# String assignment with a null byte
x = "a\0b"
IO.inspect x #=> <<97, 0, 98>>
IO.puts String.length(x) #=> 3
 
# string comparison
if x == "hello" do
IO.puts "equal"
else
IO.puts "not equal" #=> not equal
end
y = "bc"
if x < y do
IO.puts "#{x} is lexicographically less than #{y}" #=> a b is lexicographically less than bc
end
 
# string cloning
xx = x
IO.puts x == xx #=> true (same length and content)
 
# check if empty
if x=="" do
IO.puts "is empty"
end
if String.length(x)==0 do
IO.puts "is empty"
end
 
# append a byte
IO.puts x <> "\07" #=> a b 7
IO.inspect x <> "\07" #=> <<97, 0, 98, 0, 55>>
 
# substring
IO.puts String.slice("elixir", 1..3) #=> lix
IO.puts String.slice("elixir", 2, 3) #=> ixi
 
# replace bytes
IO.puts String.replace("a,b,c", ",", "-") #=> a-b-c
 
# string interpolation
a = "abc"
n = 100
IO.puts "#{a} : #{n}" #=> abc : 100
 
# join strings
a = "hel"
b = "lo w"
c = "orld"
IO.puts a <> b <> c #=> hello world</syntaxhighlight>
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">-module(binary_string).
-compile([export_all]).
 
%% Erlang has very easy handling of binary strings. Using
%% binary/bitstring syntax the various task features will be
%% demonstrated.
 
 
%% Erlang has GC so destruction is not shown.
test() ->
Binary = <<0,1,1,2,3,5,8,13>>, % binaries can be created directly
io:format("Creation: ~p~n",[Binary]),
Copy = binary:copy(Binary), % They can also be copied
io:format("Copy: ~p~n",[Copy]),
Compared = Binary =:= Copy, % They can be compared directly
io:format("Equal: ~p = ~p ? ~p~n",[Binary,Copy,Compared]),
Empty1 = size(Binary) =:= 0, % The empty binary would have size 0
io:format("Empty: ~p ? ~p~n",[Binary,Empty1]),
Empty2 = size(<<>>) =:= 0, % The empty binary would have size 0
io:format("Empty: ~p ? ~p~n",[<<>>,Empty2]),
Substring = binary:part(Binary,3,3),
io:format("Substring: ~p [~b..~b] => ~p~n",[Binary,3,5,Substring]),
Replace = binary:replace(Binary,[<<1>>],<<42>>,[global]),
io:format("Replacement: ~p~n",[Replace]),
Append = <<Binary/binary,21>>,
io:format("Append: ~p~n",[Append]),
Join = <<Binary/binary,<<21,34,55>>/binary>>,
io:format("Join: ~p~n",[Join]).
 
%% Since the task also asks that we show how these can be reproduced
%% rather than just using BIFs, the following are some example
%% recursive functions reimplementing some of the above.
 
%% Empty string
is_empty(<<>>) ->
true;
is_empty(_) ->
false.
 
%% Replacement:
replace(Binary,Value,Replacement) ->
replace(Binary,Value,Replacement,<<>>).
 
replace(<<>>,_,_,Acc) ->
Acc;
replace(<<Value,Rest/binary>>,Value,Replacement,Acc) ->
replace(Rest,Value,Replacement,<< Acc/binary, Replacement >>);
replace(<<Keep,Rest/binary>>,Value,Replacement,Acc) ->
replace(Rest,Value,Replacement,<< Acc/binary, Keep >>).</syntaxhighlight>
{{out}}
<syntaxhighlight lang="erlang">215> binary_string:test().
Creation: <<0,1,1,2,3,5,8,13>>
Copy: <<0,1,1,2,3,5,8,13>>
Equal: <<0,1,1,2,3,5,8,13>> = <<0,1,1,2,3,5,8,13>> ? true
Empty: <<0,1,1,2,3,5,8,13>> ? false
Empty: <<>> ? true
Substring: <<0,1,1,2,3,5,8,13>> [3..5] => <<2,3,5>>
Replacement: <<0,42,42,2,3,5,8,13>>
Append: <<0,1,1,2,3,5,8,13,21>>
Join: <<0,1,1,2,3,5,8,13,21,34,55>></syntaxhighlight>
=={{header|Factor}}==
Factor has a <code>byte-array</code> type which works exactly like other arrays, except only bytes can be stored in it. Comparisons on <code>byte-array</code>s (like comparisons on arrays) are lexicographic.
 
To convert a string to a byte-array:
<langsyntaxhighlight lang="factor">"Hello, byte-array!" utf8 encode .</langsyntaxhighlight>
<pre>
B{
Line 513 ⟶ 1,354:
</pre>
Reverse:
<langsyntaxhighlight lang="factor">B{ 147 250 150 123 } shift-jis decode .</langsyntaxhighlight>
<pre>"日本"</pre>
=={{header|Forth}}==
In Forth, as in Assembler, all strings are binary ie: they are simply bytes in memory. <br>
Using primitive memory operations the programmer can quickly build a string word set.
This code is an example of how to create string functions from low-level operations.
<br>
Using an Indirect Threaded Forth, this code compiles to only 304 bytes on a 16 bit controller! (with labels stripped out)
Adding the 256 byte buffer it takes only 560 bytes; useable in small embedded environments.
 
<syntaxhighlight lang="forth">\ Rosetta Code Binary Strings Demo in Forth
\ Portions of this code are found at http://forth.sourceforge.net/mirror/toolbelt-ext/index.html
 
\ String words created in this code:
\ STR< STR> STR= COMPARESTR SUBSTR STRPAD CLEARSTR
\ ="" =" STRING: MAXLEN REPLACE-CHAR COPYSTR WRITESTR
\ ," APPEND-CHAR STRING, PLACE CONCAT APPEND C+! ENDSTR
\ COUNT STRLEN
 
: STRLEN ( addr -- length) c@ ; \ alias the "character fetch" operator
 
: COUNT ( addr -- addr+1 length) \ Standard word. Shown for explanation
dup strlen swap 1+ swap ; \ returns the address+1 and the length byte on the stack
 
: ENDSTR ( str -- addr) \ calculate the address at the end of a string
COUNT + ;
 
: C+! ( n addr -- ) \ primitive: increment a byte at addr by n
DUP C@ ROT + SWAP C! ;
 
: APPEND ( addr1 length addr2 -- ) \ Append addr1 length to addr2
2dup 2>r endstr swap move 2r> c+! ;
 
: CONCAT ( string1 string2 -- ) \ concatenate counted string1 to counted string2
>r COUNT R> APPEND ;
 
: PLACE ( addr1 len addr2 -- ) \ addr1 and length, placed at addr2 as counted string
2dup 2>r char+ swap move 2r> c! ;
 
: STRING, ( addr len -- ) \ compile a string at the next available memory (called 'HERE')
here over char+ allot place ;
 
: APPEND-CHAR ( char string -- ) \ append char to string
dup >r count dup 1+ r> c! + c! ;
 
: ," [CHAR] " PARSE STRING, ; \ Parse input stream until '"' and compile into memory
 
 
: WRITESTR ( string -- ) \ output a counted string with a carriage return
count type CR ;
 
: COPYSTR ( string1 string3 -- ) \ String cloning and copying COPYSTR
>r count r> PLACE ;
 
: REPLACE-CHAR ( char1 char2 string -- ) \ replace all char2 with char1 in string
count \ get string's address and length
BOUNDS \ calc start and end addr of string for do-loop
DO \ do a loop from start address to end address
I C@ OVER = \ fetch the char at loop index compare to CHAR2
IF
OVER I C! \ if its equal, store CHAR1 into the index address
THEN
LOOP
2drop ; \ drop the chars off the stack
 
 
256 constant maxlen \ max size of byte counted string in this example
 
: string: CREATE maxlen ALLOT ; \ simple string variable constructor
 
 
: =" ( string -- ) \ String variable assignment operator (compile time only)
[char] " PARSE ROT PLACE ;
 
: ="" ( string -- ) 0 swap c! ; \ empty a string, set count to zero
 
 
: clearstr ( string -- ) \ erase a string variables contents, fill with 0
maxlen erase ;
 
 
string: strpad \ general purpose storage buffer
 
: substr ( string1 start length -- strpad) \ Extract a substring of string and return an output string
>r >r \ push start,length
count \ compute addr,len
r> 1- /string \ pop start, subtract 1, cut string
drop r> \ drop existing length, pop new length
strpad place \ place new stack string in strpad
strpad ; \ return address of strpad
 
\ COMPARE takes the 4 inputs from the stack (addr1 len1 addr2 len2 )
\ and returns a flag for equal (0) , less-than (1) or greater-than (-1) on the stack
 
: comparestr ( string1 string2 -- flag) \ adapt for use with counted strings
count rot count compare ;
 
\ now it's simple to make new operators
: STR= ( string1 string2 -- flag)
comparestr 0= ;
 
: STR> ( string1 string2 -- flag)
comparestr -1 = ;
 
: STR< ( string1 string2 -- flag)
comparestr 1 = ;
 
</syntaxhighlight>
 
With the above code compiled into our system, we can test interactively
at the Forth console to see if we have satisfied the Rosetta code requirements
 
<syntaxhighlight lang="forth">\ Rosetta Code Binary String tasks Console Tests
 
\ 1. String creation and destruction (when needed and if there's no garbage collection or similar mechanism)
 
\ RAW Forth can manually create a binary string with the C, operator.
\ C, takes a byte off the stack and writes it into the next available memory address
\ then increments the Forth internal memory pointer by 1 byte.
\ 'binary_string' drops it's address on the stack. Nothing more. (ie: pointer to the string)
 
HEX ok
create binary_string 9 c, 1 c, 2 c, 3 c, 4 c, 5 c,
0A c, 0B c, 0C c, 0FF c, \ 1st byte is length
ok
 
\ test what we created using the DUMP utility
 
binary_string count dump
25EC:7365 01 02 03 04 05 0A 0B 0C FF 04 44 55 4D 50 00 20 ..........DUMP.
ok
 
 
\ Alternatively we can create static string variables using our constructor
string: buffer1 ok
string: buffer2 ok
DECIMAL ok
\ 2. String assignment
 
\ create string constants with assignments(static, counted strings) ok
create string1 ," Now is the time for all good men to come to the aid"
create string2 ," Right now!" ok
 
\ assign text to string variables with syntacic sugar
buffer1 =" This text will go into the memory allocated for buffer1" ok
buffer2 ="" ok
 
\ or use S" and PLACE
S" The rain in Spain..." buffer2 PLACE ok
\ Test the assignments
string2 writestr Right now!
ok
string1 writestr Now is the time for all good men to come to the aid
ok
buffer1 writestr This text will go into the memory allocated for buffer1
ok
buffer2 writestr The rain in Spain...
ok
 
 
\ destroy string contents. Fill string with zero
buffer1 clearstr ok
buffer1 40 dump
25EC:7370 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
25EC:7380 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
25EC:7390 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
ok
 
\ 3. String comparison. ( the '.' prints the top of the stack in these examples)
buffer1 =" ABCDEFG" ok
buffer2 =" ABCDEFG" ok
buffer1 buffer2 STR= . ( should be -1, TRUE flag) -1 ok
 
string1 buffer1 str> . ( should be 0) 0 ok
string1 buffer1 str< . ( should be -1) -1 ok
 
 
\ 4. String cloning and copying
string1 buffer1 COPYSTR ok
 
string1 writestr Now is the time for all good men to come to the aid ok
buffer1 writestr Now is the time for all good men to come to the aid ok
 
 
\ 5. Check if a string is empty
buffer1 len . 55 ok
buffer1 ="" \ assign null string ok
buffer1 len . 0 ok
 
 
 
\ 6. Append a byte to a string
buffer2 =" Append this" ok
buffer2 writestr Append this
ok
char ! buffer2 APPEND-CHAR ok
buffer2 writestr Append this!
ok
hex ok
0A buffer2 APPEND-CHAR \ append a raw carriage return ok
0D buffer2 APPEND-CHAR \ append a raw line-feed ok
ok
buffer2 writestr Append this!
 
ok
\ we see the extra line before OK so Appending binary chars worked
 
decimal ok
 
\ 7. Extract a substring from a string. Result placed in a temp buffer automagically
 
string1 writestr Now is the time for all good men to come to the aid ok
 
string1 5 11 substr writestr is the time ok
 
 
\ 8. Replace every occurrence of a byte (or a string) in a string with another string
\ BL is a system constant for "Blank" ie the space character (HEX 020)
 
buffer1 =" This*string*is*full*of*stars*" ok
ok
BL char * buffer1 REPLACE-CHAR ok
buffer1 writestr This string is full of stars
ok
 
 
\ 9. Join strings
buffer1 =" James " ok
buffer2 =" Alexander" ok
buffer2 buffer1 CONCAT ok
ok
buffer1 writestr James Alexander
ok
 
</syntaxhighlight>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
Dim As String cad, cad2
'creación de cadenas
cad = "¡Hola Mundo!"
 
'destrucción de cadenas: no es necesario debido a la recolección de basura
cad = ""
 
'clonación/copia de cadena
cad2 = cad
 
'comparación de cadenas
If cad = cad2 Then Print "Las cadenas son iguales"
 
'comprobar si está vacío
If cad = "" Then Print "Cadena vac¡a"
 
'agregar un byte
cad += Chr(33)
 
'extraer una subcadena
cad2 = Mid(cad, 1, 5)
 
'reemplazar bytes
cad2 = "­Hola mundo!"
For i As Integer = 1 To Len(cad2)
If Mid(cad2,i,1) = "l" Then
cad2 = Left(cad2,i-1) + "L" + Mid(cad2,i+1)
End If
Next
Print cad2
 
'unir cadenas
cad = "Hasta " + "pronto " + "de momento."
 
'imprimir caracteres 2 a 4 de una cadena (una subcadena)
For i As Integer = 2 To 4
Print Chr(cad[i])
Next i
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.
 
'''Pascal String example:'''
<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
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
----------------------
Pascal String Examples
----------------------
Length of "Hello, world!" is 13 characters.
String is empty
Hello, universe!
See you later.
</pre>
 
'''CFString example:'''
<syntaxhighlight lang="futurebasic">
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 )
 
// 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>
----------------------
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
 
import (
"bytes"
"fmt"
)
 
// Strings in Go allow arbitrary bytes. They are implemented basically as
// immutable byte slices and syntactic sugar. This program shows functions
// required by the task on byte slices, thus it mostly highlights what
// happens behind the syntactic sugar. The program does not attempt to
// reproduce the immutability property of strings, as that does not seem
// to be the intent of the task.
 
func main() {
// Task point: String creation and destruction.
// Strings are most often constructed from literals as in s := "binary"
// With byte slices,
b := []byte{'b', 'i', 'n', 'a', 'r', 'y'}
fmt.Println(b) // output shows numeric form of bytes.
// Go is garbage collected. There are no destruction operations.
 
// Task point: String assignment.
// t = s assigns strings. Since strings are immutable, it is irrelevant
// whether the string is copied or not.
// With byte slices, the same works,
var c []byte
c = b
fmt.Println(c)
 
// Task point: String comparison.
// operators <, <=, ==, >=, and > work directly on strings comparing them
// by lexicographic order.
// With byte slices, there are standard library functions, bytes.Equal
// and bytes.Compare.
fmt.Println(bytes.Equal(b, c)) // prints true
 
// Task point: String cloning and copying.
// The immutable property of Go strings makes cloning and copying
// meaningless for strings.
// With byte slices though, it is relevant. The assignment c = b shown
// above does a reference copy, leaving both c and b based on the same
// underlying data. To clone or copy the underlying data,
d := make([]byte, len(b)) // allocate new space
copy(d, b) // copy the data
// The data can be manipulated independently now:
d[1] = 'a'
d[4] = 'n'
fmt.Println(string(b)) // convert to string for readable output
fmt.Println(string(d))
 
// Task point: Check if a string is empty.
// Most typical for strings is s == "", but len(s) == 0 works too.
// For byte slices, "" does not work, len(b) == 0 is correct.
fmt.Println(len(b) == 0)
 
// Task point: Append a byte to a string.
// The language does not provide a way to do this directly with strings.
// Instead, the byte must be converted to a one-byte string first, as in,
// s += string('z')
// For byte slices, the language provides the append function,
z := append(b, 'z')
fmt.Printf("%s\n", z) // another way to get readable output
 
// Task point: Extract a substring from a string.
// Slicing syntax is the for both strings and slices.
sub := b[1:3]
fmt.Println(string(sub))
 
// Task point: Replace every occurrence of a byte (or a string)
// in a string with another string.
// Go supports this with similar library functions for strings and
// byte slices. Strings: t = strings.Replace(s, "n", "m", -1).
// The byte slice equivalent returns a modified copy, leaving the
// original byte slice untouched,
f := bytes.Replace(d, []byte{'n'}, []byte{'m'}, -1)
fmt.Printf("%s -> %s\n", d, f)
 
// Task point: Join strings.
// Using slicing syntax again, with strings,
// rem := s[:1] + s[3:] leaves rem == "bary".
// Only the concatenation of the parts is different with byte slices,
rem := append(append([]byte{}, b[:1]...), b[3:]...)
fmt.Println(string(rem))
}</syntaxhighlight>
{{out}}
<pre>
[98 105 110 97 114 121]
[98 105 110 97 114 121]
true
binary
banany
false
binaryz
in
banany -> bamamy
bary
</pre>
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">import java.nio.charset.StandardCharsets
 
class MutableByteString {
private byte[] bytes
private int length
 
MutableByteString(byte... bytes) {
setInternal(bytes)
}
 
int length() {
return length
}
 
boolean isEmpty() {
return length == 0
}
 
byte get(int index) {
return bytes[check(index)]
}
 
void set(byte[] bytes) {
setInternal(bytes)
}
 
void set(int index, byte b) {
bytes[check(index)] = b
}
 
void append(byte b) {
if (length >= bytes.length) {
int len = 2 * bytes.length
if (len < 0) {
len = Integer.MAX_VALUE
}
bytes = Arrays.copyOf(bytes, len)
}
bytes[length] = b
length++
}
 
MutableByteString substring(int from, int to) {
return new MutableByteString(Arrays.copyOfRange(bytes, from, to))
}
 
void replace(byte[] from, byte[] to) {
ByteArrayOutputStream copy = new ByteArrayOutputStream()
if (from.length == 0) {
for (byte b : bytes) {
copy.write(to, 0, to.length)
copy.write(b)
}
copy.write(to, 0, to.length)
} else {
for (int i = 0; i < length; i++) {
if (regionEqualsImpl(i, from)) {
copy.write(to, 0, to.length)
i += from.length - 1
} else {
copy.write(bytes[i])
}
}
}
set(copy.toByteArray())
}
 
boolean regionEquals(int offset, MutableByteString other, int otherOffset, int len) {
if (Math.max(offset, otherOffset) + len < 0) {
return false
}
if (offset + len > length || otherOffset + len > other.length()) {
return false
}
for (int i = 0; i < len; i++) {
if (bytes[offset + i] != other.get(otherOffset + i)) {
return false
}
}
return true
}
 
String toHexString() {
char[] hex = new char[2 * length]
for (int i = 0; i < length; i++) {
hex[2 * i] = "0123456789abcdef".charAt(bytes[i] >> 4 & 0x0F)
hex[2 * i + 1] = "0123456789abcdef".charAt(bytes[i] & 0x0F)
}
return new String(hex)
}
 
String toStringUtf8() {
return new String(bytes, 0, length, StandardCharsets.UTF_8)
}
 
private void setInternal(byte[] bytes) {
this.bytes = bytes.clone()
this.length = bytes.length
}
 
private boolean regionEqualsImpl(int offset, byte[] other) {
int len = other.length
if (offset < 0 || offset + len < 0)
return false
if (offset + len > length)
return false
for (int i = 0; i < len; i++) {
if (bytes[offset + i] != other[i])
return false
}
return true
}
 
private int check(int index) {
if (index < 0 || index >= length)
throw new IndexOutOfBoundsException(String.valueOf(index))
return index
}
}</syntaxhighlight>
 
Test Code
<syntaxhighlight lang="groovy">import org.testng.Assert
import org.testng.annotations.Test
 
import java.nio.charset.StandardCharsets
 
class MutableByteStringTest {
@Test
void replaceEmpty() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
str.replace([] as byte[], ['-' as char] as byte[])
 
Assert.assertEquals(str.toStringUtf8(), "-h-e-l-l-o-")
}
 
@Test
void replaceMultiple() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
str.replace(['l' as char] as byte[], ['1' as char, '2' as char, '3' as char] as byte[])
 
Assert.assertEquals(str.toStringUtf8(), "he123123o")
}
 
@Test
void toHexString() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
 
Assert.assertEquals(str.toHexString(), "68656c6c6f")
}
 
@Test
void append() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
str.append((',' as char) as byte)
str.append((' ' as char) as byte)
str.append(('w' as char) as byte)
str.append(('o' as char) as byte)
str.append(('r' as char) as byte)
str.append(('l' as char) as byte)
str.append(('d' as char) as byte)
 
Assert.assertEquals(str.toStringUtf8(), "hello, world")
}
 
@Test
void substring() {
MutableByteString str = new MutableByteString("hello, world".getBytes(StandardCharsets.UTF_8))
 
Assert.assertEquals(str.substring(0, 5).toStringUtf8(), "hello")
Assert.assertEquals(str.substring(7, 12).toStringUtf8(), "world")
}
 
@Test
void regionEquals(){
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
 
Assert.assertTrue(str.regionEquals(0, new MutableByteString(['h' as char] as byte[]), 0, 1))
Assert.assertFalse(str.regionEquals(0, new MutableByteString(['h' as char] as byte[]), 0, 2))
}
}</syntaxhighlight>
=={{header|Haskell}}==
Note that any of the following functions can be assigned
Line 527 ⟶ 2,060:
as Haskell can be somewhat intimidating to the (currently) non-
functional programmer.
<langsyntaxhighlight lang="haskell">import Text.Regex
{- The above import is needed only for the last function.
It is used there purely for readability and conciseness -}
Line 535 ⟶ 2,068:
Haskell would be able to figure out the type
of "world" -}
string = "world" :: String</langsyntaxhighlight>
 
<langsyntaxhighlight lang="haskell">{- Comparing two given strings and
returning a boolean result using a
simple conditional -}
Line 544 ⟶ 2,077:
if x == y
then True
else False</langsyntaxhighlight>
 
<langsyntaxhighlight lang="haskell">{- As strings are equivalent to lists
of characters in Haskell, test and
see if the given string is an empty list -}
Line 553 ⟶ 2,086:
if x == []
then True
else False</langsyntaxhighlight>
 
<langsyntaxhighlight lang="haskell">{- This is the most obvious way to
append strings, using the built-in
(++) concatenation operator
Line 562 ⟶ 2,095:
as typed strings -}
strAppend :: String -> String -> String
strAppend x y = x ++ y</langsyntaxhighlight>
 
<langsyntaxhighlight lang="haskell">{- Take the specified number of characters
from the given string -}
strExtract :: Int -> String -> String
strExtract x s = take x s</langsyntaxhighlight>
 
<langsyntaxhighlight lang="haskell">{- Take a certain substring, specified by
two integers, from the given string -}
strPull :: Int -> Int -> String -> String
strPull x y s = take (y-x+1) (drop x s)</langsyntaxhighlight>
 
<langsyntaxhighlight lang="haskell">{- Much thanks to brool.com for this nice
and elegant solution. Using an imported standard library
(Text.Regex), replace a given substring with another -}
strReplace :: String -> String -> String -> String
strReplace old new orig = subRegex (mkRegex old) orig new</langsyntaxhighlight>
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon strings strings are variable length and unrestricted. See [[Logical_operations#Icon_and_Unicon|Logical Operations]] for ways to manipulate strings at the bit level.
<syntaxhighlight lang="icon">s := "\x00" # strings can contain any value, even nulls
s := "abc" # create a string
s := &null # destroy a string (garbage collect value of s; set new value to &null)
v := s # assignment
s == t # expression s equals t
s << t # expression s less than t
s <<= t # expression s less than or equal to t
v := s # strings are immutable, no copying or cloning are needed
s == "" # equal empty string
*s = 0 # string length is zero
s ||:= "a" # append a byte "a" to s via concatenation
t := s[2+:3] # t is set to position 2 for 3 characters
s := replace(s,s2,s3) # IPL replace function
s := s1 || s2 # concatenation (joining) of strings</syntaxhighlight>
 
The {{libheader|Icon Programming Library}} provides the procedure [http://www.cs.arizona.edu/icon/library/src/procs/strings.icn replace in strings]
<syntaxhighlight lang="icon">procedure replace(s1, s2, s3) #: string replacement
local result, i
 
result := ""
i := *s2
if i = 0 then fail # would loop on empty string
 
s1 ? {
while result ||:= tab(find(s2)) do {
result ||:= s3
move(i)
}
return result || tab(0)
}
 
end</syntaxhighlight>
=={{header|J}}==
J's literal data type supports arbitrary binary data (strings are binary strings by default). J's semantics are pass by value (with garbage collection) with a minor exception (mapped files).
 
* Example binary string creation
J's literal data type supports arbitrary binary data. J's semantics are pass by value (with garbage collection) with a minor exception (mapped files).
<syntaxhighlight lang="j"> name=: ''</syntaxhighlight>
 
* Example binary string deletion (removing all references to a string allows it to be deleted, in this case we give the name a numeric value to replace its prior string value):
* String creation and destruction is not needed
<syntaxhighlight lang="j"> name=: 0</syntaxhighlight>
<lang j> FIXME: show mapped file example</lang>
 
* StringExample binary string assignment
<langsyntaxhighlight lang="j"> name=: 'value'</langsyntaxhighlight>
 
* StringExample binary string comparison
<syntaxhighlight lang ="j"> name1 -: name2</langsyntaxhighlight>
 
* StringExample binary string cloning and copying is not needed
<syntaxhighlight lang ="j"> FIXMEname1=: show mapped file 'example</lang>'
name2=: name1</syntaxhighlight>
 
Though, technically, its the internal reference which is cloned, not the internal representation of the value. But operations which modify strings are copy on write, so this distinction is not visible without going outside the language.
* Check if a string is empty
<lang j> 0=#string</lang>
 
* AppendExample acheck byte toif a binary string is empty
<syntaxhighlight lang ="j"> 0=#string,byte</langsyntaxhighlight>
 
* ExtractExample apppend a substringbyte fromto a binary string
<syntaxhighlight lang="j"> string=: 'example'
<lang j> 3{.5}.'The quick brown fox runs...'</lang>
byte=: DEL
string=: string,byte</syntaxhighlight>
 
* Extract a substring from a binary string
<syntaxhighlight lang="j"> 3{.5}.'The quick brown fox runs...'</syntaxhighlight>
 
* Replace every occurrence of a byte (or a string) in a string with another string
<langsyntaxhighlight lang="j">require 'strings'
'The quick brown fox runs...' rplc ' ';' !!! '</langsyntaxhighlight>
 
* Join strings
<syntaxhighlight lang ="j"> 'string1','string2'</langsyntaxhighlight>
 
Note also: given an integer n, the corresponding byte value may be obtained by indexing into <code>a.</code> which is the ordered array of all bytes.:
<syntaxhighlight lang="j"> n{a.</syntaxhighlight>
 
Thus, the binary string containing bytes with numeric values 1 0 255 can be obtained this way:
<syntaxhighlight lang="j">1 0 255{a.</syntaxhighlight>
=={{header|Java}}==
 
<syntaxhighlight lang="java">import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
 
public class MutableByteString {
 
private byte[] bytes;
private int length;
 
public MutableByteString(byte... bytes) {
setInternal(bytes);
}
 
public int length() {
return length;
}
 
public boolean isEmpty() {
return length == 0;
}
 
public byte get(int index) {
return bytes[check(index)];
}
 
public void set(byte[] bytes) {
setInternal(bytes);
}
 
public void set(int index, byte b) {
bytes[check(index)] = b;
}
 
public void append(byte b) {
if (length >= bytes.length) {
int len = 2 * bytes.length;
if (len < 0)
len = Integer.MAX_VALUE;
bytes = Arrays.copyOf(bytes, len);
}
bytes[length] = b;
length++;
}
 
public MutableByteString substring(int from, int to) {
return new MutableByteString(Arrays.copyOfRange(bytes, from, to));
}
 
public void replace(byte[] from, byte[] to) {
ByteArrayOutputStream copy = new ByteArrayOutputStream();
if (from.length == 0) {
for (byte b : bytes) {
copy.write(to, 0, to.length);
copy.write(b);
}
copy.write(to, 0, to.length);
} else {
for (int i = 0; i < length; i++) {
if (regionEquals(i, from)) {
copy.write(to, 0, to.length);
i += from.length - 1;
} else {
copy.write(bytes[i]);
}
}
}
set(copy.toByteArray());
}
 
public boolean regionEquals(int offset, MutableByteString other, int otherOffset, int len) {
if (Math.max(offset, otherOffset) + len < 0)
return false;
if (offset + len > length || otherOffset + len > other.length())
return false;
for (int i = 0; i < len; i++) {
if (bytes[offset + i] != other.get(otherOffset + i))
return false;
}
return true;
}
 
public String toHexString() {
char[] hex = new char[2 * length];
for (int i = 0; i < length; i++) {
hex[2 * i] = "0123456789abcdef".charAt(bytes[i] >> 4 & 0x0F);
hex[2 * i + 1] = "0123456789abcdef".charAt(bytes[i] & 0x0F);
}
return new String(hex);
}
 
public String toStringUtf8() {
return new String(bytes, 0, length, StandardCharsets.UTF_8);
}
 
private void setInternal(byte[] bytes) {
this.bytes = bytes.clone();
this.length = bytes.length;
}
 
private boolean regionEquals(int offset, byte[] other) {
int len = other.length;
if (offset < 0 || offset + len < 0)
return false;
if (offset + len > length)
return false;
for (int i = 0; i < len; i++) {
if (bytes[offset + i] != other[i])
return false;
}
return true;
}
 
private int check(int index) {
if (index < 0 || index >= length)
throw new IndexOutOfBoundsException(String.valueOf(index));
return index;
}
}</syntaxhighlight>
 
Test code:
 
<syntaxhighlight lang="java">import static org.hamcrest.CoreMatchers.is;
 
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
import org.junit.Test;
 
public class MutableByteStringTest {
 
@Test
public void testReplaceEmpty() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
str.replace(new byte[]{}, new byte[]{'-'});
 
Assert.assertThat(str.toStringUtf8(), is("-h-e-l-l-o-"));
}
 
@Test
public void testReplaceMultiple() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
str.replace(new byte[]{'l'}, new byte[]{'1', '2', '3'});
 
Assert.assertThat(str.toStringUtf8(), is("he123123o"));
}
 
@Test
public void testToHexString() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
 
Assert.assertThat(str.toHexString(), is("68656c6c6f"));
}
 
@Test
public void testAppend() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
str.append((byte) ',');
str.append((byte) ' ');
str.append((byte) 'w');
str.append((byte) 'o');
str.append((byte) 'r');
str.append((byte) 'l');
str.append((byte) 'd');
 
Assert.assertThat(str.toStringUtf8(), is("hello, world"));
}
@Test
public void testSubstring() {
MutableByteString str = new MutableByteString("hello, world".getBytes(StandardCharsets.UTF_8));
 
Assert.assertThat(str.substring(0, 5).toStringUtf8(), is("hello"));
Assert.assertThat(str.substring(7, 12).toStringUtf8(), is("world"));
}
 
@Test
public void testRegionEquals() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
 
Assert.assertThat(str.regionEquals(0, new MutableByteString(new byte[]{'h'}), 0, 1), is(true));
Assert.assertThat(str.regionEquals(0, new MutableByteString(new byte[]{'h'}), 0, 2), is(false));
}
}</syntaxhighlight>
=={{header|JavaScript}}==
 
JavaScript has native support for binary strings. All strings are "binary" and they're not zero terminated; however to be more exact you can't really see the bytes on the string, strings go from Unicode 0 to Unicode FFFF
<syntaxhighlight lang="javascript">//String creation
var str='';
//or
str2=new String();
 
 
//String assignment
str="Hello";
//or
str2=', Hey there'; //can use " or '
str=str+str2;//concantenates
//string deletion
delete str2;//this will return true or false, true when it has been successfully deleted, it shouldn't/won't work when the variable has been declared with the keyword 'var', you don't have to initialize variables with the var keyword in JavaScript, but when you do, you cannot 'delete' them. However JavaScript garbage collects, so when the function returns, the variable declared on the function is erased.
 
//String comparison
str!=="Hello"; //!== not equal-> returns true there's also !===
str=="Hello, Hey there"; //returns true
//compares 'byte' by 'byte'
"Character Z">"Character A"; //returns true, when using > or < operators it converts the string to an array and evalues the first index that is higher than another. (using unicode values) in this case 'Z' char code is 90 (decimal) and 'A' char code is 65, therefore making one string "larger" than the other.
 
//String cloning and copying
string=str;//Strings are immutable therefore when you assign a string to a variable another one is created. So for two variables to have the 'same' string you have to add that string to an object, and get/set the string from that object
 
//Check if a string is empty
Boolean(''); //returns false
''[0]; //returns undefined
''.charCodeAt(); //returns NaN
''==0; //returns true
''===0; //returns false
''==false; //returns true
 
//Append byte to String
str+="\x40";//using + operator before the equal sign on a string makes it equal to str=str+"\x40"
 
//Extract a substring from a string
//str is "Hello, Hey there@"
str.substr(3); //returns "lo, Hey there@"
str.substr(-5); //returns "here@" negative values just go to the end
str.substr(7,9); //returns "Hey there" index of 7 + 9 characters after the 7
str.substring(3); //same as substr
str.substring(-5); //negative values don't work on substring same as substr(0)
str.substring(7,9); //returns "He" that is, whatever is between index 7 and index 9, same as substring(9,7)
 
//Replace every occurence of x byte with another string
str3="url,url,url,url,url";
str3.replace(/,/g,'\n') //Regex ,returns the same string with the , replaced by \n
str4=str3.replace(/./g,function(index){//it also supports callback functions, the function will be called when a match has been found..
return index==','?'\n':index;//returns replacement
})
 
//Join Strings
[str," ",str3].join(" "/*this is the character that will glue the strings*/)//we can join an array of strings
str3+str4;
str.concat('\n',str4); //concantenate them</syntaxhighlight>
=={{header|jq}}==
 
jq's strings are JSON strings and so cannot be safely used as "binary strings" in the sense of this article. The most convenient way to store a string of bytes in jq is as a jq array of integers, it being understood that jq itself does **not** provide a mechanism for guaranteeing that all the elements of a particular array are integers in the expected range.
 
It is appropriate therefore to introduce a filter for verifying that an entity is an array of integers in the appropriate range:<syntaxhighlight lang="jq"># If the input is a valid representation of a binary string
# then pass it along:
def check_binary:
. as $a
| reduce .[] as $x
($a;
if $x | (type == "number" and . == floor
and 0 <= . and . <= 255) then $a
else error("\(.) is an invalid representation of a byte")
end );</syntaxhighlight>
Examples
<syntaxhighlight lang="jq">## Creation of an entity representing an empty binary string
 
[]
 
## Assignment
 
# Unless a check is appropriate, assignment can be done in the
# usual ways, for example:
 
[0] as $x # assignment to a variable, $x
 
s as $x # assignment of s to a variable
 
.key = s # assignment to a key in a JSON object
 
# If s must be checked, these become:
 
(s|check_binary) as $x
 
.key = (s|check_binary)
 
## Concatenation:
 
str+str2
 
## Comparison
 
[72,101,108,108,111] == ("Hello"|explode) # evaluates to true
 
# Other jq comparison operators (!=, <, >, <=, >=) can be used as well.
 
## Cloning and copying
# In jq, all entities are immutable and so the distinction between
# copying and cloning is irrelevant in jq.
# For example, consider the expression "$s[0] = 1"
# in the following:
 
[0] as $s | $s[0] = 1 | $s
 
# The result is [0] because the expression "$s[0] = 1"
# evaluates to [1] but does not alter $s. The value of
# $s can be changed by assignment, e.g.
 
[0] as $s | $s[0] = 1 | . as $s
 
## Check if an entity represents the empty binary string
 
length == 0
# or
s == []
 
## append a byte, b
 
s + [b] # if the byte, b, is known to be in range
s + ([b]|check_binary) # if b is suspect
 
## Extract a substring from a string
 
# jq uses an index origin of 0 for both JSON arrays strings,
# so to extract the substring with indices from m to (n-1)
# inclusive, the expression s[m:n] can be used.
 
# There are many other possibilities, such as s[m:], s[-1], etc.
 
## Replace every occurrence of one byte, x, with
## another sequence of bytes presented as an array, a,
## of byte-valued integers:
 
reduce .[] as $byte ([];
if $byte == x then . + a else . + [$byte] end)
</syntaxhighlight>
=={{header|Julia}}==
{{trans|MATLAB}}
<syntaxhighlight lang="julia">
# String assignment. Creation and garbage collection are automatic.
a = "123\x00 abc " # strings can contain bytes that are not printable in the local font
b = "456" * '\x09'
c = "789"
println(a)
println(b)
println(c)
 
# string comparison
println("(a == b) is $(a == b)")
# String copying.
A = a
B = b
C = c
println(A)
println(B)
println(C)
# check if string is empty
if length(a) == 0
println("string a is empty")
else
println("string a is not empty")
end
 
# append a byte (actually this is a Char in Julia, and may also be up to 32 bit Unicode) to a string
a= a * '\x64'
println(a)
# extract a substring from string
e = a[1:6]
println(e)
# repeat strings with ^
b4 = b ^ 4
println(b4)
# Replace every occurrence of a string in another string with third string
r = replace(b4, "456" => "xyz")
println(r)
 
# join strings with *
d = a * b * c
println(d)
</syntaxhighlight>
{{output}}<pre>
123 abc
456
789
(a == b) is false
123 abc
456
789
string a is not empty
123 abc d
123 a
456 456 456 456
xyz xyz xyz xyz
123 abc d456 789
</pre>
=={{header|Kotlin}}==
Strings in Kotlin are sequences of 16-bit unicode characters and have a lot of functions built-in, including all those required by this task.
 
To do something different and consistent with the task, I've therefore implemented a simple ByteString class as a sequence of 8-bit signed bytes (Kotlin doesn't yet have an unsigned byte type) using the ISO 8859-1 encoding which, of course, represents the first 256 unicode characters. It's not possible to create user-defined literals in Kotlin and so ByteStrings need to be converted to 'ordinary' Strings to display them as such.
 
The implementation is not intended to be particularly efficient as I've sometimes delegated to the corresponding String class functions in the interests of both simplicity and brevity. Moreover, when Java 9's 'compact strings' feature is implemented, it won't even save memory as Strings which don't contain characters with code-points above 255 are apparently going to be flagged and stored internally as arrays of single bytes by the JVM, not arrays of 2 byte characters as at present.
<syntaxhighlight lang="scala">class ByteString(private val bytes: ByteArray) : Comparable<ByteString> {
val length get() = bytes.size
 
fun isEmpty() = bytes.isEmpty()
 
operator fun plus(other: ByteString): ByteString = ByteString(bytes + other.bytes)
 
operator fun plus(byte: Byte) = ByteString(bytes + byte)
 
operator fun get(index: Int): Byte {
require (index in 0 until length)
return bytes[index]
}
 
fun toByteArray() = bytes
 
fun copy() = ByteString(bytes.copyOf())
 
override fun compareTo(other: ByteString) = this.toString().compareTo(other.toString())
 
override fun equals(other: Any?): Boolean {
if (other == null || other !is ByteString) return false
return compareTo(other) == 0
}
 
override fun hashCode() = this.toString().hashCode()
 
fun substring(startIndex: Int) = ByteString(bytes.sliceArray(startIndex until length))
 
fun substring(startIndex: Int, endIndex: Int) =
ByteString(bytes.sliceArray(startIndex until endIndex))
 
fun replace(oldByte: Byte, newByte: Byte): ByteString {
val ba = ByteArray(length) { if (bytes[it] == oldByte) newByte else bytes[it] }
return ByteString(ba)
}
 
fun replace(oldValue: ByteString, newValue: ByteString) =
this.toString().replace(oldValue.toString(), newValue.toString()).toByteString()
 
override fun toString(): String {
val chars = CharArray(length)
for (i in 0 until length) {
chars[i] = when (bytes[i]) {
in 0..127 -> bytes[i].toChar()
else -> (256 + bytes[i]).toChar()
}
}
return chars.joinToString("")
}
}
 
fun String.toByteString(): ByteString {
val bytes = ByteArray(this.length)
for (i in 0 until this.length) {
bytes[i] = when (this[i].toInt()) {
in 0..127 -> this[i].toByte()
in 128..255 -> (this[i] - 256).toByte()
else -> '?'.toByte() // say
}
}
return ByteString(bytes)
}
 
/* property to be used as an abbreviation for String.toByteString() */
val String.bs get() = this.toByteString()
 
fun main(args: Array<String>) {
val ba = byteArrayOf(65, 66, 67)
val ba2 = byteArrayOf(68, 69, 70)
val bs = ByteString(ba)
val bs2 = ByteString(ba2)
val bs3 = bs + bs2
val bs4 = "GHI£€".toByteString()
println("The length of $bs is ${bs.length}")
println("$bs + $bs2 = $bs3")
println("$bs + D = ${bs + 68}")
println("$bs == ABC is ${bs == bs.copy()}")
println("$bs != ABC is ${bs != bs.copy()}")
println("$bs >= $bs2 is ${bs > bs2}")
println("$bs <= $bs2 is ${bs < bs2}")
println("$bs is ${if (bs.isEmpty()) "empty" else "not empty"}")
println("ABC[1] = ${bs[1].toChar()}")
println("ABC as a byte array is ${bs.toByteArray().contentToString()}")
println("ABCDEF(1..5) = ${bs3.substring(1)}")
println("ABCDEF(2..4) = ${bs3.substring(2,5)}")
println("ABCDEF with C replaced by G is ${bs3.replace(67, 71)}")
println("ABCDEF with CD replaced by GH is ${bs3.replace("CD".bs, "GH".bs)}")
println("GHI£€ as a ByteString is $bs4")
}</syntaxhighlight>
 
{{out}}
<pre>
The length of ABC is 3
ABC + DEF = ABCDEF
ABC + D = ABCD
ABC == ABC is true
ABC != ABC is false
ABC >= DEF is false
ABC <= DEF is true
ABC is not empty
ABC[1] = B
ABC as a byte array is [65, 66, 67]
ABCDEF(1..5) = BCDEF
ABCDEF(2..4) = CDE
ABCDEF with C replaced by G is ABGDEF
ABCDEF with CD replaced by GH is ABGHEF
GHI£€ as a ByteString is GHI£?
</pre>
=={{header|Liberty BASIC}}==
Liberty BASIC's strings are native byte strings. They can contain any byte sequence. They are not zero-terminated. They can be huge in size.
<syntaxhighlight lang="lb">
'string creation
s$ = "Hello, world!"
 
'string destruction - not needed because of garbage collection
s$ = ""
 
'string comparison
s$ = "Hello, world!"
If s$ = "Hello, world!" then print "Equal Strings"
 
'string copying
a$ = s$
 
'check If empty
If s$ = "" then print "Empty String"
 
'append a byte
s$ = s$ + Chr$(33)
 
'extract a substring
a$ = Mid$(s$, 1, 5)
 
'replace bytes
a$ = "Hello, world!"
for i = 1 to len(a$)
if mid$(a$,i,1)="l" then
a$=left$(a$,i-1)+"L"+mid$(a$,i+1)
end if
next
print a$
 
'join strings
s$ = "Good" + "bye" + " for now."
</syntaxhighlight>
=={{header|Lingo}}==
<syntaxhighlight lang="lingo">-- String creation and destruction
foo = "Hello world!" -- created by assignment; destruction via garbage collection
 
-- Strings are binary safe
put numtochar(0) into char 6 of foo
put chartonum(foo.char[6])
-- 0
put str.char[7..foo.length]
-- "world!"
 
-- String cloning and copying
bar = foo -- copies foo contents to bar
 
-- String comparison
put (foo=bar) -- TRUE
put (foo<>bar) -- FALSE
 
-- Check if a string is empty
put (foo=EMPTY)
put (foo="")
put (foo.length=0)
 
-- Append a byte to a string
put "X" after foo
put chartonum(88) after foo
 
-- Extract a substring from a string
put foo.char[3..5]
 
-- Replace every occurrence of a byte (or a string) in a string with another string
 
----------------------------------------
-- Replace in string
-- @param {string} stringToFind
-- @param {string} stringToInsert
-- @param {string} input
-- @return {string}
----------------------------------------
on replaceAll (stringToFind, stringToInsert, input)
output = ""
findLen = stringToFind.length - 1
repeat while TRUE
currOffset = offset(stringToFind, input)
if currOffset=0 then exit repeat
put input.char[1..currOffset] after output
delete the last char of output
put stringToInsert after output
delete input.char[1..(currOffset + findLen)]
end repeat
put input after output
return output
end
 
put replaceAll("o", "X", foo)
 
-- Join strings (4x the same result)
foo = "Hello " & "world!"
foo = "Hello" & numtochar(32) & "world!"
foo = "Hello" & SPACE & "world!"
foo = "Hello" && "world!"</syntaxhighlight>
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">foo = 'foo' -- Ducktyping foo to be string 'foo'
bar = 'bar'
assert (foo == "foo") -- Comparing string var to string literal
Line 643 ⟶ 2,824:
print (str)
 
str = foo .. bar -- Strings concatenate with .. operator</langsyntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">(* String creation and destruction *) BinaryString = {}; BinaryString = . ;
(* String assignment *) BinaryString1 = {12,56,82,65} , BinaryString2 = {83,12,56,65}
-> {12,56,82,65}
-> {83,12,56,65}
(* String comparison *) BinaryString1 === BinaryString2
-> False
(* String cloning and copying *) BinaryString3 = BinaryString1
-> {12,56,82,65}
(* Check if a string is empty *) BinaryString3 === {}
-> False
(* Append a byte to a string *) AppendTo[BinaryString3, 22]
-> {12,56,82,65,22}
(* Extract a substring from a string *) Take[BinaryString3, {2, 5}]
-> {56,82,65,22}
(* Replace every occurrence of a byte (or a string) in a string with another string *)
BinaryString3 /. {22 -> Sequence[33, 44]}
-> {12,56,82,65,33,44}
(* Join strings *) BinaryString4 = Join[BinaryString1 , BinaryString2]
-> {12,56,82,65,83,12,56,65}</syntaxhighlight>
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang="matlab">
a=['123',0,' abc '];
b=['456',9];
c='789';
disp(a);
disp(b);
disp(c);
 
% string comparison
printf('(a==b) is %i\n',strcmp(a,b));
 
% string copying
A = a;
B = b;
C = c;
disp(A);
disp(B);
disp(C);
 
% check if string is empty
if (length(a)==0)
printf('\nstring a is empty\n');
else
printf('\nstring a is not empty\n');
end
 
% append a byte to a string
a=[a,64];
disp(a);
 
% substring
e = a(1:6);
disp(e);
 
% join strings
d=[a,b,c];
disp(d);
</syntaxhighlight>
Output:
<pre>
123 abc
456
789
(a==b) is 0
123 abc
456
789
 
string a is not empty
123 abc @
123 a
123 abc @456 789
</pre>
=={{header|Nim}}==
<syntaxhighlight lang="nim">var # creation
x = "this is a string"
y = "this is another string"
z = "this is a string"
 
if x == z: echo "x is z" # comparison
 
z = "now this is another string too" # assignment
 
y = z # copying
 
if x.len == 0: echo "empty" # check if empty
 
x.add('!') # append a byte
 
echo x[5..8] # substring
echo x[8 .. ^1] # substring
 
z = x & y # join strings
 
import strutils
 
echo z.replace('t', 'T') # replace occurences of t with T</syntaxhighlight>
=={{header|OCaml}}==
 
Line 650 ⟶ 2,928:
 
<code>String.create n</code> returns a fresh string of length n, which initially contains arbitrary characters:
<langsyntaxhighlight lang="ocaml"># String.create 10 ;;
- : string = "\000\023\000\000\001\000\000\000\000\000"</langsyntaxhighlight>
 
No destruction, OCaml features a garbage collector.
Line 658 ⟶ 2,936:
 
* String assignment
<langsyntaxhighlight lang="ocaml"># let str = "some text" ;;
val str : string = "some text"
 
(* modifying a character, OCaml strings are mutable *)
# str.[0] <- 'S' ;;
- : unit = ()</langsyntaxhighlight>
 
* String comparison
<langsyntaxhighlight lang="ocaml"># str = "Some text" ;;
- : bool = true
 
# "Hello" > "Ciao" ;;
- : bool = true</langsyntaxhighlight>
 
* String cloning and copying
<langsyntaxhighlight lang="ocaml"># String.copy str ;;
- : string = "Some text"</langsyntaxhighlight>
 
* Check if a string is empty
<langsyntaxhighlight lang="ocaml"># let string_is_empty s = (s = "") ;;
val string_is_empty : string -> bool = <fun>
 
Line 684 ⟶ 2,962:
 
# string_is_empty "" ;;
- : bool = true</langsyntaxhighlight>
 
* Append a byte to a string
Line 693 ⟶ 2,971:
a byte and return the result as a new string
 
<langsyntaxhighlight lang="ocaml"># str ^ "!" ;;
- : string = "Some text!"</langsyntaxhighlight>
 
But OCaml has a module named Buffer for string buffers.
This module implements string buffers that automatically expand as necessary. It provides accumulative concatenation of strings in quasi-linear time (instead of quadratic time when strings are concatenated pairwise).
 
<syntaxhighlight lang ="ocaml">Buffer.add_char str c</langsyntaxhighlight>
 
* Extract a substring from a string
<langsyntaxhighlight lang="ocaml"># String.sub str 5 4 ;;
- : string = "text"</langsyntaxhighlight>
 
* Replace every occurrence of a byte (or a string) in a string with another string
using the '''Str''' module
<langsyntaxhighlight lang="ocaml"># #load "str.cma";;
# let replace str occ by =
Str.global_replace (Str.regexp_string occ) by str
Line 713 ⟶ 2,991:
val replace : string -> string -> string -> string = <fun>
# replace "The white dog let out a single, loud bark." "white" "black" ;;
- : string = "The black dog let out a single, loud bark."</langsyntaxhighlight>
 
* Join strings
<langsyntaxhighlight lang="ocaml"># "Now just remind me" ^ " how the horse moves again?" ;;
- : string = "Now just remind me how the horse moves again?"</langsyntaxhighlight>
=={{header|PARI/GP}}==
This code accepts arbitrary characters, but you can use <code>Strchr</code> to display ASCII strings.
<syntaxhighlight lang="parigp">cmp_str(u,v)=u==v
copy_str(v)=v \\ Creates a copy, not a pointer
append_str(v,n)=concat(v,n)
replace_str(source, n, replacement)=my(v=[]);for(i=1,#source,v=concat(v,if(source[i]==n,replacement,source[i]))); v
 
u=[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100];
v=[];
cmp_str(u,v)
w=copy_str(v)
#w==0
append_str(u,33)
u[8..12]
replace_str(u,108,[121])
concat(v,w)</syntaxhighlight>
{{out}}
<pre>%1 = 0
%2 = []
%3 = 1
%4 = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
%5 = [119, 111, 114, 108, 100]
%6 = [72, 101, 121, 121, 111, 44, 32, 119, 111, 114, 121, 100]
%7 = []</pre>
=={{header|Pascal}}==
Pascal's original strings were limited to 255 characters. Most implementations had the string length in byte 0. Extension exist for longer strings as well as C compatible string terminated by null. See Examples below
<syntaxhighlight lang="pascal">const
greeting = 'Hello';
var
s1: string;
s2: ansistring;
s3: pchar;
begin
{ Assignments }
s1 := 'Mister Presiden'; (* typo is on purpose. See below! *)
{ Comparisons }
if s2 > 'a' then
writeln ('The first letter of ', s1, ' is later than a');
{ Cloning and copying }
s2 := greeting;
{ Check if a string is empty }
if s1 = '' then
writeln('This string is empty!');
{ Append a byte to a string }
s1 := s1 + 't';
{ Extract a substring from a string }
s3 := copy(S2, 2, 4); (* s3 receives ello *)
{ String replacement } (* the unit StrUtils of the FreePascal rtl has AnsiReplaceStr *)
s1 := AnsiReplaceStr('Thees ees a text weeth typos', 'ee', 'i');
{ Join strings}
s3 := greeting + ' and how are you, ' + s1 + '?';
end.</syntaxhighlight>
=={{header|Perl}}==
Effective string manipulation has been a part of Perl since the beginning. Simple stuff is simply done, but modern Perl also supports Unicode, and tools like <code>pack/unpack</code> let you operate on strings below the level of bytes.
<syntaxhighlight lang="perl">$s = undef;
say 'Nothing to see here' if ! defined $s; # 'Nothing to see here'
say $s = ''; # ''
say 'Empty string' if $s eq ''; # 'Empty string'
say $s = 'be'; # 'be'
say $t = $s; # 'be'
say 'Same' if $t eq $s; # 'Same'
say $t = $t .'e' # 'bee'
say $t .= 'keeper'; # 'beekeeper'
$t =~ s/ee/ook/; say $t; # 'bookkeeper'
say $u = substr $t, 2, 2; # 'ok'
say 'Oklahoma' . ' is ' . uc $u; # 'Oklahoma is OK'</syntaxhighlight>
=={{header|Phix}}==
The native string type in Phix can be used to store raw binary data and supports all of the operations mentioned in this task.
Strings are reference counted, and mutable with copy-on-write semantics. Memory is managed automatically and very efficiently, strings can easily be a billion characters long (on 32-bit, the precise limit is in fact 1,610,612,711 characters, available memory and performance impacts aside) and have a null terminator for C compatibility, but can contain embedded nulls as well.
Note that attempting to set an element (character/byte) to a value outside the range 0..255 will result in automatic expansion to dword-(or qword-)sequence, and can result in a run-time type check.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"abc"</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">x"ef bb bf"</span> <span style="color: #000080;font-style:italic;">-- explicit binary string (the utf8 BOM)</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: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">s</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;">'z'</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"\#EF\0z"</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ok\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
<span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</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;">"xy"</span> <span style="color: #000080;font-style:italic;">-- s remains unaltered</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">t</span> <span style="color: #000080;font-style:italic;">-- "xyz"</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"food"</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">t</span>
<span style="color: #000000;">t</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: #0000FF;">?</span><span style="color: #000000;">t</span> <span style="color: #000080;font-style:italic;">-- "feed"</span>
<span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</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;">"ast"</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">t</span> <span style="color: #000080;font-style:italic;">-- "feasted"</span>
<span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</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;">""</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">t</span> <span style="color: #000080;font-style:italic;">-- "fed"</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"t is empty\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"t is not empty\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"be"</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'t'</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">t</span> <span style="color: #000080;font-style:italic;">-- bet</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'a'</span><span style="color: #0000FF;">&</span><span style="color: #000000;">t</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">t</span> <span style="color: #000080;font-style:italic;">-- abet</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">t</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: #000080;font-style:italic;">-- be</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"be"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"bbo"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- abbot</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"be"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"dep"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- adept</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"be"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"dep"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- to actually modify t</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"abc"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"def"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ghi"</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- "abc def ghi"</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"abc"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"def"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ghi"</span><span style="color: #0000FF;">},</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- "abcdefghi"</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"abc"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"def"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ghi"</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- "abc\ndef\nghi"</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
ok
"xyz"
"food"
"feed"
"feasted"
"fed"
t is not empty
"bet"
"abet"
"be"
"abbot"
"adept"
"abc def ghi"
"abcdefghi"
"abc\ndef\nghi"
</pre>
=={{header|Picat}}==
Strings in Picat are lists of characters.
<syntaxhighlight lang="picat">main => % - String assignment
S1 = "binary_string",
println(s1=S1),
 
% Picat has re-assignments (:=/2) as well,
S1 := "another string",
println(s1=S1),
 
% - String comparison
if S1 == "another string" then
println(same)
else
println(not_same)
end,
% - String cloning and copying
S2 = S1,
println(s2=S2),
S3 = copy_term(S1), % for strings it's the same as =/2
println(s3=S3),
% - Check if a string is empty
if S3 == "" then
println(is_empty)
else
println(not_empty)
end,
 
% - Append a byte to a string
S3 := S3 ++ "s",
println(s3=S3),
% - Extract a substring from a string
println(substring=S3[5..7]),
println(slice=slice(S1,5,7)),
println(slice=slice(S1,5)),
 
% - Replace every occurrence of a byte (or a string) in a string with another string
S4 = replace(S3,'s','x'),
println(s4=S4),
 
% - Join strings
S5 = S1 ++ " " ++ S2,
println(s5=S5),
 
% using append/4
append(S1," ", S2,S6),
println(s6=S6),
 
% find positions of substrings
println(find=findall([From,To],find(S5,"str",From,To))),
 
% split a string
println(split=split(S1," st"))</syntaxhighlight>
 
{{out}}
<pre>s1 = binary_string
s1 = another string
same
s2 = another string
s3 = another string
not_empty
s3 = another strings
substring = her
slice = her
slice = her string
s4 = another xtringx
s5 = another string another string
s6 = another string another string
find = [[9,11],[24,26]]
split = [ano,her,ring]</pre>
 
Since strings are lists of characters, all list functions/procedures are supported including the non-deterministic (backtrackable) <code>member/2</code>, <code>append/3-4</code>, <code>select/3</code> as well as list comprehensions. Some examples:
<syntaxhighlight lang="picat">main =>
println(member=findall(C,(member(C,S1), C @< 'l') )),
 
% find substrings using append/3
S = "string",
println(append=findall([A,B],append(A,B,S))),
% split around "r"
println(append=findall([A,B],append(A,"r",B,S))),
 
% select a character and returns the list without that character
println(select=findall([C,NewS],select(C,S,NewS))),
 
% list comprehension
println(list_comprehension=[ C : C in S5, membchk(C,"aeiou")]),
% sort and remove duplicates
println(sort_remove_dups=sort_remove_dups(S5)).</syntaxhighlight>
 
{{out}}
<pre>member = ahe ig
append = [[[],string],[s,tring],[st,ring],[str,ing],[stri,ng],[strin,g],[string,[]]]
append = [[st,ing]]
select = [[s,tring],[t,sring],[r,sting],[i,strng],[n,strig],[g,strin]]
list_comprehension = aoeiaoei
sort_remove_dups = aeghinorst</pre>
=={{header|PicoLisp}}==
Byte strings are represented in PicoLisp as lists of numbers. They can be
maniplated easily with the built-in list functionality.
 
I/O of raw bytes is done via the 'wr' (write) and 'rd' (read) functions. The
following creates a file consisting of 256 bytes, with values from 0 to 255:
<syntaxhighlight lang="picolisp">: (out "rawfile"
(mapc wr (range 0 255)) )</syntaxhighlight>
Looking at a hex dump of that file:
<syntaxhighlight lang="picolisp">: (hd "rawfile")
00000000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................
00000010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ................
00000020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&'()*+,-./
00000030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 0123456789:;<=>?
...</syntaxhighlight>
To read part of that file, an external tool like 'dd' might be used:
<syntaxhighlight lang="picolisp">: (in '(dd "skip=32" "bs=1" "count=16" "if=rawfile")
(make
(while (rd 1)
(link @) ) ) )
-> (32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47)</syntaxhighlight>
Now such byte lists can be assigned the normal way ('let', 'setq' etc.), they
can be compared with '=', '>', '>=' etc, and manipulated with all internal map-,
filter-, concatenation-, reversal-, pattern matching, and other functions.
 
If desired, a string containing meaningful values can also be converted to
a transient symbol, e.g. the example above
<syntaxhighlight lang="picolisp">: (pack (mapcar char (32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47)))
-> " !\"#$%&'()*+,-./"</syntaxhighlight>
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
/* PL/I has immediate facilities for all those operations except for */
/* replace. */
s = t; /* assignment */
s = t || u; /* catenation - append one or more bytes. */
if length(s) = 0 then ... /* test for an empty string. */
if s = t then ... /* compare strings. */
u = substr(t, i, j); /* take a substring of t beginning at the */
/* i-th character andcontinuing for j */
/* characters. */
substr(u, i, j) = t; /* replace j characters in u, beginning */
/* with the i-th character. */
 
/* In string t, replace every occurrence of string u with string v. */
replace: procedure (t, u, v);
declare (t, u, v) character (*) varying;
 
do until (k = 0);
k = index(t, u);
if k > 0 then
t = substr(t, 1, k-1) || v || substr(t, k+length(u));
end;
end replace;
</syntaxhighlight>
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
Clear-Host
 
## String creation (which is string assignment):
Write-Host "`nString creation (which is string assignment):" -ForegroundColor Cyan
Write-Host '[string]$s = "Hello cruel world"' -ForegroundColor Yellow
[string]$s = "Hello cruel world"
 
## String (or any variable) destruction:
Write-Host "`nString (or any variable) destruction:" -ForegroundColor Cyan
Write-Host 'Remove-Variable -Name s -Force' -ForegroundColor Yellow
Remove-Variable -Name s -Force
 
## Now reassign the variable:
Write-Host "`nNow reassign the variable:" -ForegroundColor Cyan
Write-Host '[string]$s = "Hello cruel world"' -ForegroundColor Yellow
[string]$s = "Hello cruel world"
 
Write-Host "`nString comparison -- default is case insensitive:" -ForegroundColor Cyan
Write-Host '$s -eq "HELLO CRUEL WORLD"' -ForegroundColor Yellow
$s -eq "HELLO CRUEL WORLD"
Write-Host '$s -match "HELLO CRUEL WORLD"' -ForegroundColor Yellow
$s -match "HELLO CRUEL WORLD"
Write-Host '$s -cmatch "HELLO CRUEL WORLD"' -ForegroundColor Yellow
$s -cmatch "HELLO CRUEL WORLD"
 
## Copy a string:
Write-Host "`nCopy a string:" -ForegroundColor Cyan
Write-Host '$t = $s' -ForegroundColor Yellow
$t = $s
 
## Check if a string is empty:
Write-Host "`nCheck if a string is empty:" -ForegroundColor Cyan
Write-Host 'if ($s -eq "") {"String is empty."} else {"String = $s"}' -ForegroundColor Yellow
if ($s -eq "") {"String is empty."} else {"String = $s"}
 
## Append a byte to a string:
Write-Host "`nAppend a byte to a string:" -ForegroundColor Cyan
Write-Host "`$s += [char]46`n`$s" -ForegroundColor Yellow
$s += [char]46
$s
 
## Extract (and display) substring from a string:
Write-Host "`nExtract (and display) substring from a string:" -ForegroundColor Cyan
Write-Host '"Is the world $($s.Substring($s.IndexOf("c"),5))?"' -ForegroundColor Yellow
"Is the world $($s.Substring($s.IndexOf("c"),5))?"
 
## Replace every occurrence of a byte (or a string) in a string with another string:
Write-Host "`nReplace every occurrence of a byte (or a string) in a string with another string:" -ForegroundColor Cyan
Write-Host "`$t = `$s -replace `"cruel`", `"beautiful`"`n`$t" -ForegroundColor Yellow
$t = $s -replace "cruel", "beautiful"
$t
 
## Join strings:
Write-Host "`nJoin strings [1]:" -ForegroundColor Cyan
Write-Host '"Is the world $($s.Split()[1]) or $($t.Split()[1])?"' -ForegroundColor Yellow
"Is the world $($s.Split()[1]) or $($t.Split()[1])?"
Write-Host "`nJoin strings [2]:" -ForegroundColor Cyan
Write-Host '"{0} or {1}... I don''t care." -f (Get-Culture).TextInfo.ToTitleCase($s.Split()[1]), $t.Split()[1]' -ForegroundColor Yellow
"{0} or {1}... I don't care." -f (Get-Culture).TextInfo.ToTitleCase($s.Split()[1]), $t.Split()[1]
Write-Host "`nJoin strings [3] (display an integer array using the -join operater):" -ForegroundColor Cyan
Write-Host '1..12 -join ", "' -ForegroundColor Yellow
1..12 -join ", "
 
## Display an integer array in a tablular format:
Write-Host "`nMore string madness... display an integer array in a tablular format:" -ForegroundColor Cyan
Write-Host '1..12 | Format-Wide {$_.ToString().PadLeft(2)}-Column 3 -Force' -NoNewline -ForegroundColor Yellow
1..12 | Format-Wide {$_.ToString().PadLeft(2)} -Column 3 -Force
</syntaxhighlight>
{{Out}}
<pre>
String creation (which is string assignment):
[string]$s = "Hello cruel world"
 
String (or any variable) destruction:
Remove-Variable -Name s -Force
 
Now reassign the variable:
[string]$s = "Hello cruel world"
 
String comparison -- default is case insensitive:
$s -eq "HELLO CRUEL WORLD"
True
$s -match "HELLO CRUEL WORLD"
True
$s -cmatch "HELLO CRUEL WORLD"
False
 
Copy a string:
$t = $s
 
Check if a string is empty:
if ($s -eq "") {"String is empty."} else {"String = $s"}
String = Hello cruel world
 
Append a byte to a string:
$s += [char]46
$s
Hello cruel world.
 
Extract (and display) substring from a string:
"Is the world $($s.Substring($s.IndexOf("c"),5))?"
Is the world cruel?
 
Replace every occurrence of a byte (or a string) in a string with another string:
$t = $s -replace "cruel", "beautiful"
$t
Hello beautiful world.
 
Join strings [1]:
"Is the world $($s.Split()[1]) or $($t.Split()[1])?"
Is the world cruel or beautiful?
 
Join strings [2]:
"{0} or {1}... I don't care." -f (Get-Culture).TextInfo.ToTitleCase($s.Split()[1]), $t.Split()[1]
Cruel or beautiful... I don't care.
 
Join strings [3] (display an integer array using the -join operater):
1..12 -join ", "
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
 
More string madness... display an integer array in a tablular format:
1..12 | Format-Wide {$_.ToString().PadLeft(2)}-Column 3 -Force
 
1 2 3
4 5 6
7 8 9
10 11 12
</pre>
=={{header|Prolog}}==
 
<syntaxhighlight lang="prolog">% Create a string (no destruction necessary)
?- X = "a test string".
X = "a test string".
 
% String assignment, there is no assignment but you can unify between variables, also 'String cloning and copying'
?- X = "a test string", X = Y.
X = Y, Y = "a test string".
 
% String comparison
?- X = "a test string", Y = "a test string", X = Y.
X = Y, Y = "a test string".
 
?- X = "a test string", Y = "a different string", X = Y.
false.
 
% Test for empty string, this is the same as string comparison.
?- X = "a test string", Y = "", X = Y.
false.
 
?- X = "", Y = "", X = Y.
false.
 
% Append a byte to a string
?- X = "a test string", string_concat(X, "!", Y).
X = "a test string",
Y = "a test string!".
 
% Extract a substring from a string
?- X = "a test string", sub_string(X, 2, 4, _, Y).
X = "a test string",
Y = "test".
 
?- X = "a test string", sub_string(X, Before, Len, After, test).
X = "a test string",
Before = 2,
Len = 4,
After = 7 ;
false.
 
% Replace every occurrence of a byte (or a string) in a string with another string
?- X = "a test string", re_replace('t'/g, 'o', X, Y).
X = "a test string",
Y = "a oeso soring".
 
% Join strings
?- X = "a test string", Y = " with extra!", string_concat(X, Y, Z).
X = "a test string",
Y = " with extra!",
Z = "a test string with extra!".
</syntaxhighlight>
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">
;string creation
x$ = "hello world"
;string destruction
x$ = ""
;string comparison
If x$ = "hello world" : PrintN("String is equal") : EndIf
;string copying;
y$ = x$
; check If empty
If x$ = "" : PrintN("String is empty") : EndIf
; append a byte
x$ = x$ + Chr(41)
; extract a substring
x$ = Mid(x$, 1, 5)
; replace bytes
x$ = ReplaceString(x$, "world", "earth")
; join strings
x$ = "hel" + "lo w" + "orld"
</syntaxhighlight>
=={{header|Python}}==
===2.x===
Python does have a native byte string type. They can contain any byte sequence - they're not zero-terminated. There is a separate type for Unicode data.
Python 2.x's string type (<code>str</code>) is a native byte string type. They can contain any byte sequence - they're not zero-terminated. There is a separate type for Unicode data (<code>unicode</code>).
 
* String creation and destruction
 
<langsyntaxhighlight lang="python">s1 = "A 'string' literal \n"
s2 = 'You may use any of \' or " as delimiter'
s3 = """This text
goes over several lines
up to the closing triple quote"""</langsyntaxhighlight>
 
Strings are normal objects, and are destroyed after they're no more reachable from any other live object.
 
* String assignment
Line 736 ⟶ 3,492:
There is nothing special about assignments:
 
<langsyntaxhighlight lang="python">s = "Hello "
t = "world!"
u = s + t # + concatenates</langsyntaxhighlight>
 
* String comparison
Line 744 ⟶ 3,500:
They're compared byte by byte, lexicographically:
 
<langsyntaxhighlight lang="python">assert "Hello" == 'Hello'
assert '\t' == '\x09'
assert "one" < "two"
assert "two" >= "three"</langsyntaxhighlight>
 
* String cloning and copying
 
Strings are immutable, so there is no need to clone/copy them. If you want to modify a string, you must create a new one with the desired contents. (There is another type, ''array'', that provides a mutable buffer; there is also ''bytearray'' in Python 3)
 
* Check if a string is empty
 
<langsyntaxhighlight lang="python">if x=='': print "Empty string"
if not x: print "Empty string, provided you know x is a string"</langsyntaxhighlight>
 
* Append a byte to a string
 
<langsyntaxhighlight lang="python">txt = "Some text"
txt += '\x07'
# txt refers now to a new string having "Some text\x07"</langsyntaxhighlight>
 
* Extract a substring from a string
Line 768 ⟶ 3,524:
Strings are sequences, they can be indexed with s[index] (index is 0-based) and sliced s[start:stop] (all characters from s[start] up to, but ''not'' including, s[stop])
 
<langsyntaxhighlight lang="python">txt = "Some more text"
assert txt[4] == " "
assert txt[0:4] == "Some"
assert txt[:4] == "Some" # you can omit the starting index if 0
assert txt[5:9] == "more"
assert txt[5:] == "more text" # omitting the second index means "to the end"</langsyntaxhighlight>
 
Negative indexes count from the end: -1 is the last byte, and so on:
 
<langsyntaxhighlight lang="python">txt = "Some more text"
assert txt[-1] == "t"
assert txt[-4:] == "text"</langsyntaxhighlight>
 
* Replace every occurrence of a byte (or a string) in a string with another string
Line 785 ⟶ 3,541:
Strings are objects and have methods, like replace:
 
<langsyntaxhighlight lang="python">v1 = "hello world"
v2 = v1.replace("l", "L")
print v2 # prints heLLo worLd</langsyntaxhighlight>
 
* Join strings
Line 793 ⟶ 3,549:
If they're separate variables, use the + operator:
 
<langsyntaxhighlight lang="python">v1 = "hello"
v2 = "world"
msg = v1 + " " + v2</langsyntaxhighlight>
 
If the elements to join are contained inside any iterable container (e.g. a list)
 
<langsyntaxhighlight lang="python">items = ["Smith", "John", "417 Evergreen Av", "Chimichurri", "481-3172"]
joined = ",".join(items)
print joined
# output:
# Smith,John,417 Evergreen Av,Chimichurri,481-3172</langsyntaxhighlight>
 
The reverse operation (split) is also possible:
 
<langsyntaxhighlight lang="python">line = "Smith,John,417 Evergreen Av,Chimichurri,481-3172"
fields = line.split(',')
print fields
# output:
# ['Smith', 'John', '417 Evergreen Av', 'Chimichurri', '481-3172']</langsyntaxhighlight>
 
===3.x===
Python 3.x has two binary string types: <code>bytes</code> (immutable) and <code>bytearray</code> (mutable). They can contain any byte sequence. They are completely separate from the string type (<code>str</code>). Most of the operators for strings, also work on <code>bytes</code> and <code>bytearray</code>
 
To specify a literal immutable byte string (<code>bytes</code>), prefix a string literal with "b":
<syntaxhighlight lang="python">s1 = b"A 'byte string' literal \n"
s2 = b'You may use any of \' or " as delimiter'
s3 = b"""This text
goes over several lines
up to the closing triple quote"""</syntaxhighlight>
 
You can use the normal string escape sequences to encode special bytes.
 
Indexing a byte string results in an integer (the byte value at that byte):
<syntaxhighlight lang="python">x = b'abc'
x[0] # evaluates to 97</syntaxhighlight>
 
Similarly, a byte string can be converted to and from a list of integers:
 
<syntaxhighlight lang="python">x = b'abc'
list(x) # evaluates to [97, 98, 99]
bytes([97, 98, 99]) # evaluates to b'abc'</syntaxhighlight>
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
#lang racket
 
;; Byte strings can be created either by a function (b1)
;; or as a literal string (b2). No operation is needed for
;; destruction due to garbage collection.
 
(define b1 (make-bytes 5 65)) ; b1 -> #"AAAAA"
(define b2 #"BBBBB") ; b2 -> #"BBBBB"
 
;; String assignment. Note that b2 cannot be
;; mutated since literal byte strings are immutable.
 
(bytes-set! b1 0 66) ; b1 -> #"BAAAA"
 
;; Comparison. Less than & greater than are
;; lexicographic comparison.
 
(bytes=? b1 b2) ; -> #f
(bytes<? b1 b2) ; -> #t
(bytes>? b1 b2) ; -> #f
 
;; Byte strings can be cloned by copying to a
;; new one or by overwriting an existing one.
 
(define b3 (bytes-copy b1)) ; b3 -> #"BAAAA"
(bytes-copy! b1 0 b2) ; b1 -> #"BBBBB"
 
;; Byte strings can be appended to one another. A
;; single byte is appended as a length 1 string.
 
(bytes-append b1 b2) ; -> #"BBBBBBBBBB"
(bytes-append b3 #"B") ; -> #"BAAAAB"
 
;; Substring
 
(subbytes b3 0) ; -> #"BAAAA"
(subbytes b3 0 2) ; -> #"BA"
 
;; Regular expressions can be used to do replacements
;; in a byte string (or ordinary strings)
 
(regexp-replace #"B" b1 #"A") ; -> #"ABBBB" (only the first one)
(regexp-replace* #"B" b1 #"A") ; -> #"AAAAA"
 
;; Joining strings
 
(bytes-join (list b2 b3) #" ") ; -> #"BBBBB BAAAA"
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.03}}
<syntaxhighlight lang="raku" line># Raku is perfectly fine with NUL *characters* in strings:
my Str $s = 'nema' ~ 0.chr ~ 'problema!';
say $s;
 
# However, Raku makes a clear distinction between strings
# (i.e. sequences of characters), like your name, or …
my Str $str = "My God, it's full of chars!";
# … and sequences of bytes (called Bufs), for example a PNG image, or …
my Buf $buf = Buf.new(255, 0, 1, 2, 3);
say $buf;
 
# Strs can be encoded into Blobs …
my Blob $this = 'round-trip'.encode('ascii');
# … and Blobs can be decoded into Strs …
my Str $that = $this.decode('ascii');
 
# So it's all there. Nevertheless, let's solve this task explicitly
# in order to see some nice language features:
 
# We define a class …
class ByteStr {
# … that keeps an array of bytes, and we delegate some
# straight-forward stuff directly to this attribute:
# (Note: "has byte @.bytes" would be nicer, but that is
# not yet implemented in Rakudo.)
has Int @.bytes handles(< Bool elems gist push >);
 
# A handful of methods …
method clone() {
self.new(:@.bytes);
}
 
method substr(Int $pos, Int $length) {
self.new(:bytes(@.bytes[$pos .. $pos + $length - 1]));
}
 
method replace(*@substitutions) {
my %h = @substitutions;
@.bytes.=map: { %h{$_} // $_ }
}
}
 
# A couple of operators for our new type:
multi infix:<cmp>(ByteStr $x, ByteStr $y) { $x.bytes.join cmp $y.bytes.join }
multi infix:<~> (ByteStr $x, ByteStr $y) { ByteStr.new(:bytes(|$x.bytes, |$y.bytes)) }
 
# create some byte strings (destruction not needed due to garbage collection)
my ByteStr $b0 = ByteStr.new;
my ByteStr $b1 = ByteStr.new(:bytes( |'foo'.ords, 0, 10, |'bar'.ords ));
 
# assignment ($b1 and $b2 contain the same ByteStr object afterwards):
my ByteStr $b2 = $b1;
 
# comparing:
say 'b0 cmp b1 = ', $b0 cmp $b1;
say 'b1 cmp b2 = ', $b1 cmp $b2;
 
# cloning:
my $clone = $b1.clone;
$b1.replace('o'.ord => 0);
say 'b1 = ', $b1;
say 'b2 = ', $b2;
say 'clone = ', $clone;
 
# to check for (non-)emptiness we evaluate the ByteStr in boolean context:
say 'b0 is ', $b0 ?? 'not empty' !! 'empty';
say 'b1 is ', $b1 ?? 'not empty' !! 'empty';
 
# appending a byte:
$b1.push: 123;
say 'appended = ', $b1;
 
# extracting a substring:
my $sub = $b1.substr(2, 4);
say 'substr = ', $sub;
 
# replacing a byte:
$b2.replace(102 => 103);
say 'replaced = ', $b2;
 
# joining:
my ByteStr $b3 = $b1 ~ $sub;
say 'joined = ', $b3;</syntaxhighlight>
 
{{out}}
Note: The ␀ represents a NUL byte.
<pre>nema␀problema!
Buf:0x<ff 00 01 02 03>
round-trip
b0 cmp b1 = Less
b1 cmp b2 = Same
b1 = [102 0 0 0 10 98 97 114]
b2 = [102 0 0 0 10 98 97 114]
clone = [102 111 111 0 10 98 97 114]
b0 is empty
b1 is not empty
appended = [102 0 0 0 10 98 97 114 123]
substr = [0 0 10 98]
replaced = [103 0 0 0 10 98 97 114 123]
joined = [103 0 0 0 10 98 97 114 123 0 0 10 98]</pre>
=={{header|Red}}==
<syntaxhighlight lang="rebol">Red []
s: copy "abc" ;; string creation
 
s: none ;; destruction
t: "Abc"
if t == "abc" [print "equal case"] ;; comparison , case sensitive
if t = "abc" [print "equal (case insensitive)"] ;; comparison , case insensitive
s: copy "" ;; copying string
if empty? s [print "string is empty "] ;; check if string is empty
append s #"a" ;; append byte
substr: copy/part at "1234" 3 2 ;; ~ substr ("1234" ,3,2) , red has 1 based indices !
?? substr
s: replace/all "abcabcabc" "bc" "x" ;; replace all "bc" by "x"
?? s
s: append "hello " "world" ;; join 2 strings
?? s
s: rejoin ["hello " "world" " !"] ;; join multiple strings
?? s
</syntaxhighlight>
{{out}}
<pre>equal (case insensitive)
string is empty
substr: "34"
s: "axaxax"
s: "hello world"
s: "hello world !"
>>
</pre>
=={{header|REXX}}==
Programming note: &nbsp; this REXX example demonstrates two types of &nbsp; ''quoting''.
<syntaxhighlight lang="rexx">/*REXX program demonstrates methods (code examples) to use and express binary strings.*/
dingsta= '11110101'b /*four versions, bit string assignment.*/
dingsta= "11110101"b /*this is the same assignment as above.*/
dingsta= '11110101'B /* " " " " " " " */
dingsta= '1111 0101'B /* " " " " " " */
 
dingsta2= dingsta /*clone one string to another (a copy).*/
other= '1001 0101 1111 0111'b /*another binary (or bit) string. */
if dingsta= other then say 'they are equal' /*compare the two (binary) strings. */
if other== '' then say "OTHER is empty." /*see if the OTHER string is empty.*/
otherA= other || '$' /*append a dollar sign ($) to OTHER. */
otherB= other'$' /*same as above, but with less fuss. */
guts= substr(c2b(other), 10, 3) /*obtain the 10th through 12th bits.*/
new= changeStr('A' , other, "Z") /*change the upper letter A ──► Z. */
tt= changeStr('~~', other, ";") /*change two tildes ──► one semicolon.*/
joined= dingsta || dingsta2 /*join two strings together (concat). */
say joined c2b(joined)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
c2b: Return x2b(c2x(arg(1))) /*return the string as a binary string.*/</syntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, &nbsp; so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]].
<br><br>
{{out}}
<pre>§§ 1111010111110101</pre>
 
=={{header|Ring}}==
The String in the Ring programming language holds and manipulates an arbitrary sequence of bytes.
 
<syntaxhighlight lang="ring"># string creation
x = "hello world"
# string destruction
x = NULL
# string assignment with a null byte
x = "a"+char(0)+"b"
see len(x) # ==> 3
# string comparison
if x = "hello"
See "equal"
else
See "not equal"
ok
y = 'bc'
if strcmp(x,y) < 0
See x + " is lexicographically less than " + y
ok
# string cloning
xx = x
See x = xx # true, same length and content
 
# check if empty
if x = NULL
See "is empty"
ok
# append a byte
x += char(7)
# substring
x = "hello"
x[1] = "H"
See x + nl
# join strings
a = "hel"
b = "lo w"
c = "orld"
See a + b + c
</syntaxhighlight>
=={{header|RPL}}==
Strings are a sequence of bytes.
 
String creation:
"This is a new string" <span style="color:grey">@ String is put at stack level 1</span>
No need for destruction as there is a garbage collection mechanism.
 
String assignment:
"This is a string" '<span style="color:green">MYTEXT</span>' STO <span style="color:grey">@ String is transferred from stack level 1 to MYTEXT variable</span>
String comparison: numeric operators can be used for equality or lexicographic order
"This" "That" == <span style="color:grey">@ test equality - returns 0 here</span>
"This" "That" > <span style="color:grey">@ test order - returns 1 here</span>
Inclusion can also be tested:
"This" "is" POS <span style="color:grey">@ returns position of substring - 3 here</span>
String cloning and copying: any object, including strings, can be duplicated in the stack:
"This" DUP <span style="color:grey">@ Put a copy of "This" at stack level #2</span>
Check if a string is empty:
<span style="color:green">MYTEXT</span> "" == <span style="color:grey">@ returns 1 if MYTEXT contains an empty string</span>
or
<span style="color:green">MYTEXT</span> SIZE NOT <span style="color:grey">@ returns 1 if MYTEXT contains an empty string</span>
Append a byte to a string:
"Thi" "s" +
Extract a substring from a string:
"This is a string" 2 4 SUB <span style="color:grey">@ returns substring from 2nd to 4th character</span>
Replace every occurrence of a byte (or a string) in a string with another string: we need a small program here, since the REPL function replaces only the first occurence of the searched substring
≪ → string old new <span style="color:grey">@ store arguments</span>
≪ string <span style="color:grey">@ put string to be replaced in stack</span>
'''WHILE''' DUP old POS '''REPEAT''' <span style="color:grey">@ while old is present in string</span>
LAST SWAP new REPL <span style="color:grey">@ replace old by new</span>
'''END'''
≫ ≫ '<span style="color:blue">REPLALL</span>' STO
Join strings:
"This is" " a string" +
 
=={{header|Ruby}}==
A String object holds and manipulates an arbitrary sequence of bytes. There are also the [http://www.ruby-doc.org/core/classes/Array.html#M002222 Array#pack] and [http://www.ruby-doc.org/core/classes/String.html#M000760 String#unpack] methods to convert data to binary strings.
<langsyntaxhighlight lang="ruby"># string creation
x = "hello world"
Line 827 ⟶ 3,895:
# string comparison
if x == "hello"
puts "equal"
else
puts "not equal"
Line 847 ⟶ 3,915:
# append a byte
p x << "\07"
# substring
p xx = x[0..-2]
x[1,2] = "XYZ"
 
p x
# replace bytes
p y = "hello world".tr("l", "L")
# join strings
Line 859 ⟶ 3,929:
b = "lo w"
c = "orld"
p d = a + b + c</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">' Create string
s$ = "Hello, world"
' String destruction
s$ = ""
' String comparison
If s$ = "Hello, world" then print "Equal String"
' Copying string
a$ = s$
 
' Check If empty
If s$ = "" then print "String is MT"
' 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" then
a$=left$(a$,i-1)+"universe"+mid$(a$,i+5)
end if
next
print a$
'join strings
s$ = "See " + "you " + "later."
print s$</syntaxhighlight>
=={{header|Rust}}==
For extra documentation, refer to [https://doc.rust-lang.org/std/string/struct.String.html] and [https://doc.rust-lang.org/book/strings.html].
<syntaxhighlight lang="rust">use std::str;
 
fn main() {
// Create new string
let string = String::from("Hello world!");
println!("{}", string);
assert_eq!(string, "Hello world!", "Incorrect string text");
 
// Create and assign value to string
let mut assigned_str = String::new();
assert_eq!(assigned_str, "", "Incorrect string creation");
assigned_str += "Text has been assigned!";
println!("{}", assigned_str);
assert_eq!(assigned_str, "Text has been assigned!","Incorrect string text");
 
// String comparison, compared lexicographically byte-wise same as the asserts above
if string == "Hello world!" && assigned_str == "Text has been assigned!" {
println!("Strings are equal");
}
 
// Cloning -> string can still be used after cloning
let clone_str = string.clone();
println!("String is:{} and Clone string is: {}", string, clone_str);
assert_eq!(clone_str, string, "Incorrect string creation");
 
// Copying, string won't be usable anymore, accessing it will cause compiler failure
let copy_str = string;
println!("String copied now: {}", copy_str);
 
// Check if string is empty
let empty_str = String::new();
assert!(empty_str.is_empty(), "Error, string should be empty");
 
// Append byte, Rust strings are a stream of UTF-8 bytes
let byte_vec = [65]; // contains A
let byte_str = str::from_utf8(&byte_vec).unwrap();
assert_eq!(byte_str, "A", "Incorrect byte append");
 
// Substrings can be accessed through slices
let test_str = "Blah String";
let mut sub_str = &test_str[0..11];
assert_eq!(sub_str, "Blah String", "Error in slicing");
sub_str = &test_str[1..5];
assert_eq!(sub_str, "lah ", "Error in slicing");
sub_str = &test_str[3..];
assert_eq!(sub_str, "h String", "Error in slicing");
sub_str = &test_str[..2];
assert_eq!(sub_str, "Bl", "Error in slicing");
 
// String replace, note string is immutable
let org_str = "Hello";
assert_eq!(org_str.replace("l", "a"), "Heaao", "Error in replacement");
assert_eq!(org_str.replace("ll", "r"), "Hero", "Error in replacement");
 
// Joining strings requires a `String` and an &str or a two `String`s one of which needs an & for coercion
let str1 = "Hi";
let str2 = " There";
let fin_str = str1.to_string() + str2;
assert_eq!(fin_str, "Hi There", "Error in concatenation");
 
// Joining strings requires a `String` and an &str or two `Strings`s, one of which needs an & for coercion
let str1 = "Hi";
let str2 = " There";
let fin_str = str1.to_string() + str2;
assert_eq!(fin_str, "Hi There", "Error in concatenation");
 
// Splits -- note Rust supports passing patterns to splits
let f_str = "Pooja and Sundar are up in Tumkur";
let split_str: Vec<_> = f_str.split(' ').collect();
assert_eq!(split_str, ["Pooja", "and", "Sundar", "are", "up", "in", "Tumkur"], "Error in string split");
}</syntaxhighlight>
=={{header|Seed7}}==
Seed7 strings are capable to hold binary data.
The memory of Seed7 strings is managed automatically.
String declaration:
<pre>
var string: stri is "asdf"; # variable declaration
const string: stri is "jkl"; # constant declaration
</pre>
 
String assignment
<pre>
stri := "blah";
</pre>
 
String comparison
<pre>
stri1 = stri2 # equal
stri1 <> stri2 # not equal
stri1 < stri2 # less than
stri1 <= stri2 # less than or equal
stri1 > stri2 # greater than
stri1 >= stri2 # greater than or equal
compare(stri1, stri2) # return -1, 0 or 1, depending on the comparison.
</pre>
 
String copying (same as assignment)
<pre>
stri2 := stri2;
</pre>
 
Check if a string is empty
<pre>
stri = "" # compare with ""
length(stri) = 0 # check length
</pre>
 
Append a byte to a string
<pre>
stri &:= 'a';
</pre>
 
Extract a substring from a string
<pre>
stri[startPos .. endPos] # substring from startPos to endPos
stri[startPos ..] # substring from startPos to the end of stri
stri[.. endPos] # substring from the beginning of stri to endPos
stri[startPos len aLength # substring from startPos with maximum length of aLength
</pre>
 
Replace every occurrence of a byte (or a string) in a string with another string
<pre>
replace(stri,"la","al");
</pre>
 
Join strings
<pre>
stri3 = stri1 & stri2;
</pre>
 
The [http://seed7.sourceforge.net/libraries/string.htm string.s7i] library contains
more string functions.
=={{header|Smalltalk}}==
Smalltalk strings are variable length and unrestricted. They are builtin and no additional library is req'd.
 
<syntaxhighlight lang="smalltalk">s := "abc" # create a string (immutable if its a literal constant in the program)
s := #[16r01 16r02 16r00 16r03] asString # strings can contain any value, even nulls
s := String new:3. # a mutable string
v := s # assignment
s = t # same contents
s < t # less
s <= t # less or equal
s = '' # equal empty string
s isEmpty # ditto
s size # string length
t := s copy # a copy
t := s copyFrom:2 to:3 # a substring
t := s copyReplaceFrom:2 to:3 with:'**' # a copy with some replacements
s replaceFrom:2 to:3 with:'**' # inplace replace (must be mutable)
s replaceAll:$x with:$y # destructive replace of characters
s copyReplaceAll:$x with:$y # non-destructive replace
s replaceString:s1 withString:s2 # substring replace
s3 := s1 , s2 # concatenation of strings
s2 := s1 , $x # append a character
s2 := s1 , 123 asCharacter # append an arbitrary byte
s := 'Hello / 今日は' # they support unicode (at least up to 16rFFFF, some more)</syntaxhighlight>
 
In addition (because they inherit from collection), a lot more is inherited (map, fold, enumeration, finding substrings, etc.)
=={{header|Tcl}}==
Tcl strings are binary safe, and a binary string is any string that only contains UNICODE characters in the range <tt>\u0000</tt>–<tt>\u00FF</tt>.
<langsyntaxhighlight lang="tcl"># string creation
set x "hello world"
 
Line 898 ⟶ 4,162:
set b "lo w"
set c "orld"
set d $a$b$c</langsyntaxhighlight>
=={{header|VBA}}==
Before start, see this link :
https://msdn.microsoft.com/fr-fr/VBA/Language-Reference-VBA/articles/option-compare-statement :
<pre>The Option Compare instruction is used at module level to declare the default comparison method to use when string data is compared.
The default text comparison method is Binary.
</pre>
<syntaxhighlight lang="vb">
'Set the string comparison method to Binary.
Option Compare Binary ' That is, "AAA" is less than "aaa".
' Set the string comparison method to Text.
Option Compare Text ' That is, "AAA" is equal to "aaa".</syntaxhighlight>
String creation and destruction :
<syntaxhighlight lang="vb">
Sub Creation_String_FirstWay()
Dim myString As String
'Here, myString is created and equal ""
End Sub '==> Here the string is destructed !</syntaxhighlight>
String assignment :
<syntaxhighlight lang="vb">Sub String_Assignment()
Dim myString$
'Here, myString is created and equal ""
'assignments :
myString = vbNullString 'return : ""
myString = "Hello World" 'return : "Hello World"
myString = String(12, "A") 'return : "AAAAAAAAAAAA"
End Sub</syntaxhighlight>
String comparison :
<syntaxhighlight lang="vb">Sub String_Comparison_FirstWay()
Dim A$, B$, C$
 
If A = B Then Debug.Print "A = B"
A = "creation": B = "destruction": C = "CREATION"
'test equality : (operator =)
If A = B Then
Debug.Print A & " = " & B
'used to Sort : (operator < and >)
ElseIf A > B Then
Debug.Print A & " > " & B
Else 'here : A < B
Debug.Print A & " < " & B
End If
'test if A is different from C
If A <> C Then Debug.Print A & " and " & B & " are differents."
'same test without case-sensitive
If UCase(A) = UCase(C) Then Debug.Print A & " = " & C & " (no case-sensitive)"
'operator Like :
If A Like "*ation" Then Debug.Print A & " Like *ation"
If Not B Like "*ation" Then Debug.Print B & " Not Like *ation"
'See Also :
'https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/like-operator
End Sub</syntaxhighlight>
String cloning and copying :
<syntaxhighlight lang="vb">Sub String_Clone_Copy()
Dim A As String, B$
A = "Hello world!"
'cloning :
B = A
End Sub</syntaxhighlight>
Check if a string is empty :
<syntaxhighlight lang="vb">Sub Check_Is_Empty()
Dim A As String, B As Variant
 
Debug.Print IsEmpty(A) 'return False
Debug.Print IsEmpty(Null) 'return False
Debug.Print IsEmpty(B) 'return True ==> B is a Variant
 
Debug.Print A = vbNullString 'return True
Debug.Print StrPtr(A) 'return 0 (zero)
'Press the OK button without enter a data in the InputBox :
A = InputBox("Enter your own String : ")
Debug.Print A = "" 'return True
Debug.Print IsEmpty(A) 'return False
Debug.Print StrPtr(A) = 0 'return False
'Press the cancel button (with or without enter a data in the InputBox)
A = InputBox("Enter your own String : ")
Debug.Print StrPtr(A) = 0 'return True
Debug.Print IsEmpty(A) 'return False
Debug.Print A = "" 'return True
'Note : StrPtr is the only way to know if you cancel the inputbox
End Sub</syntaxhighlight>
Append a byte to a string :
<syntaxhighlight lang="vb">Sub Append_to_string()
Dim A As String
A = "Hello worl"
Debug.Print A & Chr(100) 'return : Hello world
End Sub</syntaxhighlight>
Extract a substring from a string :
<syntaxhighlight lang="vb">Sub ExtractFromString()
Dim A$, B As String
A = "Hello world"
B = Mid(A, 3, 8)
Debug.Print B 'return : llo worl
End Sub</syntaxhighlight>
Replace every occurrence of a byte (or a string) in a string with another string :
<syntaxhighlight lang="vb">Sub ReplaceInString()
Dim A$, B As String, C$
A = "Hello world"
B = Chr(108) ' "l"
C = " "
Debug.Print Replace(A, B, C) 'return : He o wor d
End Sub</syntaxhighlight>
Join Strings :
<syntaxhighlight lang="vb">Sub Join_Strings()
Dim A$, B As String
 
A = "Hello"
B = "world"
Debug.Print A & " " & B 'return : Hello world
'If you're sure that A and B are Strings, you can use + instead of & :
Debug.Print A + " " + B 'return : Hello world
End Sub
</syntaxhighlight>
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Sub Main()
'string creation
Dim x = "hello world"
 
' mark string for garbage collection
x = Nothing
 
' string assignment with a null byte
x = "ab" + Chr(0)
Console.WriteLine(x)
Console.WriteLine(x.Length)
 
'string comparison
If x = "hello" Then
Console.WriteLine("equal")
Else
Console.WriteLine("not equal")
End If
 
If x.CompareTo("bc") = -1 Then
Console.WriteLine("x is lexicographically less than 'bc'")
End If
 
'string cloning
Dim c(3) As Char
x.CopyTo(0, c, 0, 3)
Dim objecty As New String(c)
Dim y As New String(c)
 
Console.WriteLine(x = y) 'same as string.equals
Console.WriteLine(x.Equals(y)) 'it overrides object.equals
 
Console.WriteLine(x = objecty) 'uses object.equals, return false
 
'check if empty
Dim empty = ""
Dim nullString As String = Nothing
Dim whitespace = " "
If IsNothing(nullString) AndAlso empty = String.Empty _
AndAlso String.IsNullOrEmpty(nullString) AndAlso String.IsNullOrEmpty(empty) _
AndAlso String.IsNullOrWhiteSpace(nullString) AndAlso String.IsNullOrWhiteSpace(empty) _
AndAlso String.IsNullOrWhiteSpace(whitespace) Then
Console.WriteLine("Strings are null, empty or whitespace")
End If
 
'append a byte
x = "helloworld"
x += Chr(83)
Console.WriteLine(x)
 
'substring
Dim slice = x.Substring(5, 5)
Console.WriteLine(slice)
 
'replace bytes
Dim greeting = x.Replace("worldS", "")
Console.WriteLine(greeting)
 
'join strings
Dim join = greeting + " " + slice
Console.WriteLine(join)
End Sub
 
End Module</syntaxhighlight>
=={{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.
<syntaxhighlight lang="wren">// 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"</syntaxhighlight>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Binary_strings
// by Galileo, 07/2022
 
// Create string
s$ = "Hello, world"
// String destruction
s$ = ""
// String comparison
If s$ = "Hello, world" print "Equal String"
// Copying string
a$ = s$
// Check If empty
If s$ = "" print "String is MT"
// 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" then
a$=left$(a$,i-1)+"universe"+mid$(a$,i+5)
end if
next
print a$
//join strings
s$ = "See " + "you " + "later."
print s$</syntaxhighlight>
{{out}}
<pre>String is MT
Hello, universe
See you later.
---Program done, press RETURN---</pre>
=={{header|zkl}}==
Strings in zkl are bytes with a zero terminator. So they are binary safe as long as the data doesn't contain a zero. Data objects are true binary strings: bytes with a length. It is basically a binary editor. Datas come in two flavors: Int and String. The difference is some methods treat String type as containing (zero or more) null terminated strings. Datas have an implicit null terminator so they can easily convert to a String.
<syntaxhighlight lang="zkl">Data(0,.Int,1,2,3) // bytes
Data(0,String,1,2,3) // same
Data(0,Int,"foo","bar") //-->foobar\0
d:=Data(0,String,"foo","bar") //-->foo\0bar\0\0
d==d // -->True: byte by byte comparison
d.copy() //-->clone
d.len() //-->8, 0 if empty
 
d.append("1").len(); //-->10 // or d+"1"
Data(0,Int,"foo","bar").len() //-->6
Data(0,Int,"foo","bar").append("1").len() //-->7
 
d.readString(4) //-->"bar"
d.readNthString(2) //-->"1"
d[2,4] //-->"o", really "o\0ba" but String sees the null
 
while(Void!=(n:=d.findString("bar"))){ d[n,4]="hoho" }
d.bytes() //-->L(102,111,111,0,104,111,104,111,0,49,0)
 
d2:=Data(0,Int,"sam");
d.append(d2).text // or d+d2</syntaxhighlight>
{{omit from|GUISS}}
{{omit from|Lotus 123 Macro Scripting}}
416

edits