英文:
I don't see the mistake. (Conversion code for Mailto)
问题
public String mailto(String texto){
String total = "";
for (int i = 0; i < texto.length(); i++)
if (texto.charAt(i) != ' ' && texto.charAt(i) != '\n'){
total += texto.charAt(i);
} else {
if(texto.charAt(i) == ' ') {
total = total + "%20";
} else {
if(texto.charAt(i) == '\n'){
total = total + "%0D%0A";
}
}
}
}
return total;
}
英文:
My intentions is convert a String for mailto but, I found a problem, when I set breakline remove all and set only the last line.
public String mailto(String texto){
String total="";
for (int i = 0; i < texto.length(); i++)
if (texto.charAt(i)!=' ' && texto.charAt(i)!='\n'{
total += texto.charAt(i);
} else {
if(texto.charAt(i)==' ') {
total = total + "%20";
} else {
if(texto.charAt(i)=='\n'){
total = total + "%0D%0A";
}
}
}
}
return total
}
答案1
得分: 1
不要手动进行URL编码(很容易出错!),使用现有的 URLEncoder
。
public String mailto(String texto) {
return URLEncoder.encode(texto);
}
注意,这会产生略有不同但有效的结果:空格被编码为 +
而不是 %20
。
如果出于某种原因,您想要/需要编写自己的临时电子邮件编码器,可以使用 String.replace
来缩短代码:
public String mailto(String texto) {
return texto.replace(" ", "%20").replace("\n", "%0D%0A");
}
如果您关心性能(务必进行实际测量!),不要通过串联构建字符串,而是使用 StringBuilder
。
结合对代码的修复以及稍微重写以提高可读性,会得到以下结果:
public String mailto(final String texto) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < texto.length(); i++) {
final char c = texto.charAt(i);
if (c == ' ') {
sb.append("%20");
} else if (c == '\n') {
sb.append("%0A%0D");
} else {
sb.append(c);
}
}
return sb.toString();
}
英文:
Don’t hand-roll URL encoding (it’s quite easy to get wrong!), use the existing URLEncoder
for that.
public String mailto(String texto) {
return URLEncoder.encode(texto);
}
Note that this yields a slightly different (but valid) result: space is encoded as +
instead of as %20
.
If for some reason you want/need to write your own ad-hoc email encoder, you can shorten your code by using String.replace
:
public String mailto(String texto) {
return texto.replace(" ", "%20").replace("\n", "%0D%0A");
}
If you’re concerned about performance (be careful to actually measure!), don’t construct your string via concatenation, use a StringBuilder
instead.
Together with the fixes of your code, as well as a slight rewrite to increase readability, this yields
public String mailto(final String texto) {
final StringBuillder sb = new StringBuilder();
for (int i = 0; i < texto.length(); i++) {
final char c = texto.charAt(i);
if (c == ' ') {
sb.append("%20");
} else if (c == '\n') {
sb.append("%0A%0D");
} else {
sb.append(c);
}
}
return sb.toString();
}
答案2
得分: 0
public String mailto(String texto) {
String total = "";
for (int i = 0; i < texto.length(); i++)
if (texto.charAt(i) == ' ') {
total = total + "%20";
} else if (texto.charAt(i) == '\n') {
total = total + "%0D%0A";
} else {
total += texto.charAt(i);
}
}
return total;
}
To reduce the number of tests, you can inverse the logic: first check for ' ' and '\n'.
英文:
public String mailto(String texto){
String total="";
for (int i = 0; i < texto.length(); i++)
if(texto.charAt(i)==' ') {
total = total + "%20";
} else if(texto.charAt(i)=='\n'){
total = total + "%0D%0A";
} else {
total += texto.charAt(i);
}
}
return total
}
To reduce the number of tests you can inverse the logic: first check on ' '
and '\n'
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论