使用Java的StringBuilder确保在字符串末尾删除密码。

huangapple go评论65阅读模式
英文:

ensure removal of password at end of string using StringBuilder with java

问题

我需要确保在这个字符串的末尾始终有一个密码时,从那里开始将密码掩盖并不显示。

请记住,最后的值不总是cards-app,它可以是不同的值,我需要掩盖从&password=的字符串末尾以掩盖密码。

如何确保使用StringBuilder来实现这一点?

示例:

https://hlg-gateway.odsus.com.br/token?grant_type=password&username=cards-app&password=cards-app;

期望的输出:

https://hlg-gateway.odsus.com.br/token?grant_type=password&username=cards-app&password=######;

这些信息显示在日志中,我只是想确保字符串的末尾被掩盖以不显示密码。

英文:

I need to ensure that always at the end of this string when there is a password, from there it leaves the password masked and does not display.

Remembering that in the end, the value cannot always be cards-app, it can be a different value, I need to mask the end of this string from &password= in order to mask the password.

How can I do this for sure using StringBuilder?

Ex.:

https://hlg-gateway.odsus.com.br/token?grant_type=password&username=cards-app&password=cards-app;

expected output:

https://hlg-gateway.odsus.com.br/token?grant_type=password&username=cards-app&password=######;

This information is displayed in the log, I just want to make sure the end of the string is masked to not display the password.

答案1

得分: 0

作为注意,_StringBuilder_ 在这里不是必需的—您可以只使用基本的字符串连接。

Output

```none
https://hlg-gateway.odsus.com.br/token?grant_type=password&username=cards-app&password=#########;

并且,没有 StringBuilder,您可以使用以下连接—代替 replace 方法调用。

string = string.substring(0, indexOfA + 1) + mask + string.substring(indexOfB);
英文:

As a note, StringBuilder is not required here—you could just use basic string concatenation.

String string = "https://hlg-gateway.odsus.com.br/token?grant_type=password&username=cards-app&password=cards-app;";
StringBuilder stringBuilder = new StringBuilder(string);
int indexOfA = string.indexOf("&password=");
indexOfA = string.indexOf('=', indexOfA);
int indexOfB = string.indexOf(';', indexOfA);
String mask = "#".repeat(indexOfB - indexOfA - 1);
stringBuilder.replace(indexOfA, indexOfB, mask);

Output

https://hlg-gateway.odsus.com.br/token?grant_type=password&username=cards-app&password=#########;

And, without the StringBuilder, you could use the following concatenation—in place of the replace method call.

string = string.substring(0, indexOfA + 1) + mask + string.substring(indexOfB);

huangapple
  • 本文由 发表于 2023年6月6日 02:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定