> ## Documentation Index
> Fetch the complete documentation index at: https://fluxon.tabooproject.org/llms.txt
> Use this file to discover all available pages before exploring further.

# String 扩展函数

> 字符串操作、查找、转换等扩展方法

String 扩展函数通过上下文调用语法 `::` 使用，提供丰富的字符串处理能力。

## 基础属性

| 方法          | 说明                     |
| ----------- | ---------------------- |
| `length()`  | 返回字符串长度。               |
| `isEmpty()` | 检查字符串是否为空（长度为 0）。      |
| `isBlank()` | 检查字符串是否为空白（空或仅包含空白字符）。 |

```fluxon theme={null}
"hello" :: length()       # => 5
"" :: isEmpty()           # => true
"   " :: isBlank()        # => true
"  x  " :: isBlank()      # => false
```

## 去除空白

| 方法        | 说明            |
| --------- | ------------- |
| `trim()`  | 去除首尾空白字符。     |
| `ltrim()` | 去除左侧（开头）空白字符。 |
| `rtrim()` | 去除右侧（结尾）空白字符。 |

```fluxon theme={null}
"  hello  " :: trim()     # => "hello"
"  hello  " :: ltrim()    # => "hello  "
"  hello  " :: rtrim()    # => "  hello"
```

## 查找与包含

| 方法                        | 说明                          |
| ------------------------- | --------------------------- |
| `indexOf(str)`            | 返回子字符串首次出现的索引，未找到返回 `-1`。   |
| `indexOf(str, fromIndex)` | 从指定位置开始查找子字符串。              |
| `lastIndexOf(str)`        | 返回子字符串最后一次出现的索引，未找到返回 `-1`。 |
| `contains(str)`           | 检查是否包含指定子字符串。               |
| `startsWith(prefix)`      | 检查是否以指定前缀开头。                |
| `endsWith(suffix)`        | 检查是否以指定后缀结尾。                |

```fluxon theme={null}
"hello world" :: indexOf("o")        # => 4
"hello world" :: indexOf("o", 5)     # => 7
"hello world" :: lastIndexOf("o")    # => 7
"hello world" :: contains("wor")     # => true
"hello world" :: startsWith("hello") # => true
"hello world" :: endsWith("world")   # => true
```

## 大小写转换

| 方法             | 说明          |
| -------------- | ----------- |
| `lowercase()`  | 转换为小写。      |
| `uppercase()`  | 转换为大写。      |
| `capitalize()` | 首字母大写，其余不变。 |

```fluxon theme={null}
"Hello World" :: lowercase()   # => "hello world"
"Hello World" :: uppercase()   # => "HELLO WORLD"
"hello" :: capitalize()        # => "Hello"
```

## 字符串操作

| 方法                               | 说明             |
| -------------------------------- | -------------- |
| `substring(start)`               | 从指定位置截取到末尾。    |
| `substring(start, end)`          | 截取指定范围（左闭右开）。  |
| `replace(old, new)`              | 替换首次出现的子字符串。   |
| `replaceAll(regex, replacement)` | 使用正则表达式替换所有匹配。 |
| `split(delimiter)`               | 按分隔符分割为列表。     |
| `split(regex, limit)`            | 使用正则分割，限制结果数量。 |
| `reverse()`                      | 反转字符串。         |
| `repeat(count)`                  | 重复字符串指定次数。     |

```fluxon theme={null}
"hello world" :: substring(6)           # => "world"
"hello world" :: substring(0, 5)        # => "hello"
"hello world" :: replace("world", "fluxon")  # => "hello fluxon"
"a1b2c3" :: replaceAll("\\d", "X")      # => "aXbXcX"
"a,b,c" :: split(",")                   # => ["a", "b", "c"]
"hello" :: reverse()                    # => "olleh"
"ab" :: repeat(3)                       # => "ababab"
```

## 填充

| 方法                       | 说明             |
| ------------------------ | -------------- |
| `padLeft(length)`        | 左侧填充空格到指定长度。   |
| `padLeft(length, char)`  | 左侧填充指定字符到指定长度。 |
| `padRight(length)`       | 右侧填充空格到指定长度。   |
| `padRight(length, char)` | 右侧填充指定字符到指定长度。 |

```fluxon theme={null}
"42" :: padLeft(5)          # => "   42"
"42" :: padLeft(5, "0")     # => "00042"
"hi" :: padRight(5)         # => "hi   "
"hi" :: padRight(5, ".")    # => "hi..."
```

## 字符访问

| 方法                  | 说明                    |
| ------------------- | --------------------- |
| `charAt(index)`     | 返回指定位置的字符（字符串形式）。     |
| `charCodeAt(index)` | 返回指定位置字符的 Unicode 码点。 |
| `toCharArray()`     | 将字符串转换为字符数组。          |

```fluxon theme={null}
"hello" :: charAt(0)        # => "h"
"hello" :: charCodeAt(0)    # => 104
"abc" :: toCharArray()      # => ["a", "b", "c"]
```

## 正则匹配

| 方法               | 说明                     |
| ---------------- | ---------------------- |
| `matches(regex)` | 检查整个字符串是否匹配正则表达式。      |
| `findAll(regex)` | 查找所有匹配正则表达式的子字符串，返回列表。 |

```fluxon theme={null}
"hello123" :: matches(".*\\d+")     # => true
"a1b2c3" :: findAll("\\d")          # => ["1", "2", "3"]
"hello world" :: findAll("\\w+")    # => ["hello", "world"]
```

## 类型转换

字符串可以通过类型函数转换为其他类型：

```fluxon theme={null}
"123" :: { int(this()) }      # => 123
"3.14" :: { double(this()) }  # => 3.14

# 或使用全局函数
int("123")                    # => 123
```

## 相关链接

* [List 扩展函数](/language/extensions/list)
* [Map 扩展函数](/language/extensions/map)
* [类型函数](/language/functions/type)
* [运算符与优先级](/language/operators-and-priority)
