-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathStringLength.java
53 lines (34 loc) · 1.32 KB
/
StringLength.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.zetcode;
import java.text.BreakIterator;
public class StringLength {
public static void main(String[] args) {
var text1 = "falcon";
var n1 = text1.length();
System.out.printf("%s has %d characters%n", text1, n1);
System.out.println("----------------------------");
var text2 = "вишня";
var n2 = text2.length();
System.out.printf("%s has %d characters%n", text2, n2);
System.out.println("----------------------------");
var text3 = "🐺🦊🦝";
var n3 = text3.length();
System.out.printf("%s has %d characters%n", text3, n3);
var n3_ = graphemeLength(text3);
System.out.printf("%s has %d characters%n", text3, n3_);
System.out.println("----------------------------");
var text4 = "नमस्ते";
var n4 = text4.length();
System.out.printf("%s has %d characters%n", text4, n4);
var n4_ = graphemeLength(text4);
System.out.printf("%s has %d characters%n", text4, n4_);
}
public static int graphemeLength(String text) {
BreakIterator it = BreakIterator.getCharacterInstance();
it.setText(text);
int count = 0;
while (it.next() != BreakIterator.DONE) {
count++;
}
return count;
}
}