如何找到第n个标记的位置

发布于 2024-07-27 15:58:36 字数 172 浏览 2 评论 0原文

我们有一个字符串,其最大长度限制为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一指流沙 2024-08-03 15:58:36

UDF ListLeft() 应该执行您想要的操作。 它接受一个列表并返回包含您定义的元素数量的列表。 “空格”作为分隔符很好。

/**
 * A Left() function for lists.  Returns the n leftmost elements from the specified list.
 * 
 * @param list      List you want to return the n leftmost elements from. 
 * @param numElements      Number of leftmost elements you want returned. 
 * @param delimiter      Delimiter for the list.  Default is the comma. 
 * @return Returns a string, 
 * @author Rob Brooks-Bilson ([email protected]) 
 * @version 1, April 24, 2002 
 */
function ListLeft(list, numElements){
  var tempList="";
  var i=0;
  var delimiter=",";
  if (ArrayLen(arguments) gt 2){
    delimiter = arguments[3];
  }
  if (numElements gte ListLen(list, delimiter)){
    return list;
  }
  for (i=1; i LTE numElements; i=i+1){
    tempList=ListAppend(tempList, ListGetAt(list, i, delimiter), delimiter);
  }
  return tempList;
}

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.

/**
 * A Left() function for lists.  Returns the n leftmost elements from the specified list.
 * 
 * @param list      List you want to return the n leftmost elements from. 
 * @param numElements      Number of leftmost elements you want returned. 
 * @param delimiter      Delimiter for the list.  Default is the comma. 
 * @return Returns a string, 
 * @author Rob Brooks-Bilson ([email protected]) 
 * @version 1, April 24, 2002 
 */
function ListLeft(list, numElements){
  var tempList="";
  var i=0;
  var delimiter=",";
  if (ArrayLen(arguments) gt 2){
    delimiter = arguments[3];
  }
  if (numElements gte ListLen(list, delimiter)){
    return list;
  }
  for (i=1; i LTE numElements; i=i+1){
    tempList=ListAppend(tempList, ListGetAt(list, i, delimiter), delimiter);
  }
  return tempList;
}

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.

早乙女 2024-08-03 15:58:36

还可以使用正则表达式(组 #1 包含匹配项):^(?:\w+\s+){19}(\w+)

Can also use a regular expression (group #1 contains match): ^(?:\w+\s+){19}(\w+)

﹉夏雨初晴づ 2024-08-03 15:58:36

也许您可以避免修剪,而是从头开始重建结果,例如(伪代码,我不知道 ColdFusion):

  result = ''
  for (i = 0; i < 20; ++i)
  {
     result = result + GetToken(myString, i, ' ');
  }

这可行吗?

Maybe you could avoid trimming and instead rebuild the result from scratch, something like (pseudo-code, I don't know ColdFusion):

  result = ''
  for (i = 0; i < 20; ++i)
  {
     result = result + GetToken(myString, i, ' ');
  }

Would that work?

话少情深 2024-08-03 15:58:36

不确定 CF 是否提供此功能,但通常有一个 LastIndexOf(string token) 方法。 将其与子字符串函数结合使用。 例如(伪代码):

string lastWord = GetToken(myString, 20, ' ');
string output = Substring(mystring, 0, LastIndexOf(mystring, lastWord)+StrLength(lastWord));

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):

string lastWord = GetToken(myString, 20, ' ');
string output = Substring(mystring, 0, LastIndexOf(mystring, lastWord)+StrLength(lastWord));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文