1

What I want is to calculate how much time the caret will move from the beginning till the end of the string.

Explanations:
Look this string "" in this fiddle: http://jsfiddle.net/RFuQ3/
If you put the caret before the first quote then push the right arrow you will push 3 times to arrive after the second quote (instead of 2 times for an empty string).

The first way, and the easiest to calculate the length of a string is <string>.length.
But here, it returns 2.

The second way, from JavaScript Get real length of a string (without entities) gives 2 too.

How can I get 1?


1-I thought to a way to put the string in a text input, and then do a while loop with a try{setCaret}catch(){}
2-It's just for fun

Community
  • 1
  • 1
Alexandre Khoury
  • 3,896
  • 5
  • 37
  • 58
  • Just add one to the length returned? – Brendan Lesniak Jun 30 '12 at 18:29
  • What are you actually trying to accomplish? That would probably be useful to know. Are you wanting to track cursor crawling left-to-right to detect or manipulate something? – Jared Farrish Jun 30 '12 at 18:30
  • Also, can you demonstrate that effect somewhere other than jsFiddle, which is something of a unique editing environment? In other words, regular `input` and `textarea` don't have this effect. jsFiddle's text manipulation scripts aren't perfectly tuned. – Jared Farrish Jun 30 '12 at 18:33
  • @JaredFarrish If a user push the arrow to count the length in a textbox, he will get `x`. In most cases ("foo",123,ಠ_ಠ) `length` will give x. But not in my example. – Alexandre Khoury Jun 30 '12 at 18:33
  • See my second comment. Do you have another place to demonstrate that's not jsFiddle? What kind of "cursor-based" environment do have to work with? – Jared Farrish Jun 30 '12 at 18:34
  • @JaredFarrish You are right, in jsbin the length is 2 !!! But in TextEdit (on Mac) it's 1. Ahhh! How can we know? – Alexandre Khoury Jun 30 '12 at 18:36
  • What environments are you needing to design code for? In a browser window? In a `textarea` or a `contenteditable="true"` element? That makes a difference. – Jared Farrish Jun 30 '12 at 18:46
  • The content will be in a variable. (When I was talking about textbox, it was to, maybe, use the idea I said at the bottom of my question). – Alexandre Khoury Jun 30 '12 at 18:48

3 Answers3

2

The character in your question "󠀁" is the Unicode Character 'LANGUAGE TAG' (U+E0001).

From the following Stack Overflow questions,

we learn that

JavaScript strings are UCS-2 encoded but can represent Unicode code points outside the Basic Multilingual Pane (U+0000-U+D7FF and U+E000-U+FFFF) using two 16 bit numbers (a UTF-16 surrogate pair), the first of which must be in the range U+D800-U+DFFF.

The UTF-16 surrogate pair representing "󠀁" is U+DB40 and U+DC01. In decimal U+DB40 is 56128, and U+DC01 is 56321.

console.log("".length); // 2
console.log("".charCodeAt(0)); // 56128
console.log("".charCodeAt(1)); // 56321
console.log("\uDB40\uDC01" === ""); // true
console.log(String.fromCharCode(0xDB40, 0xDC01) === ""); // true

Adapting the code from https://stackoverflow.com/a/4885062/788324, we just need to count the number of code points to arrive at the correct answer:

var getNumCodePoints = function(str) {
    var numCodePoints = 0;
    for (var i = 0; i < str.length; i++) {
        var charCode = str.charCodeAt(i);
        if ((charCode & 0xF800) == 0xD800) {
            i++;
        }
        numCodePoints++;
    }
    return numCodePoints;
};

console.log(getNumCodePoints("")); // 1

jsFiddle Demo

Community
  • 1
  • 1
creemama
  • 6,559
  • 3
  • 21
  • 26
0
function realLength(str) {
    var i = 1;
    while (str.substring(i,i+1) != "") i++;
    return (i-1);
}

Didn't try the code, but it should work I think.

JohannesB
  • 1,995
  • 21
  • 35
  • Accepting the premise of the ghost hault on the cursor, [`"2"`](http://jsfiddle.net/userdude/RFuQ3/2/) still has the issue after the first `"` (in Firefox). – Jared Farrish Jun 30 '12 at 18:28
  • @user1493235 Sorry, but it doen't work, it just return `.length-1` Look this http://jsfiddle.net/zDwPu/ (And test the real length with your caret) – Alexandre Khoury Jun 30 '12 at 18:30
  • This solution is slow (`.substring(i,i+1)` instead of `[i]`), wrong (`var i = 1;`) and useless (isn't the answer to the question). – MaxArt Jun 30 '12 at 18:39
  • No down votes for "Didn't try the code, but it should work I think." ? – user1566694 Feb 17 '16 at 22:10
0

Javascript doesn't really support unicode. You can try

yourstring.replace(/[\uD800-\uDFFF]{2}/g, "0").length

for what it's worth

panda-34
  • 4,089
  • 20
  • 25