英文:
ensure removal of password at end of string using StringBuilder with java
问题
我需要确保在这个字符串的末尾始终有一个密码时,从那里开始将密码掩盖并不显示。
请记住,最后的值不总是cards-app
,它可以是不同的值,我需要掩盖从&password=
的字符串末尾以掩盖密码。
如何确保使用StringBuilder来实现这一点?
示例:
期望的输出:
这些信息显示在日志中,我只是想确保字符串的末尾被掩盖以不显示密码。
英文:
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.:
expected output:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论