"startswith" and "endswith" operators

ColdFusion has a contains operator, as in:

if ( value contains "foo" ) { ... }

To be fair, I almost never use this; but, I do like the sematics of it. Seems strange that we don’t have a startswith and endswith operator as well. As in:

if ( value endsWith "@bennadel.com" ) { ... }
if ( value startsWith "http://" ) { ... }

I know that the underlying java.lang.String implementation has these corresponding methods. So, it seems like it would be rather trivial to expose them as operators ?

3 Likes

You already can…

<cfscript>
    foo = "Lindsay Lohan";
    dump(foo.startsWith("Lindsay"));
</cfscript>

Yes, the .startsWith() and .endsWith() methods are part of the java.lang.String class, which is under the hood. This is one of those things that’s “kind of known”, but never explicitly documented to be OK to use. All I’m talking about is essentially taking those methods and exposing them (more or less) as CFML-native operators.

Meaning, that contains and does not contain operators are likely little more thin layers over .indexOf() in the Java layer. But, have more CFML semantics.

Plus, the Java methods are likely case-sensitive, whereas CFML operators are usually not case-sensitive.

3 Likes