Unexpected difference between String.toCharArray() and String.listToArray("") when using isNumeric()

61 views
Skip to first unread message

Juan Aguilar

unread,
Aug 3, 2016, 8:31:08 PM8/3/16
to Lucee
I have to loop over a string to evaluate each character in the string and determine whether each character is numeric. In converting the string to an array, I came across some odd behavior:

When I use toCharArray() and then use a for loop over the array, isNumeric() returns false for each value. But if I use a from-to loop and call the array item by index, it evaluates properly:

strAccountNumber = "8B345-7890";

arrAccountNumberTCA
= strAccountNumber.toCharArray();
dump(var=arrAccountNumberTCA, label="strAccountNumber.toCharArray()");

echo
("<p>");
for (itmTCA in arrAccountNumberTCA){
 echo
("isNumeric(#itmTCA#):" & isNumeric(itmTCA) & " (#itmTCA#)<br/>" );
}
echo
("</p>");

echo
("<p>");
intTCALen
= Len(arrAccountNumberTCA);
loop index
="i" from="1" to="#intTCALen#" {
 echo
("isNumeric(arrAccountNumberTCA[#i#]): " & isNumeric(arrAccountNumberTCA[i]) & " (#arrAccountNumberTCA[i]#)<br/>" );
}
echo
("</p>");

Oddly, if I use listToArray("") instead, neither the for loop nor the from-to loops return the expected values from isNumeric():

strAccountNumber = "8B345-7890";

arrAccountNumberLTA
= strAccountNumber.listToArray("");
dump(var=arrAccountNumberLTA, label="strAccountNumber.listToArray("""")");

echo
("<p>");
for (itmLTA in arrAccountNumberLTA){
 echo
("isNumeric(#itmLTA#):" & isNumeric(itmLTA) & " (#itmLTA#)<br/>" );
}
echo
("</p>");

echo
("<p>");
intLTALen
= Len(arrAccountNumberLTA);
loop index
="i" from="1" to="#intLTALen#" {
 echo
("isNumeric(arrAccountNumberLTA[#i#]): " & isNumeric(arrAccountNumberLTA[i]) & " (#arrAccountNumberLTA[i]#)<br/>" );
}
echo
("</p>");

Is this a bug, expected behavior, or am I going about this the wrong way? I guess I could just use Mid without converting the string to an array:

strAccountNumber = "8B345-7890";

echo
("<p>");
intStrLen
= Len(strAccountNumber);
loop index
="i" from="1" to="#intStrLen#" {
 echo
("isNumeric(Mid(strAccountNumber, #i#, 1)): " & isNumeric(Mid(strAccountNumber, i, 1)) & " (#Mid(strAccountNumber, i, 1)#)<br/>" );
}
echo
("</p>");

Is there a "best" way to do this?

Environment is:

VersionLucee 4.5.3.018 final


OSMac OS X (10.11.6) 64bit
Remote IP127.0.0.1


Servlet ContainerApache Tomcat/8.0.15
Java1.8.0_66 (Oracle Corporation) 64bit
Architecture64bit

Thanks,

Juan

Jean Moniatte

unread,
Aug 4, 2016, 12:40:04 AM8/4/16
to lu...@googlegroups.com
Hello,

what about using a regular expression to make sure that each character is numeric?

reFind("^([0-9])$",strAccountNumber)

Thanks,
Jean

--
Get 10% off of the regular price for this years CFCamp in Munich, Germany (Oct. 20th & 21st) with the Lucee discount code Lucee@cfcamp. 189€ instead of 210€. Visit https://ti.to/cfcamp/cfcamp-2016/discount/Lucee@cfcamp
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/c691c4e0-8bf4-42db-92dd-6e1c3868dc15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Denard Springle

unread,
Aug 4, 2016, 9:49:22 AM8/4/16
to Lucee
toCharArray() is a java string method, so it converts all of the numbers to strings.

listToArray( strAccountNumber, '' )

works in ACF & Lucee

strAccountNumber.listToArray('')

does not work in Lucee, but does in ACF

I'd say that's a bug and I'd suggest filing a bug report for that.

In the meantime, to get you going, however, I'd just swap around to using the function listToArray() instead of the member function


Denard Springle

unread,
Aug 4, 2016, 9:54:44 AM8/4/16
to Lucee
And by 'does not work in Lucee' I meant that it produces 'no' for isNumeric() when using the listToArray() member function, to clear up any confusion :)

Michael Offner

unread,
Aug 4, 2016, 2:38:56 PM8/4/16
to lucee
toCharArray is a method of the class String that is returning a char array, not a string array
so question is should a char primitive type or a java.lang.Character that contains a number be interpreted as numeric.

when i run this in acf and lucee:
c=createObject('java','java.lang.Character').init('1');
writedump(isNumeric(c));
writedump(isNumeric(c&""));

 both engines give me the same result:
false
true

so both interpret a character not as numeric even it contains a number, but they can convert it to a number.
in my case this is a bug in both engines we should address.

but coming back to your problem at hand. the easiest way to do it is this:
strAccountNumber = "8B345-7890";
length=strAccountNumber.len();
for(i=1;i<=length;i++) {
digit=strAccountNumber[i];
echo("isNumeric(#digit#):" & isNumeric(digit) & "<br/>" );
}
// or
loop from=1 to=strAccountNumber.len() index="i" {
digit=strAccountNumber[i];
echo("isNumeric(#digit#):" & isNumeric(digit) & "<br/>" );
}

you can address a string like an array

Micha





--
Get 10% off of the regular price for this years CFCamp in Munich, Germany (Oct. 20th & 21st) with the Lucee discount code Lucee@cfcamp. 189€ instead of 210€. Visit https://ti.to/cfcamp/cfcamp-2016/discount/Lucee@cfcamp
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+unsubscribe@googlegroups.com.

Michael Offner

unread,
Aug 4, 2016, 2:43:35 PM8/4/16
to lucee

On Thu, Aug 4, 2016 at 3:54 PM, Denard Springle <denard....@gmail.com> wrote:
And by 'does not work in Lucee' I meant that it produces 'no' for isNumeric() when using the listToArray() member function, to clear up any confusion :)

--
Get 10% off of the regular price for this years CFCamp in Munich, Germany (Oct. 20th & 21st) with the Lucee discount code Lucee@cfcamp. 189€ instead of 210€. Visit https://ti.to/cfcamp/cfcamp-2016/discount/Lucee@cfcamp
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.

Juan Aguilar

unread,
Aug 4, 2016, 3:52:49 PM8/4/16
to lu...@googlegroups.com
Thanks so much, Micha. 

Now that you mention it, I think I remember seeing the "string like an array" in one of your video presentations (but I can't remember for sure.) I was looking all over but was searching for a blog post or thread not a video.

Thanks again,

Juan

You received this message because you are subscribed to a topic in the Google Groups "Lucee" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lucee/oHoKmaSxlGc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lucee+unsubscribe@googlegroups.com.

To post to this group, send email to lu...@googlegroups.com.

Michael Offner

unread,
Aug 4, 2016, 3:58:55 PM8/4/16
to lucee
@Juan np
in meantime i also fixed the issue with characters, character are now handled the same way as strings.

@Denard i also fixed that listToArray return a character array in that case.

new version is building atm

Micha


Denard Springle

unread,
Aug 5, 2016, 4:07:24 PM8/5/16
to Lucee
Thanks Micha! Great work. And thanks for the clarification. :)

-- Denny
Reply all
Reply to author
Forward
0 new messages