Skip to content Skip to sidebar Skip to footer

How To Calculate The Length Of An Encoded String?

so i have an arabic string then i encode it using encodeURIComponent, then i try to know the length from the encoded string but this code don't work why? http://jsfiddle.net/mCwaj

Solution 1:

You are passing the non-encoded str to your function instead of encd. Therefore, the regex does not match and the result null throws an exception on accessing its length property.


Solution 2:

try using "escape()" instead of "encodeURIComponent()"

//14 charachters
var str="مرحبا أنا ياسر";  

var result=custom_length(escape(str));

alert(result); //it'll display 14

function custom_length(str){
   var tab=str.match(/%../g);
   return tab.length;
}

Demo:http://jsfiddle.net/ysinjab/KDyhp/


Post a Comment for "How To Calculate The Length Of An Encoded String?"