2021-07-16 07:31:17 +08:00
|
|
|
## 字符串扩展函数
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
### 1.ulen
|
2021-07-16 07:31:17 +08:00
|
|
|
|
|
|
|
> 获取 unicode 字符个数。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
ulen(str) => int
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
print(ulen('致远电子'))
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
### 2.char\_at
|
2021-07-16 07:31:17 +08:00
|
|
|
|
|
|
|
> 获取指定位置的字符。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
char_at(str, index) => str
|
|
|
|
```
|
|
|
|
|
2021-07-19 18:32:35 +08:00
|
|
|
> index 为负数时,从后往前取。比如-1 表示最后一个元素。
|
|
|
|
|
2021-07-16 07:31:17 +08:00
|
|
|
#### 示例
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
print(char_at('致远电子', 1))
|
|
|
|
print(char_at('ZLG', 1))
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
### 3.index\_of
|
2021-07-16 07:31:17 +08:00
|
|
|
|
|
|
|
> 查找指定子串的位置,如果没有找到返回-1。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
index_of(str, substr) => int
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
print(index_of('hello AWTK and AWTK', 'AWTK'))
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
### 4.last\_index\_of
|
2021-07-16 07:31:17 +08:00
|
|
|
|
|
|
|
> 反向查找指定子串的位置,如果没有找到返回-1。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
last_index_of(str, substr) => int
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
print(last_index_of('hello AWTK and AWTK', 'AWTK'))
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
### 5.trim\_left
|
2021-07-16 07:31:17 +08:00
|
|
|
|
|
|
|
> 去掉字符串左边的空白字符。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
trim_left(str) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
print(trim_left(' AWTK '))
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
### 6.trim\_right
|
2021-07-16 07:31:17 +08:00
|
|
|
|
|
|
|
> 去掉字符串右边的空白字符。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
trim_right(str) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
print(trim_right(' AWTK ') + "1234")
|
|
|
|
```
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
### 7.totitle
|
2021-07-16 07:31:17 +08:00
|
|
|
|
|
|
|
> 将字符串中单词的首字母转换成大写。
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
#### 原型
|
2021-07-16 07:31:17 +08:00
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
totitle(str) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
print(totitle("it is ok!"))
|
|
|
|
```
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
### 8.prompt
|
2021-07-16 07:31:17 +08:00
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
> 在控制台提示用户输入字符串(仅当定义了 HAS\_STIOD 时有效)。
|
2021-07-16 07:31:17 +08:00
|
|
|
----------------------------
|
|
|
|
|
2021-11-22 16:39:37 +08:00
|
|
|
#### 原型
|
2021-07-16 07:31:17 +08:00
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-16 07:31:17 +08:00
|
|
|
prompt(tips) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2022-08-23 10:34:55 +08:00
|
|
|
var a = prompt('input a number:');
|
2021-07-16 07:31:17 +08:00
|
|
|
print(int(a)+100);
|
|
|
|
```
|
2021-07-19 18:32:35 +08:00
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
### 9.usubstr
|
2021-07-19 18:32:35 +08:00
|
|
|
|
|
|
|
> 取子字符串(unicode)。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2021-07-19 18:32:35 +08:00
|
|
|
usubstr(str, start, end) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
> * 不包括end
|
|
|
|
> * start 为负数时,从后往前取。比如-1 表示最后一个元素。
|
|
|
|
> * end 为负数时,从后往前取。比如-1 表示最后一个元素。
|
|
|
|
> * end 省略表示到最后全部元素。
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
2022-08-23 18:27:17 +08:00
|
|
|
var a="致远电子"
|
2022-08-23 10:12:36 +08:00
|
|
|
assert(usubstr(a) == '致远电子');
|
|
|
|
assert(usubstr(a,0) == '致远电子');
|
|
|
|
assert(usubstr(a,0,1) == '致');
|
|
|
|
assert(usubstr(a,0,-1) == '致远电');
|
|
|
|
assert(usubstr(a,-2,-1) == '电');
|
|
|
|
assert(usubstr(a, -3) == '远电子');
|
|
|
|
```
|
2021-07-19 18:32:35 +08:00
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
### 10.text\_reverse
|
2022-01-01 18:47:50 +08:00
|
|
|
|
|
|
|
> 字符串反向
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2022-01-01 18:47:50 +08:00
|
|
|
text_reverse(str) => str
|
|
|
|
```
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
print(text_reverse("hello"));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 11.text\_count
|
2022-01-01 18:47:50 +08:00
|
|
|
|
|
|
|
> 统计子串出现的次数。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
2022-08-23 10:12:36 +08:00
|
|
|
```js
|
2022-01-01 18:47:50 +08:00
|
|
|
text_count(str, substr) => int
|
|
|
|
```
|
2022-08-23 10:12:36 +08:00
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
print(text_count("hello zlg!", "zlg"));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 12.str\_len
|
|
|
|
|
|
|
|
> 计算字符串的长度。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
str_len(str) => uint32
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
print(str_len("hello"));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 13.str\_is\_empty
|
|
|
|
|
|
|
|
> 判断字符串是否为空。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
str_is_empty(str) => bool
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
print(str_is_empty(""));
|
|
|
|
print(str_is_empty("zlg"));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 14.str\_append
|
|
|
|
|
|
|
|
> 追加字符串。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
str_append(str, append_str) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
print(str_append("hello ", "zlg"));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 15.char\_at\_first
|
|
|
|
|
|
|
|
> 获取首字符。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
char_at_first(str) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
print(char_at_first("hello"));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 16.char\_at\_last
|
|
|
|
|
|
|
|
> 获取尾字符。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
char_at_last(str) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
print(char_at_last("hello"));
|
|
|
|
```
|
|
|
|
|
|
|
|
### 17.char\_at\_random
|
|
|
|
|
|
|
|
> 获取随机位置的字符。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
char_at_random(str) => str
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
print(char_at_random("hello"));
|
|
|
|
```
|
2024-10-09 11:55:32 +08:00
|
|
|
|
|
|
|
### 18.str_start_with
|
|
|
|
|
|
|
|
> 检查字符串是否以指定的字符串开头。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
str_start_with(str, prefix) => bool
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
str_start_with('AWTK', 'AW')
|
|
|
|
```
|
|
|
|
|
|
|
|
### 19.str_end_with
|
|
|
|
|
|
|
|
> 检查字符串是否以指定的字符串结尾。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```js
|
|
|
|
str_end_with(str, prefix) => bool
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
str_end_with('AWTK', 'TK')
|
|
|
|
```
|