Java Coding: How To Find Number of Characters in a String (!= String.length()) Codage Java: Comment trouver nombre de caractères dans une chaîne (! = String.length ())
Length of string can be interpreted variously - Longueur de chaîne peut être interprété diversement --
- number of chars in the string nombre de caractères dans la chaîne
- number of characters in the string nombre de caractères dans la chaîne
- number of bytes in the string nombre d'octets dans la chaîne
String.length() gives you the number of chars in the string accurately. String.length () vous donne le nombre de caractères dans la chaîne de précision.
However a char is not necessarily a complete character. Toutefois, un char n'est pas nécessairement un caractère complet. Why? Pourquoi?
Supplementary characters exist in the Unicode charset. Caractères supplémentaires existent dans le jeu de caractères Unicode. These are characters that have code points above the base set, and they have values greater than 0xFFFF. Ces sont des personnages qui ont points de code au-dessus de la base, et ils ont des valeurs supérieures à 0xFFFF. They extend all the way up to 0×10FFFF. Elles s'étendent sur toute la hauteur à 0 × 10FFFF.
In Java, these supplementary characters are represented as surrogate pairs, pairs of char units that fall in a specific range. En Java, ces caractères supplémentaires sont représentés comme des paires de substitution, des paires de char unités qui entrent dans une gamme spécifique. The leading or high surrogate value is in the 0xD800 through 0xDBFF range. Le chef de file ou de substitution de haute valeur est dans l'0xD800 par 0xDBFF gamme. The trailing or low surrogate value is in the 0xDC00 through 0xDFFF range. La moyenne mobile ou à faible valeur de substitution est en 0xDC00 par 0xDFFF gamme.
J2SE 5.0 API has a new String method: codePointCount(int beginIndex, int endIndex) which tells you how many Unicode code points are between the two indices. API J2SE 5,0 a une nouvelle méthode String: codePointCount (int beginIndex, int endIndex) qui vous indique le nombre de points de code Unicode sont entre les deux indices. The index values refer to code unit or char locations, so endIndex - beginIndex for the entire String is equivalent to the String’s length. Les valeurs de l'indice de référence ou code unité char endroits, afin endIndex - beginIndex pour l'ensemble à cordes est l'équivalent de la chaîne de dépendance.
So: Donc:
int characterLength = myString.codePointCount(0, charLength); int characterLength = myString.codePointCount (0, charLength);
As before: Comme par le passé:
int charLength = myString.length(); int charLength = myString.length ();
Unless you plan to sell your software to China or Japan (read internationalize) you are unlikely to encounter any difference between charLength and characterLength. À moins que vous envisagez de vendre votre logiciel en Chine ou au Japon (lire l'internationalisation), vous ne risquent pas de rencontrer toute différence entre charLength et characterLength.
So how many bytes are in a String? Donc, combien d'octets sont dans un string?
int byteCount = myString.getBytes().length; int byteCount = myString.getBytes (). longueur;
getBytes converts its Unicode characters into a legacy charset with the exception of UTF-8 which is a multibyte encoding of Unicode and not a legacy charset. getBytes convertit ses caractères Unicode dans un héritage de caractères à l'exception de l'UTF-8 qui est un multi-encodage d'Unicode et non un héritage de caractères. It then returns the characters in a byte array. Il retourne ensuite les personnages dans un tableau d'octets.
Filed under Classé sous Headline News Headline News , How To Comment , Java Software Logiciel de Java , Tech Note Note technique | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | cet article |
Email this Article Envoyer cet article
You may also like to read Vous mai également à lire |





































November 11th, 2005 at 11:12 am Novembre 11, 2005 at 11:12 am
So you saying that you should always use “int byteCount = myString.getBytes().length;” instead of “myString.length()”, just in case you Internationalize later? Donc, vous dites que vous devez toujours utiliser "int byteCount = myString.getBytes (). Longueur;" au lieu de "myString.length ()", juste au cas où vous Internationaliser plus tard?
August 30th, 2007 at 1:26 am Août 30th, 2007 at 1:26 am
Is there an example for this. Existe-t-il un exemple pour illustrer cela.
August 30th, 2007 at 3:12 am Août 30th, 2007 at 3:12 am
Yes, Jason. Oui, Jason.
August 30th, 2007 at 3:13 am Août 30th, 2007 at 3:13 am
I gave the example code in the article. J'ai donné l'exemple dans l'article. What other examples are you looking for? Quels sont les autres exemples recherchez-vous?
October 1st, 2007 at 3:41 pm Octobre 1st, 2007 at 3:41 pm
Is there a way to filter out the punctuation similar to setting delimiters to where the program only counts the letters? Existe-t-il un moyen de filtrer la ponctuation similaire à la mise en délimiteurs de l'endroit où le programme compte seulement les lettres?
April 1st, 2008 at 11:13 pm 1er avril 2008 à 11:13 pm
how can i find a length of string with out using any function in java.. Comment puis-je trouver une longueur de chaîne avec l'aide de toute fonction en java .. plz help me.. plz help me ..