strings Module
The strings module provides functions for string manipulation.
String Builder
Harneet provides a strings.Builder for efficient string concatenation, modeled after Go's strings.Builder.
Creating a Builder
Use the constructor to create a builder instance:
Returns (builder, error) where error is None on success.
Methods
-
WriteString(s string) (int, error)Appendssto the builder and returns bytes written. -
String() (string, error)Returns the accumulated string.
Example
Notes
- Follow normal tuple-return patterns: always check the
errorvalue. strings.Builderis ideal for building large strings incrementally.
Multiline Strings
Harneet supports multiline strings using backtick (`) delimiters. These are raw strings that preserve formatting and don't interpret escape sequences, making them perfect for SQL queries, HTML templates, JSON data, and configuration files.
Basic Syntax
| Multiline String Syntax | |
|---|---|
Key Features
- Raw String Behavior: No escape sequence processing - backslashes, quotes, and other special characters are treated literally
- Formatting Preservation: All whitespace, indentation, and line breaks are preserved exactly as written
- No Escaping Required: Include quotes, backslashes, and other special characters without escaping
Common Use Cases
SQL Queries
JSON Data
| JSON Data | |
|---|---|
HTML Templates
| HTML Templates | |
|---|---|
Configuration Files
| Configuration Files | |
|---|---|
Raw String Examples
Multiline strings are raw strings, meaning special characters are treated literally:
Integration with String Functions
Multiline strings work seamlessly with all existing string functions:
Best Practices
- Use for Formatted Content: Perfect for SQL, HTML, JSON, YAML, and other structured text
- Preserve Indentation: Maintain consistent indentation for readability
- No Escape Sequences: Remember that
\n,\t, etc. are literal characters, not escape sequences - String Operations: All existing string functions work normally with multiline strings
Migration from Regular Strings
If you have existing code with escaped strings, you can easily migrate to multiline strings:
| Migration Example | |
|---|---|
Common Gotchas and Best Practices
Gotcha 1: Literal vs Interpreted Escape Sequences
| Escape Sequences Gotcha | |
|---|---|
Gotcha 2: Leading and Trailing Whitespace
| Whitespace Gotcha | |
|---|---|
Gotcha 3: Backticks in Content
| Backticks Gotcha | |
|---|---|
Best Practice 1: Consistent Indentation
Best Practice 2: Choose the Right String Type
Best Practice 3: String Operations Work Identically
Slicing strings
Strings in Harneet support Go-like slicing using the s[a:b] syntax (and the shorthand forms [:b], [a:], and [:]).
Rules and semantics: - Indices a and b must be integers. The type checker validates this. - Bounds are clamped to the valid range [0, len(s)]. - Half-open interval: a is inclusive, b is exclusive. - If a > b, the result is an empty string. - Slicing is currently byte-based. If your strings contain multi-byte UTF-8 characters, make sure your indices align with character boundaries to avoid splitting a rune. Rune-aware slicing may be added in the future.
Examples:
| String Slicing | |
|---|---|
See array and typed-array slicing semantics: arrays module documentation.
Functions
Len(string)
Returns the length of a string.
Parameters: - string: A string.
Returns: - (integer, error): The length of the string, or an error if the argument is not a string.
Example:
| Len Example | |
|---|---|
FromCodePoint(int)
Constructs a single-character string from a Unicode code point (rune).
Parameters: - int: Unicode code point in the range 0..0x10FFFF.
Returns: - (string, error): The single-character string, or an error if the code point is out of range.
Example:
| FromCodePoint Example | |
|---|---|
CodePoints(string)
Converts a UTF-8 string into an array of Unicode code points (runes) represented as integers.
Parameters: - string: Input string.
Returns: - (array, error): An array of integers, one per Unicode code point in the string, or an error if the argument is not a string.
Example:
| CodePoints Example | |
|---|---|
Upper(string)
Converts a string to uppercase.
Parameters: - string: A string.
Returns: - (string, error): The uppercase version of the string, or an error if the argument is not a string.
Example:
| Upper Example | |
|---|---|
Lower(string)
Converts a string to lowercase.
Parameters: - string: A string.
Returns: - (string, error): The lowercase version of the string, or an error if the argument is not a string.
Example:
| Lower Example | |
|---|---|
Contains(string, substring)
Checks if a string contains a substring.
Parameters: - string: The string to search in. - substring: The substring to search for.
Returns: - (boolean, error): true if the substring is found, false otherwise. Returns an error if the arguments are not strings.
Example:
| Contains Example | |
|---|---|
Replace(string, old, new)
Replaces all occurrences of a substring with a new string.
Parameters: - string: The original string. - old: The substring to be replaced. - new: The new string to replace with.
Returns: - (string, error): A new string with all occurrences of old replaced by new. Returns an error if the arguments are not strings.
Example:
| Replace Example | |
|---|---|
Split(string, separator)
Splits a string into an array of substrings.
Parameters: - string: The string to be split. - separator: The separator to split by.
Returns: - (array, error): An array of strings. Returns an error if the arguments are not strings.
Example:
| Split Example | |
|---|---|
ToInt(string)
Parses a string to an integer.
Parameters: - string: The string to be parsed.
Returns: - (integer, error): The integer value, or an error if the string cannot be parsed as an integer.
Example:
HasPrefix(string, prefix)
Checks whether a string begins with the given prefix.
Parameters: - string: The string to test. - prefix: The prefix to check.
Returns: - (boolean, error): true if string starts with prefix, else false.
Example:
| HasPrefix Example | |
|---|---|
HasSuffix(string, suffix)
Checks whether a string ends with the given suffix.
Parameters: - string: The string to test. - suffix: The suffix to check.
Returns: - (boolean, error): true if string ends with suffix, else false.
Example:
| HasSuffix Example | |
|---|---|
Index(string, substr)
Returns the index of the first occurrence of substr in string, or -1 if not present.
Parameters: - string: The string to search. - substr: The substring to find.
Returns: - (integer, error): The index or -1.
Example:
| Index Example | |
|---|---|
LastIndex(string, substr)
Returns the index of the last occurrence of substr in string, or -1 if not present.
Parameters: - string: The string to search. - substr: The substring to find.
Returns: - (integer, error): The index or -1.
Example:
| LastIndex Example | |
|---|---|
Trim(string, cutset)
Removes all leading and trailing characters in cutset from string.
Parameters: - string: The string to trim. - cutset: Characters to trim from both ends.
Returns: - (string, error): The trimmed string.
Example:
| Trim Example | |
|---|---|
TrimSpace(string)
Removes leading and trailing Unicode whitespace.
Parameters: - string: The string to trim.
Returns: - (string, error): The trimmed string.
Example:
| TrimSpace Example | |
|---|---|
TrimPrefix(string, prefix)
Removes prefix from the start of string if present.
Parameters: - string: The input string. - prefix: Prefix to remove.
Returns: - (string, error): The result string.
Example:
| TrimPrefix Example | |
|---|---|
TrimSuffix(string, suffix)
Removes suffix from the end of string if present.
Parameters: - string: The input string. - suffix: Suffix to remove.
Returns: - (string, error): The result string.
Example:
| TrimSuffix Example | |
|---|---|
Repeat(string, count)
Returns a new string consisting of count copies of string.
Parameters: - string: The string to repeat. - count: Number of repetitions (non-negative).
Returns: - (string, error): The repeated string.
Example:
| Repeat Example | |
|---|---|
Count(string, substr)
Counts non-overlapping instances of substr in string.
Parameters: - string: The string to search. - substr: The substring to count.
Returns: - (integer, error): The count.
Example:
| Count Example | |
|---|---|
Join(array, sep)
Joins an array of strings using the separator sep.
Parameters: - array: Array of strings. - sep: Separator string.
Returns: - (string, error): The joined string.
Example:
| Join Example | |
|---|---|
Fields(string)
Splits a string around runs of Unicode whitespace.
Parameters: - string: Input string.
Returns: - (array, error): Array of fields.
Example:
| Fields Example | |
|---|---|
ReplaceN(string, old, new, n)
Replaces up to n non-overlapping instances of old with new. If n < 0, replaces all.
Parameters: - string: Source string. - old: Substring to replace. - new: Replacement string. - n: Maximum number of replacements (-1 for all).
Returns: - (string, error): The result string.
Example:
| ReplaceN Example | |
|---|---|
EqualFold(a, b)
Reports whether a and b are equal under simple Unicode case-folding.
Parameters: - a: First string. - b: Second string.
Returns: - (boolean, error): true if equal ignoring case.
Example:
| EqualFold Example | |
|---|---|
Compare(a, b)
Lexicographically compares two strings.
Parameters: - a: First string. - b: Second string.
Returns: - (integer, error): 0 if equal, -1 if a < b, +1 if a > b.
Example: