工具 / StringUtil
StringUtil Class
字符串工具
Table of contents
Methods
clipboardCopy(text: string): void client |
|---|
| 文本复制 |
clipboardPaste(): string client |
| 文本粘贴,获取剪切板的文本 |
format(str: string, ...param: any[]): string |
将{i}中的内容依次替换为后续参数。i从0开始,表示第i+2个参数,详细请查看使用示例。 |
isEmpty(str: string): boolean |
| 判断字符串是否为空 (null或"") |
maskWordCheck(text: string): Promise<[maskWordCheck](mw.StringUtil.md#maskwordcheck)Result> |
| 屏蔽字检测 |
Methods
clipboardCopy
• Static clipboardCopy(text): void client
文本复制
Parameters
text string | 复制到剪切板的文本 range: 无限制 |
|---|
将字符串复制到剪切板
使用示例:创建一个名为StringExample的脚本,放置在对象栏中,打开脚本,将原本内容修改为如下内容,保存并运行游戏,会将hello world!文本复制到剪切板,此时可以在其他地方粘贴ts
@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
StringUtil.clipboardCopy("hello world!");
}
}@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
StringUtil.clipboardCopy("hello world!");
}
}clipboardPaste
• Static clipboardPaste(): string client
文本粘贴,获取剪切板的文本
Returns
string | 剪切板的文本 |
|---|
ts
@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
let text = StringUtil.clipboardPaste();
console.log("clipboardPaste", text);
}
}@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
let text = StringUtil.clipboardPaste();
console.log("clipboardPaste", text);
}
}format
• Static format(str, ...param): string
将{i}中的内容依次替换为后续参数。i从0开始,表示第i+2个参数,详细请查看使用示例。
Parameters
str string | 要处理的字符串 range: 不做限制 |
|---|---|
...param any[] | 替换序列 |
Returns
string | 新的字符串 |
|---|
ts
@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
let targetString = StringUtil.format("{0} {1}{2}", "hello", "world", "!");
console.log(targetString);
}
}@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
let targetString = StringUtil.format("{0} {1}{2}", "hello", "world", "!");
console.log(targetString);
}
}isEmpty
• Static isEmpty(str): boolean
判断字符串是否为空 (null或"")
Parameters
str string | 要判断的字符串 range: 不做限制 |
|---|
Returns
boolean | 结果 |
|---|
ts
@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
let isEmpty1 = StringUtil.isEmpty("hello world!");
console.log("isEmpty1: " + isEmpty1);
}
}@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
let isEmpty1 = StringUtil.isEmpty("hello world!");
console.log("isEmpty1: " + isEmpty1);
}
}maskWordCheck
• Static maskWordCheck(text): Promise<maskWordCheckResult>
屏蔽字检测
Parameters
text string | 要检测的文本 range: 不做限制 |
|---|
Returns
Promise<maskWordCheckResult> | 检测结果回调 |
|---|
Precautions
接口中遇到异常情况会返回 reject,使用该接口需要用 catch 处理这种异常情况
ts
@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
StringUtil.maskWordCheck("自杀之王").then(result => {
if (!result.result) {
console.log("屏蔽字检测不通过");
console.log("命中的文本:" + result.hits);
}
}).catch(error => {
console.log("屏蔽字检测出错");
console.log(error);
});
}
}@Component
export default class StringExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): `Promise`<`void`\> {
StringUtil.maskWordCheck("自杀之王").then(result => {
if (!result.result) {
console.log("屏蔽字检测不通过");
console.log("命中的文本:" + result.hits);
}
}).catch(error => {
console.log("屏蔽字检测出错");
console.log(error);
});
}
}