# Programming with ooc/Strings
## String type
### Usage

The String type is defined in the core module lang/types. String literals such as "abc", are of this type.
On the right hand side a String object is created represented by the string literal. Its reference is assigned to the str variable.
A few operators are overloaded for strings by default. For example, ( + ) is used as a string concatenation operator. For example:
( + ) is overloaded for pretty much every type inherited from C (strings, chars, numeric types) so that you can do
Also overloaded in the standard SDK is [ ], used as a slicing operator
Note: the slicing operator applies on bytes, not characters. See the UTF-8 Support section below for more informations.
Many more operators are overloaded on strings, such as == for comparison, * for repeating, [ ]= for bound-checked modification, etc.
Strings in ooc aren't immutable, but every method in the standard type String returns a copy of the given string, and never modifies the original.
The standard type String provides a nice set of methods for string manipulation. Since they all return copies, you should do things like that
But never:
This would create a new trimmed string and throw it away. It is good to study the String type and know its methods in order not to reinvent the wheel.

### UTF-8 support

At the time of this writing, there is no built-in UTF-8 support in ooc. The length function returns the number of bytes used to store the String, never the number of characters.
The reason for that is that there is no clearly defined boundaries between character in the Unicode standard. One can roughly determinate 'grapheme clusters', i.e. associate modifiers with glyphs that correspond to characters, etc. but it's a very difficult problem, and there are edge cases with non-European/non-American languages.
Therefore, for now, there's no UTF-8 character, nor codepoint, etc. but that doesn't prevent one from using UTF-8 in ooc programs. The ICU and utf8proc libraries seem especially interesting for handling such encoding matters in an ooc codebase.
Note that the language design on this issue is not definitive, and is subject to changes in the future, as soon as other, more pressing matters are decided upon.

### Length in bytes

As a consequence of the lack of UTF-8 support, the length methods returns the number of bytes so that:
is 2, but
is 6, because 3 bytes are used to store each of the characters that make up the Japanese word "Kanji"

### Creating new strings

You can create a new String from a char:
or just allocate a fixed number of chars:
A String literal, such as "abc", is also of type String

### Iterating through a string

You can iterate over the bytes of a String, because it implements the iterator method.

### Comparing strings

You can use the == operator to compare two Strings, because it's overloaded in the SDK. It calls the equals method, so that, in ooc:
This behavior greatly enhances the readability of ooc code, as opposed to, say, Java.
You can still compare the addresses of Strings by casting them to Pointers first:
The compare method can be used to test parts of strings for equality, for example:

### Substrings and slicing

The [ ] operator can be used with a range to obtain the same effect as calling substring

### Searching in a string

The indexOf and lastIndexOf methods allow to search for the first and last occurrence, respectively, of a byte or a string in another string.

### Repeating a string

A String can be repeated multiple time using the overloaded ( * ) operator, or with the times method:
Note that because of precedence, we can't write:
Because the compiler would read that as:
Which is definitely not what we intended.

### Reversing a string

A String can be reversed using the reverse method.
Be aware that reverse works on bytes, not characters. See the UTF-8 Support section for more information.

### Appending strings

You can use either the ( + ) operator or the append and prepend methods:
As with all other string methods, a copy is returned, the original string is not modified.
However, if you are building a string from many smaller parts, it is better to use a Buffer instead, as detailed below.