Binary strings: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 422:
return 0;
}</lang>
 
=={{header|C++}}==
<lang cpp>#include <iomanip>
#include <iostream>
 
using namespace std;
 
string replaceFirst(string &s, const string &target, const string &replace) {
auto pos = s.find(target);
if (pos == string::npos) return s;
return s.replace(pos, target.length(), replace);
}
 
int main() {
 
// string creation
string x = "hello world";
 
// reassign string (no garbage collection)
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
auto join = greeting + ' ' + slice;
cout << join << '\n';
 
return 0;
}</lang>
{{out}}
<pre>ab
2
not equal
x is lexigraphically less than 'bc'
true
false
String is empty
helloworldS
world
hello
hello world</pre>
 
=={{header|C sharp|C#}}==