代码之家  ›  专栏  ›  技术社区  ›  Darren

子字符串函数类似于PHP函数?

  •  0
  • Darren  · 技术社区  · 15 年前

    有类似PHPs子串的函数吗?我看到了左边和中间,但是这些都让我指定了我想要的绳子的长度。

    以下是PHP的函数示例: $rest=substr(“abcdef”,1);//返回“b”

    我只想能从某个点开始,而不是结束。

    3 回复  |  直到 15 年前
        1
  •  4
  •   Edward M Smith    15 年前

    因为CF字符串是java字符串,所以可以使用java字符串函数

    <cfset foo="abcdef">
    <cfoutput>#foo.substring(1)#</cfoutput>
    
    returns: 'bcdef'
    

    mid(foo,2,len(foo))
    
        2
  •  1
  •   Justin Ethier    15 年前

    substring in coldfusion :

    Left(string, length) //returns number of characters defined by length
    

    Mid(string, start, count) //returns the set of characters from string, beginning at start, of length count.
    
        3
  •  0
  •   ale    15 年前

    CFLib.org 是用户定义函数的awesomest集合。

    有一个自定义项可以满足您的要求: SubStr

    “substr”,包含中间、左侧和 正确的功能 函数和添加一些 额外的功能和技巧。为了 与右(“abcdef”,2)相同- substr(“abcdef”,1,3)与 左(“abcdef”,3),-substr(“abcdef”, 4) 同时,它允许 like-substr(“abcdef”,2)代替 中(“abcdef”,2,len(“abcdef”)-2)- 在字符串结尾前2个字符, 取1个字符“-substr(“abcdef”, 最后一个字符。”它返回一个空的 索引。

    这是消息来源:

    <cfscript>
    /**
    * Returns the substring of a string. It mimics the behaviour of the homonymous php function so it permits negative indexes too.
    *
    * @param buf      The string to parse. (Required)
    * @param start      The start position index. If negative, counts from the right side. (Required)
    * @param length      Number of characters to return. If not passed, returns from start to end (if positive start value). (Optional)
    * @return Returns a string.
    * @author Rudi Roselli Pettazzi (rhodion@tiscalinet.it)
    * @version 2, July 2, 2002
    */
    function SubStr(buf, start) {
    // third argument (optional)
    var length = 0;
    var sz = 0;
    
    sz = len(buf);
    
    if (arrayLen(arguments) EQ 2) {
    
            if (start GT 0) {
             length = sz;
            } else if (start LT 0) {
             length = sz + start;
             start = 1;
            }
    
    } else {
    
            length = Arguments[3];
            if (start GT 0) {
             if (length LT 0) length = 1+sz+length-start;
            } else if (start LT 0) {
             if (length LT 0) length = length-start;
             start = 1+sz+start;
    
            }
    }
    
    if (isNumeric(start) AND isNumeric(length) AND start GT 0 AND length GT 0) return mid(buf, start, length);
    else return "";
    }
    </cfscript>
    
    推荐文章