# std.string

String manipulation utilities for common text operations.

```tea
use string from "std.string"
```

Examples may be fragments requiring the module import or additional setup.

## std.string.index_of

```tea
pub def index_of(text: String, pattern: String) -> Int
```

Find the first occurrence of a substring.

## std.string.contains

```tea
pub def contains(text: String, pattern: String) -> Bool
```

Return true when the string contains a substring.

## std.string.split

```tea
pub def split(text: String, delimiter: String) -> List[String]
```

Split a string by a delimiter.

```tea
use string from "std.string"

string.split("red,green,blue", ",") # => ["red", "green", "blue"]
```

## std.string.last_index_of

```tea
pub def last_index_of(text: String, pattern: String) -> Int
```

Find the last occurrence of a substring.

## std.string.starts_with

```tea
pub def starts_with(text: String, prefix: String) -> Bool
```

Check if a string starts with a given prefix.

```tea
use string from "std.string"

string.starts_with("hello world", "hello") # => true
string.starts_with("hello world", "world") # => false
```

## std.string.ends_with

```tea
pub def ends_with(text: String, suffix: String) -> Bool
```

Check if a string ends with a given suffix.

```tea
string.ends_with("hello world", "world")  # => true
string.ends_with("hello world", "hello")  # => false
```

## std.string.trim_start

```tea
pub def trim_start(text: String) -> String
```

Trim whitespace from the start of a string.

```tea
string.trim_start("  hello  ")  # => "hello  "
string.trim_start("\t\nhello")  # => "hello"
```

## std.string.trim_end

```tea
pub def trim_end(text: String) -> String
```

Trim whitespace from the end of a string.

```tea
string.trim_end("  hello  ")  # => "  hello"
string.trim_end("hello\n\t")  # => "hello"
```

## std.string.trim

```tea
pub def trim(text: String) -> String
```

Trim whitespace from both ends of a string.

```tea
string.trim("  hello  ")  # => "hello"
string.trim("\t\nhello\n\t")  # => "hello"
```

## std.string.reverse

```tea
pub def reverse(text: String) -> String
```

Reverse a string.

```tea
string.reverse("hello")  # => "olleh"
string.reverse("ab")  # => "ba"
```

## std.string.replace

```tea
pub def replace(text: String, pattern: String, replacement: String) -> String
```

Replace all occurrences of a substring with another string.

```tea
string.replace("hello world", "world", "tea")  # => "hello tea"
string.replace("foo bar foo", "foo", "baz")  # => "baz bar baz"
```

## std.string.replace_once

```tea
pub def replace_once(text: String, pattern: String, replacement: String) -> String
```

Replace the first occurrence of a substring with another string.

## std.string.to_lower

```tea
pub def to_lower(text: String) -> String
```

Convert a string to lowercase.

```tea
string.to_lower("Hello World")  # => "hello world"
string.to_lower("TEA")  # => "tea"
```

## std.string.to_upper

```tea
pub def to_upper(text: String) -> String
```

Convert a string to uppercase.

```tea
string.to_upper("Hello World")  # => "HELLO WORLD"
string.to_upper("tea")  # => "TEA"
```

## std.string.join

```tea
pub def join(parts: List[String], separator: String) -> String
```

Join a list of strings with a separator.

## std.string.lines

```tea
pub def lines(text: String) -> List[String]
```

Split text into lines, trimming a trailing "\r" from each line and dropping the final empty line produced by a trailing newline.

## std.string.split_once

```tea
pub def split_once(text: String, delimiter: String) -> List[String]?
```

Split a string into two parts at the first occurrence of a delimiter.

```tea
use string from "std.string"

const parts = string.split_once("name=green=tea", "=")
if parts != nil
  const pair = parts!
  @println(pair[0]) # name
  @println(pair[1]) # green=tea
end
@println(string.split_once("name", "=") == nil) # true
```

## std.string.rsplit_once

```tea
pub def rsplit_once(text: String, delimiter: String) -> List[String]?
```

Split a string into two parts at the last occurrence of a delimiter.

```tea
use string from "std.string"

const parts = string.rsplit_once("archive.tar.gz", ".")
if parts != nil
  @println(parts!) # [archive.tar, gz]
end
```

## std.string.count

```tea
pub def count(text: String, pattern: String) -> Int
```

Count non-overlapping occurrences of a substring.

## std.string.strip_prefix

```tea
pub def strip_prefix(text: String, prefix: String) -> String?
```

Remove a prefix when present.

```tea
use string from "std.string"

@println(string.strip_prefix("Bearer token123", "Bearer ") ?? "") # token123
@println(string.strip_prefix("token123", "Bearer ") == nil) # true
```

## std.string.strip_suffix

```tea
pub def strip_suffix(text: String, suffix: String) -> String?
```

Remove a suffix when present.

```tea
use string from "std.string"

@println(string.strip_suffix("report.txt", ".txt") ?? "report.txt") # report
```

## std.string.repeat

```tea
pub def repeat(text: String, times: Int) -> String
```

Repeat a string count times.

## std.string.pad_start_with

```tea
pub def pad_start_with(text: String, width: Int, fill: String) -> String
```

Pad a string on the left with a fill string until it reaches width.

```tea
use string from "std.string"

@println(string.pad_start_with("42", 5, "0")) # 00042
```

## std.string.pad_start

```tea
pub def pad_start(text: String, width: Int) -> String
```

Pad a string on the left with spaces until it reaches width.

## std.string.pad_end_with

```tea
pub def pad_end_with(text: String, width: Int, fill: String) -> String
```

Pad a string on the right with a fill string until it reaches width.

## std.string.pad_end

```tea
pub def pad_end(text: String, width: Int) -> String
```

Pad a string on the right with spaces until it reaches width.

```tea
use string from "std.string"

@println(string.pad_end("tea", 6) + "|") # tea   |
```
