Strings
String representation, System::String utilities, StringBuilder, and encoding.
The String Type: std::string
In sharp-runtime, C# string maps directly to std::string.
There is no custom String object type. std::string is UTF-8 by convention,
though the library does not enforce encoding — it relies on the content passed by the caller.
In SharpRuntimeHelper.hpp, a type alias is provided for porting convenience:
namespace SharpRuntime {
using String = std::string; // C# string → std::string
}
System::String — Static Utility Class
include/System/String.hpp | src/System/String.cpp
System::String is a deleted-constructor static class — it cannot be instantiated.
It provides static helper methods that mirror commonly-used C# string instance methods.
Status: Partial — only a useful subset is implemented.
| Method | Description | Status |
|---|---|---|
Split(string, char) | Splits by a single character delimiter; returns vector<string> | Implemented |
IsEmpty(string) | True if string is empty | Implemented |
IsNullOrEmpty(string) | True if string is empty (null not possible in C++, same as IsEmpty) | Implemented |
StartsWith(string, string) | True if string starts with prefix | Implemented |
Format(string, intcs) | Formats with one integer argument | Implemented |
Format(string, string) | Formats with one string argument | Implemented |
ToString(intcs, int, char) | Zero-pads integer to given width | Implemented |
using namespace System;
auto parts = String::Split("a,b,c", ','); // {"a", "b", "c"}
bool empty = String::IsNullOrEmpty(""); // true
bool sw = String::StartsWith("hello", "hel"); // true
std::string fmt = String::Format("{0} items", 5); // "5 items"
string has many instance methods (ToLower(), Trim(), Contains(), etc.)
that are NOT in System::String. For these, use std::string methods directly:
s.find(), s.substr(), etc., or the C++20/23 standard library.
System::Text::StringBuilder
include/System/Text/StringBuilder.hpp | src/System/Text/StringBuilder.cpp
A mutable string buffer for efficient string construction. Backed by an internal std::string.
Status: Partial
System::Text::StringBuilder sb;
sb.Append("Hello");
sb.Append(", ");
sb.Append("World");
std::string result = sb.ToString(); // "Hello, World"
sb.Clear();
// Insert, Remove, Replace are also implemented
sb.Append("abcdef");
sb.Insert(2, "XY"); // "abXYcdef"
sb.Remove(0, 2); // "XYcdef"
sb.Replace("cd", "ZZ"); // "XYZZef"
String Comparison
include/System/StringComparison.hpp | include/System/StringComparer.hpp
Headers for StringComparison enum (OrdinalIgnoreCase, CurrentCulture, etc.) and StringComparer class exist as compatibility types.
StringComparer comparison modes are fully implemented needs verification from the source headers.
Encoding
include/System/Text/Encoding.hpp
Abstract encoding base class with static accessors for common encodings:
Encoding::UTF8→UTF8EncodingEncoding::ASCII→ASCIIEncodingEncoding::Latin1→Latin1EncodingEncoding::Unicode→UnicodeEncoding(UTF-16)Encoding::UTF32→UTF32Encoding
Character Type
C# char (UTF-16, 16-bit) maps to SharpRuntime::charcs = char16_t.
In practice, most string operations in the library use std::string (byte strings)
rather than std::u16string.