gpt4 book ai didi

javascript - JavaScript 中第一行文本的长度

转载 作者:行者123 更新时间:2023-12-02 18:01:49 24 4
gpt4 key购买 nike

如果我要将一段文本加载到变量中,我想计算第一行的宽度。这是我正在尝试使用的变量中的文本 block 的值:

Wine comes in at the mouth
And love comes in at the eye;
That's all we shall know for truth
Before we grow old and die.
I lift the glass to my mouth,
I look at you, and I sigh.
--W.B. Yeats

结果应该是 26。我如何获取第一行的长度?

最佳答案

获取第一个换行符的索引,即第一行的长度:

var len = text.indexOf('\n');

关于javascript - JavaScript 中第一行文本的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20460582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com