Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/String-length/Java/string-length-1.java
Normal file
4
Task/String-length/Java/string-length-1.java
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
String s = "Hello, world!";
|
||||
int byteCountUTF16 = s.getBytes("UTF-16").length; // Incorrect: it yields 28 (that is with the BOM)
|
||||
int byteCountUTF16LE = s.getBytes("UTF-16LE").length; // Correct: it yields 26
|
||||
int byteCountUTF8 = s.getBytes("UTF-8").length; // yields 13
|
||||
2
Task/String-length/Java/string-length-2.java
Normal file
2
Task/String-length/Java/string-length-2.java
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
String s = "Hello, world!";
|
||||
int not_really_the_length = s.length(); // XXX: does not (always) count Unicode characters (code points)!
|
||||
3
Task/String-length/Java/string-length-3.java
Normal file
3
Task/String-length/Java/string-length-3.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
String str = "\uD834\uDD2A"; //U+1D12A
|
||||
int not_really__the_length = str.length(); // value is 2, which is not the length in characters
|
||||
int actual_length = str.codePointCount(0, str.length()); // value is 1, which is the length in characters
|
||||
19
Task/String-length/Java/string-length-4.java
Normal file
19
Task/String-length/Java/string-length-4.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import java.text.BreakIterator;
|
||||
|
||||
public class Grapheme {
|
||||
public static void main(String[] args) {
|
||||
printLength("møøse");
|
||||
printLength("𝔘𝔫𝔦𝔠𝔬𝔡𝔢");
|
||||
printLength("J̲o̲s̲é̲");
|
||||
}
|
||||
|
||||
public static void printLength(String s) {
|
||||
BreakIterator it = BreakIterator.getCharacterInstance();
|
||||
it.setText(s);
|
||||
int count = 0;
|
||||
while (it.next() != BreakIterator.DONE) {
|
||||
count++;
|
||||
}
|
||||
System.out.println("Grapheme length: " + count+ " " + s);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue