Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7832e60

Browse files
hide/truncate snippets
1 parent aa4cd5f commit 7832e60

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Mask Text
3+
description: Masks portions of a string, leaving specific parts at the beginning and end visible while replacing the rest with a specified character
4+
author: Mcbencrafter
5+
tags: string,mask,hide
6+
---
7+
8+
```java
9+
public static String partialMask(String text, int maskLengthStart, int maskLengthEnd, char mask)
10+
if (text == null)
11+
return null;
12+
13+
StringBuilder maskedText = new StringBuilder();
14+
maskedText.append(text, 0, maskLengthStart);
15+
16+
for (int currentChar = maskLengthStart; currentChar < text.length(); currentChar++) {
17+
maskedText.append(mask);
18+
}
19+
maskedText.append(text, text.length() - maskLengthEnd, text.length());
20+
return maskedText.toString();
21+
}
22+
23+
// Usage:
24+
System.out.println(partialMask("1234567890", 4, 2, '*')); // "1234****90"
25+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Truncate String
3+
description: Truncates a string after a specified length (can also be used for hiding information)
4+
author: Mcbencrafter
5+
tags: string,truncate,mask,hide
6+
---
7+
8+
```java
9+
public static String truncate(String text, int length, String suffix) {
10+
if (text.length() <= length)
11+
return text;
12+
13+
return text.substring(0, length).trim() + (suffix != null ? suffix : "");
14+
}
15+
16+
// Usage:
17+
System.out.println(truncate("hello world", 5, "...")); // "hello..."
18+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /