Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you check if a string ends with a specific substring?

Is there a coldfusion string function to check if a string ends with another string? If not, what is the simplest and most efficient way to accomplish this?

like image 971
froadie Avatar asked Dec 16 '10 08:12

froadie


3 Answers

You can use the right(string, numberofcharacters) function.

example (cfscript):

existingString = "The Quick brown Fox jumps";
tailString = "umps";
stringMatch = false;
if (right(existingString, len(tailString)) eq tailString){
   stringMatch = true;
}
like image 127
Andreas Schuldhaus Avatar answered Oct 18 '22 23:10

Andreas Schuldhaus


This is where I skip down to the java level real fast.

string = "This is my fancy string";

<cfoutput>#string.endsWith("string")#</cfoutput>

This should output TRUE

More details here: http://download.oracle.com/javase/6/docs/api/java/lang/String.html#endsWith(java.lang.String)

Note that endsWith() is case sensitive.

To get around this, use LCase() or UCase(), e.g.

Ucase(string).endsWith("STRING");

Should also return TRUE

like image 22
Mark Mandel Avatar answered Oct 18 '22 21:10

Mark Mandel


A solution that I found ( http://tutorial130.easycfm.com/ ) - Use a regular expression find - REFindNoCase, with a $ sign to represent the end of the string.

REFindNoCase("end$", "check if this string ends with end")
like image 2
froadie Avatar answered Oct 18 '22 23:10

froadie