如何从字符串中切分字符并返回剩余字符的计数

我有下面的字符串,我想从开头切成22个字符,并需要显示剩余字符的计数。


对于前 -红色,蓝色,ABC,测试1,REN,绿色,测试,红色 - 共 37 个字符


预期输出


红色,蓝色,ABC,测试1,ren --------------- 15


在总共 37 个字符中,22 个字符是切片字符,15 是总字符串的剩余计数。


以下是我的代码。


viewMore(text) {

    if (text.values) {

      const enumText = text.values[0];

      return enumText.slice(0, 22) + (enumText.length > 22 ? enumText.length : "");

    }   

  }

http://img.mukewang.com/632d78640001c64905870159.jpg

慕仙森
浏览 88回答 4
4回答

浮云间

您只需从原始文本长度中减去切片文本长度即可viewMore(text) {&nbsp; &nbsp; if (text.values) {&nbsp; &nbsp; &nbsp; const enumText = text.values[0];&nbsp; &nbsp; &nbsp; this.htmlToAdd = enumText.slice(0, 22) + (enumText.length > 22 ? "------<strong>("+(enumText.length - enumText.slice(0, 22).length)+")</strong>" : "");;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; }网页:<div class="one" [innerHtml]="htmlToAdd"></div>

慕后森

var text = "Red,Blue,ABC,Test1,REN,Green,Test,Red";arrayOfText = text.split('');if(arrayOfText.length > 22) {&nbsp; &nbsp; var remainingChars = arrayOfText.length - 22;&nbsp; &nbsp; console.log(text.substring(0, 22) + ' -------- ' + remainingChars);} else {&nbsp; &nbsp; console.log("");}

DIEA

试试这个,假设您只需要切片 22 个字符。我没有像您那样创建确切的数据类型,但您可以进行其余的修改并查看所需的输出。var types = 'Red,Blue,ABC,Test1,REN,Green,Test,Red';console.log(types.slice(0,22) +'-------------'+ (types.length - 22))

12345678_0001

//[with ES6] --dummyStr = "Red,Blue,ABC,Test1,REN,Green,Test,Red";chopOffPoint = 22;&nbsp;`${dummyStr.slice(0,chopOffPoint)}----------${dummyStr.length-chopOffPoint}`;// Hope this will help
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript