HighDots Forums  

Re: Zeichen =?UTF-8?B?WsOkaGxlbjogU29uZGVyemVpY2hlbiB1bmQgVW1sYXU=?= =?UTF-8?B?dGU=?=

Javascript (German) Programmiersprache JavaScript. (de.comp.lang.javascript)


Discuss Re: Zeichen =?UTF-8?B?WsOkaGxlbjogU29uZGVyemVpY2hlbiB1bmQgVW1sYXU=?= =?UTF-8?B?dGU=?= in the Javascript (German) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Zeichen =?UTF-8?B?WsOkaGxlbjogU29uZGVyemVpY2hlbiB1bmQgVW1sYXU=?= =?UTF-8?B?dGU=?= - 05-24-2005 , 09:02 AM








Martin Honnen wrote:


Quote:
Hat jemand eine Idee wie ich die Zeichen konvertieren/ zählen kann?


Dazu muesste man eine Zeichenkette per UTF-8-Kodierung in eine
Bytesequenz umwandeln und die Anzahl der Bytes in der Sequenz zaehlen.
So ich bei schnellem Ueberfliegen des Unicodestandards und der dortigen
Erklaerung der UTF-8-Kodierung das jetzt richtig verstanden habe, laesst
sich die Laenge so bestimmen:

String.prototype.getUTF8ByteSequenceLength = function () {
var length = 0;
for (var i = 0; i < this.length; i++) {
var codePoint = this.charCodeAt(i);
if (codePoint <= 0x7F) {
length += 1;
}
else if (codePoint > 0x7F && codePoint < 0x800) {
length += 2;
}
else if (codePoint >= 0x800 && codePoint <= 0xFFFF) {
length += 3;
}
else if (codePoint > 0xFFFF) {
length += 4;
}
}
return length;
}


var s = 'a1ä€';
alert(s.getUTF8ByteSequenceLength()); // shows 7

--

Martin Honnen
http://JavaScript.FAQTs.com/


Reply With Quote
  #2  
Old   
Dietmar Meier
 
Posts: n/a

Default =?utf-8?Q?Re:_Zeichen_Z=C3=A4hlen:_Sonderzeiche?==?utf-8?Q?n_und_Umlaute?= - 05-24-2005 , 09:39 AM






Martin Honnen wrote:

Quote:
String.prototype.getUTF8ByteSequenceLength = function () { [...]
Etwas kompakter:

String.prototype.getUTF8ByteSequenceLength = function () {
for (var i = 0, l = 0; i < this.length; i++) {
var c = this.charCodeAt(i);
l += 1 + (c > 0x7F) + (c > 0x7FF) + (c > 0xFFFF);
}
return l;
}

ciao, dhgm


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.