3 Replies Latest reply on Feb 13, 2016 3:53 AM by BKBK

    Please help me in handling java.lang.StringIndexOutOfBoundsException: String index out of range: -1 error

    Lakshmikanth S_13

      Hi all,

       

      I am very new to cfscripts.

      I am having below function  <cffunction name="newReplace" > and whenever I am calling this function I am getting java.lang.StringIndexOutOfBoundsException: String index out of range: -1 error.

      I think I missed some validation to check the null but unable to identify.

       

       

       

      <cffunction name="newReplace" returntype="string" description="Replace the original CF9 Replace() and ReplaceNoCase() which don't support some UTF-8 string">

       

        <cfargument name="stg" type="string">

       

        <cfargument name="origStg" >

       

        <cfargument name="rplStg" >

       

        <cfargument name="all" type="boolean" default=true>

       

        <cfargument name="caseSensitive" type="boolean" default=false>

       

        <cfset var result="">

       

       

       

        <cfscript>

       

        var oStringBuffer = createObject("java","java.lang.StringBuffer");

       

        var i = 0;

       

        var tmpStg = "";

       

       

       

        oStringBuffer = oStringBuffer.init(arguments.stg);

       

        while(i + origStg.length() <= oStringBuffer.length()) {

       

        tmStg = oStringBuffer.substring(i, i + origStg.length());

       

        if(tmStg.equalsIgnoreCase(origStg)) {

       

        oStringBuffer.replace(i, i + origStg.length(), rplStg);

       

        if(!all)

       

        break;

       

        else

       

        i += rplStg.length();

       

        }

       

        else

       

        i++;

       

        }

       

        result = oStringBuffer.toString();

       

        </cfscript>

       

        <cfreturn result/>

       

        </cffunction>

       

       

       

      function call:

       

      <cfset sndMsg = newReplace(sndMsg,"%EXPIRYDATE%",expiryDate,"ALL")>

       

      error:

      java.lang.StringIndexOutOfBoundsException: String index out of range: -1

      at java.lang.String.substring(Unknown Source)

      at coldfusion.runtime.StringFunc.Replace(StringFunc.java:390)

      at coldfusion.runtime.CFPage.newReplace(CFPage.java:2633)