gpt4 book ai didi

string - 如何获取字符串中的行数?

转载 作者:行者123 更新时间:2023-12-02 09:34:10 27 4
gpt4 key购买 nike

总的来说,我从 JSON 对中获取了一个包含“\n”符号的字符串。例如,

"I can see how the earth nurtures its grass,\nSeparating fine grains from lumpy earth,\nPiercing itself with its own remains\nEnduring the crawling insects on its surface.\nI can see how like a green wave\nIt lifts the soil, swelling it up,\nAnd how the roots penetrate the surrounding mulch\nHappily inhaling the air in the sky.\nI can see how the light illuminates the flowers, -\nPouring itself into their tight buds!\nThe earth and the grass – continue to grow!\nDrowning the mountains in a sea of green...\nOh, The power of motion of the young,\nThe muscular pull of the plants!\nOpening up to the planet, the sun and to you,\nBreaking through the undergrowth to the fresh spring air!"

该字符串是一首针对某幅画的诗。

现在我需要根据文本长度调整 display.newText 对象的大小。

我认为这样做的方法如下:

  1. 获取行数(“\n”的数量+1,因为哪里最后没有“\n”)
  2. 在for循环中获取最长的行
  3. 设置display.newText对象的大小。可能正在使用 fontSize 来计算系数...

问题是:如何获取行数?

最佳答案

要获取字符串中'\n'的数量,可以使用 string.gsub ,它用于字符串替换,但它也会返回匹配数作为第二个返回值。

local count  = select(2, str:gsub('\n', '\n'))

或类似:

local _, count = str:gsub('\n', '\n')

关于string - 如何获取字符串中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24690910/

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