Sub-função que é como a função PHP?

Existe uma função como substring PHPs? Eu vi esquerda e meio, mas todos me fazem especificar o comprimento do que eu quero que a corda seja.

Aqui está o exemplo de função do PHP: $rest = substr ("abcdef", 1); / / devolve "b"

Só quero ser capaz de pegar numa corda e iniciá-la a um certo ponto, e não acabar com ela também.

Author: Darren, 2010-11-05

3 answers

Dado que as cadeias de caracteres CF são cadeias de caracteres java, poderá usar as funções de cadeias de caracteres java

<cfset foo="abcdef">
<cfoutput>#foo.substring(1)#</cfoutput>

returns: 'bcdef'

Ou, se você não gosta disso, você poderia fazer (não elegante, mas funcional):

mid(foo,2,len(foo))
 4
Author: Edward M Smith, 2010-11-05 16:10:33

De substring in coldfusion:

Tenta

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

Ou

Mid(string, start, count) //returns the set of characters from string, beginning at start, of length count.
 1
Author: Justin Ethier, 2010-11-05 15:57:04

CFLib.org é a colecção awesomest de funções definidas pelo utilizador.

Existe uma UDF que faz exatamente o que você quer: SubStr

Imita o comportamento do php. "subtr", que inclui o meio, a esquerda e funcionalidades direitas num único função e adicionar algumas extra-funcionalidade e Truque. Para instância: - substr ("abcdef", -2) é o mesmo que direito ("abcdef", 2), - substr ("abcdef", 1, 3) é o mesmo que esquerda ("abcdef", 3), - subgr ("abcdef"), 2, 4) é o mesmo que mid ("abcdef", 2, 4) ao mesmo tempo, permite as coisas como-substr ("abcdef", 2) em vez de mid ("abcdef", 2, len ("abcdef") -2) - substr ("abcdef", -2, 1) para dizer "Iniciar" 2 caracteres antes do fim do texto, e leva um char."- substr ("abcdef"), -4, -1) para dizer " Iniciar 4 caracteres antes do fim da cadeia, e jogar fora o último char."Devolve um vazio cadeia em caso de incoerência Index.
Aqui está a fonte.:
<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 ([email protected])
* @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>
 0
Author: ale, 2010-11-05 17:05:38