去除字符串首尾的空格、URL 编码和引号。
| 参数 |
说明 |
类型 |
string |
清理后的字符串 |
string |
| 参数 |
说明 |
类型 |
默认值 |
str |
要清理的字符串 |
string |
无 |
options |
配置选项 |
ClearStrOption |
{} |
| 参数 |
说明 |
类型 |
默认值 |
urlencoded |
是否进行 URL 解码 |
boolean |
true |
import { clearStr } from 'ranuts';
const str = ' "hello world" ';
const cleaned = clearStr(str);
console.log(cleaned); // 'hello world'
import { clearStr } from 'ranuts';
const encoded = ' "hello%20world" ';
const cleaned = clearStr(encoded);
console.log(cleaned); // 'hello world' (自动解码)
import { clearStr } from 'ranuts';
const str = ' "hello%20world" ';
const cleaned = clearStr(str, { urlencoded: false });
console.log(cleaned); // 'hello%20world' (不解码)
import { clearStr } from 'ranuts';
const str1 = "'test'";
const str2 = '"test"';
console.log(clearStr(str1)); // 'test'
console.log(clearStr(str2)); // 'test'
- 清理内容:会移除首尾空格、单引号和双引号。
- URL 解码:默认会进行 URL 解码,可通过
urlencoded: false 禁用。
- 用途:常用于清理用户输入或从 URL 参数中提取的值。