> For the complete documentation index, see [llms.txt](https://enma-1.gitbook.io/enma/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://enma-1.gitbook.io/enma/addons/strings.md).

# Strings

`std::string` (UTF-8 / byte string) and `std::wstring` (UTF-16) are value structs from the `std` prelude — available in every script, no import. With `using namespace std;` the bare spellings `string` / `wstring` work too.

```cpp
std::string s = "hello";
s += " world";
if (s.starts_with("hello") && s.size() == 11) { ... }
```

A string literal is a `const char*` (a wide literal `L"..."` a `wchar*`). Literals bind `char*` parameters and comparisons directly — `s == "hi"`, `s + "x"`, `"lit" + s` allocate no temporary. There is no implicit `std::string` → `char*` conversion; call `.c_str()` where a native expects a C string.

## std::string

```cpp
string()                     // empty
string(char* src)            // from C string / literal
string(const std::string&)   // copy

s.size()     s.length()      // element count (both, exactly C++)
s.empty()
s.at(i)                      // char at index (also s[i], read-only)
s.c_str()                    // char* to the buffer
s.substr(start, count)
s.find(needle)               // std::string, char* or char; optional start pos
s.rfind(needle)
s.contains(needle)
s.starts_with(p)  s.ends_with(p)
s.append(o)      s.push_back(ch)
s.insert(pos, str)           // mutating; returns std::string&
s.erase(pos, count)          // mutating; returns std::string&
s.clear()
```

Operators: `+` `+=` `==` `!=` `<` `>` `<=` `>=` against `std::string` and `char*` / literals on either side.

## Utilities (members with no C++ name collision)

```cpp
s.split(delim)               // → std::vector<std::string>
s.chars()                    // → std::vector<int64> of char codes
s.to_upper()  s.to_lower()
s.trim()  s.trim_left()  s.trim_right()
s.reverse()
s.repeat(n)
s.pad_left(width, ch)  s.pad_right(width, ch)
s.starts_with_i(p)  s.ends_with_i(p)   // case-insensitive
```

## Free functions (namespace std)

```cpp
std::stoi(s)                          // string → int64 (also wstring)
std::stod(s)                          // string → float64
std::to_string(x)                     // int64 / float64 / bool / string / char*
std::replace_all(s, from, to)         // → new string
std::replace_first(s, from, to)
std::join(vec, sep)                   // vector<string> / vector<int64> / vector<wstring>
std::format("a={} b={}", x, y)        // variadic; {f} reinterprets the slot as float
chr(code)   ord(ch)
hex_encode(v)  hex_decode(s)  hex_to_int(s)  to_hex(v)
base64_encode(s)  base64_decode(s)
url_encode(s)  url_decode(s)
```

`f"x={expr}"` interpolation desugars to `std::to_string(expr)` joined with `+` — overload `to_string` for a user type to make it interpolable.

## std::wstring

Same shape with `wchar` (UTF-16) elements: size/length, empty, at, c\_str, find/rfind, substr, insert/erase, push\_back, and the comparison/concat operators against `std::wstring` and `wchar*` / `L"..."`.

Cross-width conversion is explicit, exactly like C++ (no implicit string↔wstring conversion):

```cpp
std::wstring w = wstring_from_str(s);    // UTF-8 → UTF-16 (surrogate-aware)
std::string  s2 = wstring_to_str(w);     // UTF-16 → UTF-8
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://enma-1.gitbook.io/enma/addons/strings.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
