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

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("

");
for (itmTCA in arrAccountNumberTCA){
echo(“isNumeric(#itmTCA#):” & isNumeric(itmTCA) & " (#itmTCA#)
" );
}
echo("

");

echo("

");
intTCALen = Len(arrAccountNumberTCA);
loop index=“i” from=“1” to="#intTCALen#" {
echo(“isNumeric(arrAccountNumberTCA[#i#]): " & isNumeric(
arrAccountNumberTCA[i]) & " (#arrAccountNumberTCA[i]#)
” );
}
echo("

");

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("

");
for (itmLTA in arrAccountNumberLTA){
echo(“isNumeric(#itmLTA#):” & isNumeric(itmLTA) & " (#itmLTA#)
" );
}
echo("

");

echo("

");
intLTALen = Len(arrAccountNumberLTA);
loop index=“i” from=“1” to="#intLTALen#" {
echo(“isNumeric(arrAccountNumberLTA[#i#]): " & isNumeric(
arrAccountNumberLTA[i]) & " (#arrAccountNumberLTA[i]#)
” );
}
echo("

");

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("

");
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)#)
” );
}
echo("

");

Is there a “best” way to do this?

Environment is:

Version Lucee 4.5.3.018 final

OS Mac OS X (10.11.6) 64bit
Remote IP 127.0.0.1

Servlet Container Apache Tomcat/8.0.15
Java 1.8.0_66 (Oracle Corporation) 64bit
Architecture 64bit
Thanks,

Juan

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 :slight_smile:

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

FYI

https://luceeserver.atlassian.net/browse/LDEV-955On Thu, Aug 4, 2016 at 3:54 PM, Denard Springle <@Denard_Springle> 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 :slight_smile:


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 lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/
msgid/lucee/9913b0f0-93ff-487c-ab1c-82e07b4762fb%40googlegroups.com
https://groups.google.com/d/msgid/lucee/9913b0f0-93ff-487c-ab1c-82e07b4762fb%40googlegroups.com?utm_medium=email&utm_source=footer
.

For more options, visit https://groups.google.com/d/optout.

Hello,

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

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

Thanks,
JeanOn Wed, Aug 3, 2016 at 5:31 PM, Juan Aguilar <@Juan_Aguilar> wrote:

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("

");
for (itmTCA in arrAccountNumberTCA){
echo(“isNumeric(#itmTCA#):” & isNumeric(itmTCA) & " (#itmTCA#)
" );
}
echo("

");

echo("

");
intTCALen = Len(arrAccountNumberTCA);
loop index=“i” from=“1” to="#intTCALen#" {
echo(“isNumeric(arrAccountNumberTCA[#i#]): " & isNumeric(
arrAccountNumberTCA[i]) & " (#arrAccountNumberTCA[i]#)
” );
}
echo("

");

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("

");
for (itmLTA in arrAccountNumberLTA){
echo(“isNumeric(#itmLTA#):” & isNumeric(itmLTA) & " (#itmLTA#)
" );
}
echo("

");

echo("

");
intLTALen = Len(arrAccountNumberLTA);
loop index=“i” from=“1” to="#intLTALen#" {
echo(“isNumeric(arrAccountNumberLTA[#i#]): " & isNumeric(
arrAccountNumberLTA[i]) & " (#arrAccountNumberLTA[i]#)
” );
}
echo("

");

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("

");
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)#)
” );
}
echo("

");

Is there a “best” way to do this?

Environment is:

Version Lucee 4.5.3.018 final

OS Mac OS X (10.11.6) 64bit
Remote IP 127.0.0.1

Servlet Container Apache Tomcat/8.0.15
Java 1.8.0_66 (Oracle Corporation) 64bit
Architecture 64bit
Thanks,

Juan


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 lucee@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/lucee/c691c4e0-8bf4-42db-92dd-6e1c3868dc15%40googlegroups.com
https://groups.google.com/d/msgid/lucee/c691c4e0-8bf4-42db-92dd-6e1c3868dc15%40googlegroups.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

Thanks Micha! Great work. And thanks for the clarification. :slight_smile:

– Denny