System::String

Static string utility class. The actual string type is std::string.

include/System/String.hpp

Status: Partial

Key Design Choice
In sharp-runtime, C# string maps to std::string, not to System::String. The System::String class contains only static utility methods. Its constructor is deleted — you cannot instantiate it.

Overview

When porting C# code, replace all uses of C# string (the type) with std::string. Static methods that C# calls as string.IsNullOrEmpty(s) become System::String::IsNullOrEmpty(s) in sharp-runtime.

Static Methods

IsNullOrEmpty / IsNullOrWhiteSpace

static bool IsNullOrEmpty(const std::string& s);
static bool IsNullOrWhiteSpace(const std::string& s);

IsNullOrEmpty returns true if the string is empty (there is no null in C++; "null" strings map to empty strings by convention).

IsEmpty

static bool IsEmpty(const std::string& s);

Equivalent to s.empty().

StartsWith / EndsWith

static bool StartsWith(const std::string& s, const std::string& prefix);
static bool EndsWith(const std::string& s, const std::string& suffix);

Checks if the string starts or ends with the given substring.

Contains

static bool Contains(const std::string& s, const std::string& value);

Returns true if value is found within s.

Split

static std::vector<std::string> Split(const std::string& s, char delimiter);
static std::vector<std::string> Split(const std::string& s, const std::string& delimiter);

Splits the string on a delimiter. Returns a std::vector<std::string> (maps to C# string[]).

Format

template<typename... Args>
static std::string Format(const std::string& format, Args&&... args);

C#-style format string. Placeholder syntax: {0}, {1}, etc.

std::string s = System::String::Format("Hello {0}, you are {1} years old", name, age);

ToString

template<typename T>
static std::string ToString(T value);

Converts a value to its string representation.

Join

static std::string Join(const std::string& separator,
                        const std::vector<std::string>& values);

Joins vector elements with a separator string.

Trim / TrimStart / TrimEnd

static std::string Trim(const std::string& s);
static std::string TrimStart(const std::string& s);
static std::string TrimEnd(const std::string& s);

Removes leading/trailing whitespace.

ToUpper / ToLower

static std::string ToUpper(const std::string& s);
static std::string ToLower(const std::string& s);

Case conversion. Needs verification — locale behavior may differ from .NET.

Replace

static std::string Replace(const std::string& s,
                           const std::string& oldValue,
                           const std::string& newValue);

Returns a new string with all occurrences of oldValue replaced.

Substring

static std::string Substring(const std::string& s, int startIndex);
static std::string Substring(const std::string& s, int startIndex, int length);

Extracts a substring. Delegates to std::string::substr.

String Comparisons

Use standard C++ equality operators for string comparison:

// C# equality:  s1 == s2
// sharp-runtime: s1 == s2   (std::string operator==)

// C# ordinal ignore case:  string.Equals(a, b, StringComparison.OrdinalIgnoreCase)
// sharp-runtime:
bool eq = System::String::Equals(a, b, System::StringComparison::OrdinalIgnoreCase);

Mapping from C#

C# codesharp-runtime equivalent
string s = "hello";std::string s = "hello";
string.IsNullOrEmpty(s)System::String::IsNullOrEmpty(s)
s.StartsWith("x")System::String::StartsWith(s, "x")
s.Split(',')System::String::Split(s, ',')
string.Format("{0}", x)System::String::Format("{0}", x)
s.Lengths.size() or s.length()
s[i]s[i] (same)
s1 + s2s1 + s2 (same)
s == nulls.empty() (by convention)

char type

C# char is a 16-bit Unicode value. In sharp-runtime the alias charcs maps to char16_t. The underlying std::string uses 8-bit char (UTF-8 bytes). See Strings for the encoding discussion.