Home
Analytics
JavaScript String.charAt(i) won't return a character
AlexAD
Hello.
As I wrote JavaScript logic I ran into a bug that having a string str="W01", str.charAt(0) returns 87 (ASCII code of the character 'W' and hence str.charAt(0) == 'W' is false. I tried the same in Chrome javascript console and had the expected result str.charAt(0) == 'W' is true. As far as I'm concerned BIRT uses Rhino and that's might have something to do with the problem. Do you have any clues?
Thank you in advance.
Find more posts tagged with
Comments
CBR
Hi Alex,
Rhino is an implementation of server side javascript. The name javascript for server side javascript is totaly missleading because it has nothing to do with client side javascript. Client side javascript was invented be Netscape to enable browser to run some code embedded in a webpage.
Server side javascript is more comparable to a full blown language that is used to build applications and has nothing in common with client side javascript (except similar syntax). This is very important to understand because it means that you cannot use Rhino to solve problems you would expect to be done in client side javascript.
Rhino is implemented in Java. charAt returns a character. In your code you are trying to compare a String with a character which will always return false because a string is not a character.
Can you try to change your code to:
if(str.charAt(0)=='W') //use simple quotes to create a character
CBR
Sorry i ve seen that you already use single quotes.<br />
<br />
Another problem is caused by Java and Rhino integration because most objects in BIRT are Java and not Rhino objects. So it is likely that the same is happening here and that you try to compare a java character with a Rhino character.<br />
<br />
See example below:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
var s = new java.lang.String("Wilhelm");
s.charAt(0)=='W'
</pre>
<br />
Code above will always return false. Because s.charAt(0) will return a java character. but =='W' will compare it with a rhino character.<br />
<br />
Try wrapping s into a Rhino String:<br />
var s = new java.lang.String("Wilhelm");<br />
s = new String(s);<br />
if(s.charAt(0)=='W') will work then.
AlexAD
Hi, Christian.<br />
<br />
Thank you, for your explanation. <br />
The string str="2012-W01" comes as a parameter where typeof str is object.<br />
Then I get strArr = str.split("-") which components are also of type object.<br />
This code snippet won't return true on that condition.<br />
By the way typeof weekPartStr is also object...<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>weekPartStr = new java.lang.String(strArr[1]);
if (weekPartStr.charAt(0) == 'W') { ... }</pre>
<br />
I guess both Rhino and V8 JavaScript engines (used in Chrome) are at first implementations of JavaScript language standardized by Standard ECMA-262 ECMAScript and therefore scripting for both should not deviate from the specification.
CBR
Hi,<br />
<br />
that's true.<br />
<br />
Can you try if it works using a Rhino String?<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
weekPartStr = new String(strArr[1]);
if(weekPartStr.charAt(0)=='W')
</pre>
AlexAD
<blockquote class='ipsBlockquote' data-author="'cbrell'" data-cid="100316" data-time="1336758307" data-date="11 May 2012 - 10:45 AM"><p>
Hi,<br />
<br />
that's true.<br />
<br />
Can you try if it works using a Rhino String?<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
weekPartStr = new String(strArr[1]);
if(weekPartStr.charAt(0)=='W')
</pre></p></blockquote>
It works, thank you, Christian.<br />
Could you explain why that doesn't work with java.lang.String and without specifying anything? Is there a way to find out the type of a variable in a script so that I know what type is used in every case?<br />
<br />
Thanks in advance.