Skip to content

Commit a51f845

Browse files
committed
1 parent a2f3b2c commit a51f845

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
# [Get the π‘Ÿπ‘’π‘Žπ‘™ length of a string](https://www.codewars.com/kata/get-the-length-of-a-string "https://www.codewars.com/kata/599c4b69eb8e49effa000079")
22

3-
In languages that use [UTF-16 encoding](https://en.wikipedia.org/wiki/UTF-16) for strings (JavaScript, JVM languages like Java, .NET languages like C#...), if the code point of a character is larger than `0xFFFF`, it will be treated as two code units.
3+
In languages that use [UTF-16 encoding](https://en.wikipedia.org/wiki/UTF-16) for strings (JavaScript, JVM languages like Java, .NET
4+
languages like C#...), if the code point of a character is larger than `0xFFFF`, it will be treated as two code units.
45

56
For example:
67

78
The code point of the emoji `πŸ™‰` (`U+1F649`, *Hear-No-Evil Monkey*) is `0x1F649`.
89

9-
```javascript
10-
'πŸ™‰'.length; // 2
11-
```
12-
1310
```java
1411
"πŸ™‰".length(); // 2
1512
```
1613

17-
```csharp
18-
"πŸ™‰".Length; // 2
19-
```
20-
2114
Write a function that returns the *real* length of a string.
2215

2316
```
2417
"abcd" --> 4
2518
"πŸ™‰" --> 1
2619
"πŸ˜ΈπŸ¦ŒπŸš€" --> 3
27-
```
20+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Kata {
2+
static int getRealLength(String str) {
3+
return str.codePointCount(0, str.length());
4+
}
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
class SolutionTest {
7+
@ParameterizedTest
8+
@CsvSource(textBlock = """
9+
'', 0
10+
abcd, 4
11+
δΈ­ε›½, 2
12+
π“ͺ𝓫𝓬𝓭, 4
13+
𨭎𩷢, 2
14+
πŸ˜ΈπŸ¦ŒπŸš€, 3
15+
↓→↑←, 4
16+
'\nabc\ndef\n', 9
17+
""")
18+
void sample(String str, int expected) {
19+
assertEquals(expected, Kata.getRealLength(str));
20+
}
21+
}

0 commit comments

Comments
Β (0)