问个初学问题

照着网上做了个输入文本框,想问个初级点的问题
追加文本用的 this.testMsg_Label.string += this.edtMsg_EditBox.string + ‘\n’;
如果只保留若干行文本,前面输入的字符串怎么去掉?
是搞个数组[]push进去再把它去掉吗?

properties: {
testMsg_Label: cc.Label,
testMsg_scrollView: cc.ScrollView,
edtMsg_EditBox: cc.EditBox,
},

addMsg(msg) {
    if (this.edtMsg_EditBox.string != '') {
        this.testMsg_Label.string += this.edtMsg_EditBox.string + '\n';
        this.edtMsg_EditBox.string = '';
    }
},

没有自动换行的话,可以考虑正则通过 \n 实现或者通过indexOf("\n")拿到索引,然后通过以下API操作。

语法
str.substring(indexStart[, indexEnd])
参数
indexStart
需要截取的第一个字符的索引,该索引位置的字符作为返回的字符串的首字母。
indexEnd
可选。一个 0 到字符串长度之间的整数,以该数字为索引的字符不包含在截取的字符串内。
返回值
包含给定字符串的指定部分的新字符串。

但是indexOf只会拿到第一个的匹配索引,所以需要几行就需要重复操作几次。
正则表达式则需要你查找正则公式了~

我自己想了个思路解决了,
做个set()
每次用newMsg=‘文本’ 进行赋值的时候用push保存文本段落,在超出文本段落大小的时候去掉之前的数组值
自我感觉挺方便的

    properties: {
    testMsg_Label: cc.Label,
    newMsgArr: [],
    newMsg: {
        set(msg) {
            if (this.newMsgArr.length > 5) {
                this.newMsgArr.shift();
            }
            this.newMsgArr.push(msg);
            let tempMsg = '';
            for (let g of this.newMsgArr) {
                tempMsg += g + '\n';
            }
            this.testMsg_Label.string = tempMsg;
        }
    },
}

我在addMsg里面直接给newMsg赋值就行了

addMsg() {
if (this.edtMsg_EditBox.string != '') {
  this.newMsg=this.edtMsg_EditBox.string;
  this.edtMsg_EditBox.string = '';
}