如何找到第n个标记的位置
我们有一个字符串,其最大长度限制为 20 个单词。 如果用户输入的内容超过 20 个单词,我们需要在第 20 个单词处截断字符串。 我们如何才能实现自动化? 我们可以使用 #GetToken(myString, 20, ' ')# 找到第 20 个标记,但不确定如何找到它的位置以便向左修剪。 有任何想法吗? 提前致谢。
We have a string that has a maximum limit of 20 words. If the user enters something that is more than 20 words, we then need to truncate the string at its 20th word. How can we automate this? We are able to find the 20th token with #GetToken(myString, 20, ' ')#, but are unsure on how to find it's position in order to left-trim. Any ideas?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
UDF ListLeft() 应该执行您想要的操作。 它接受一个列表并返回包含您定义的元素数量的列表。 “空格”作为分隔符很好。
ps CFLIB.org 是一个出色的资源,通常是我寻找此类内容时的第一站。 我强烈推荐它。
The UDF ListLeft() should do what you want. It takes a list and returns the list with the number of elements you define. "Space" is fine as a delimiter.
p.s. CFLIB.org is an outstanding resource, and is usually my first stop when I'm looking for something like this. I recommend it highly.
还可以使用正则表达式(组 #1 包含匹配项):
^(?:\w+\s+){19}(\w+)
Can also use a regular expression (group #1 contains match):
^(?:\w+\s+){19}(\w+)
也许您可以避免修剪,而是从头开始重建结果,例如(伪代码,我不知道 ColdFusion):
这可行吗?
Maybe you could avoid trimming and instead rebuild the result from scratch, something like (pseudo-code, I don't know ColdFusion):
Would that work?
不确定 CF 是否提供此功能,但通常有一个 LastIndexOf(string token) 方法。 将其与子字符串函数结合使用。 例如(伪代码):
Not sure if CF provides this, but generally there is a LastIndexOf(string token) method. Use that combined with a substring function. For isntance (psuedocode):