• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Nov 12, 2015 Nov 12, 2015

Copy link to clipboard

Copied

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)

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 12, 2015 Nov 12, 2015

Copy link to clipboard

Copied

Well, for one thing, you want to replace "<=" with "lte".  Even though CFSCRIPT is a lot like JavaScript, you still have to use CF conditionals and concatenations, although + will still do math.

I don't really understand exactly what you're doing with this, and I've never heard of Replace() or ReplaceNoCase() having any UTF-8 issues, but this should at least get you going in the right direction.

HTH,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 11, 2016 Feb 11, 2016

Copy link to clipboard

Copied

I ran into a similar issue using ColdFusion 9/Windows.  Using rereplacenocase(textMessage, "[hash]", newValue, "all") was throwing a "String index out of range: -1" error on a value returned from a MSSQL nvarchar datatype.  (If I changed the word "hash" to anything else it would work, but possibly because it wasn't finding a match.)  I copied the original "textMessage" value out of the database, pasted it into my text editor, copied it back and pasted it into the database and ran the script again and then it worked. I think replacenocase() may have issues with UTF-8 or possibly extended-ASCII invisible characters.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2016 Feb 13, 2016

Copy link to clipboard

Copied

LATEST

Lakshmikanth S_13 wrote:

function call:

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

Two fatal mistakes:

1) Invoking the function recursively with the variable sndMsg sends it into an infinite loop.

2) The last argument should be a boolean (True or False) rather than a string ("ALL").

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation