跳到主要内容

GetCurrentWord

返回当前单词或当前单词的一部分。

语法

expression.GetCurrentWord(sWordPart);

expression - 表示 ApiDocument 类的变量。

参数

名称必需/可选数据类型默认值描述
sWordPart必需undefined | "before" | "after"要返回的当前单词的指定部分。

返回值

string

示例

读取文档中光标位置处的单词,包括光标前后的文本。

// How do I get the word the cursor is currently placed on in a document?

// Split the word at the cursor to inspect its left and right portions separately in a document.

let doc = Api.GetDocument();
let para1 = doc.GetElement(0);
para1.AddText("The quick brown ");
let run = para1.AddText("fox");
para1.AddText(" jumps over the lazy dog");

run.MoveCursorToPos(1);

let para2 = Api.CreateParagraph();
para2.AddText("The current word is " + doc.GetCurrentWord());
doc.Push(para2);

para2 = Api.CreateParagraph();
para2.AddText("The part of the word after cursor is " + doc.GetCurrentWord("after"));
doc.Push(para2);

para2 = Api.CreateParagraph();
para2.AddText("The part of the word before cursor is " + doc.GetCurrentWord("before"));
doc.Push(para2);