Skip to content

Commit 026c778

Browse files
authored
[mask-string-except-last-4] mask str (#17661)
* [mask-string-except-last-4] mask str * [mask-string-except-last-4] move to a new module
1 parent d57b9eb commit 026c778

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
### Relevant Articles:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>core-java-string-operations-11</artifactId>
5+
<packaging>jar</packaging>
6+
<name>core-java-string-operations-11</name>
7+
<parent>
8+
<groupId>com.baeldung.core-java-modules</groupId>
9+
<artifactId>core-java-modules</artifactId>
10+
<version>0.0.1-SNAPSHOT</version>
11+
</parent>
12+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.maskstring;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class MaskStringUnitTest {
10+
11+
private static final String INPUT_1 = "a b c d 1234";
12+
private static final String INPUT_2 = "a b c d ";
13+
private static final String INPUT_3 = "a b";
14+
15+
private static final String EXPECTED_1 = "********1234";
16+
private static final String EXPECTED_2 = "******** ";
17+
private static final String EXPECTED_3 = "a b";
18+
19+
public String maskByCharArray(String input) {
20+
if (input.length() <= 4) {
21+
return input;
22+
}
23+
char[] chars = input.toCharArray();
24+
Arrays.fill(chars, 0, chars.length - 4, '*');
25+
return new String(chars);
26+
}
27+
28+
@Test
29+
void whenUsingCharArray_thenCorrect() {
30+
assertEquals(EXPECTED_1, maskByCharArray(INPUT_1));
31+
assertEquals(EXPECTED_2, maskByCharArray(INPUT_2));
32+
assertEquals(EXPECTED_3, maskByCharArray(INPUT_3));
33+
}
34+
35+
public String maskBySubstring(String input) {
36+
if (input.length() <= 4) {
37+
return input;
38+
}
39+
String toMask = input.substring(0, input.length() - 4);
40+
String keepPlain = input.substring(input.length() - 4);
41+
return toMask.replaceAll(".", "*") + keepPlain;
42+
}
43+
44+
@Test
45+
void whenUsingSubstring_thenCorrect() {
46+
assertEquals(EXPECTED_1, maskBySubstring(INPUT_1));
47+
assertEquals(EXPECTED_2, maskBySubstring(INPUT_2));
48+
assertEquals(EXPECTED_3, maskBySubstring(INPUT_3));
49+
}
50+
51+
public String maskByRegex(String input) {
52+
if (input.length() <= 4) {
53+
return input;
54+
}
55+
return input.replaceAll(".(?=.{4})", "*");
56+
}
57+
58+
@Test
59+
void whenUsingRegex_thenCorrect() {
60+
assertEquals(EXPECTED_1, maskByRegex(INPUT_1));
61+
assertEquals(EXPECTED_2, maskByRegex(INPUT_2));
62+
assertEquals(EXPECTED_3, maskByRegex(INPUT_3));
63+
}
64+
65+
public String maskByRepeat(String input) {
66+
if (input.length() <= 4) {
67+
return input;
68+
}
69+
int maskLen = input.length() - 4;
70+
return "*".repeat(maskLen) + input.substring(maskLen);
71+
}
72+
73+
@Test
74+
void whenUsingRepeat_thenCorrect() {
75+
assertEquals(EXPECTED_1, maskByRepeat(INPUT_1));
76+
assertEquals(EXPECTED_2, maskByRepeat(INPUT_2));
77+
assertEquals(EXPECTED_3, maskByRepeat(INPUT_3));
78+
}
79+
}

core-java-modules/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
<module>core-java-string-operations-8</module>
218218
<module>core-java-string-operations-9</module>
219219
<module>core-java-string-operations-10</module>
220+
<module>core-java-string-operations-11</module>
220221
<module>core-java-regex</module>
221222
<module>core-java-regex-2</module>
222223
<module>core-java-regex-3</module>

0 commit comments

Comments
 (0)