我不看到错误。(用于邮件链接的转换代码)

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

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=&quot;&quot;;

    for (int i = 0; i &lt; texto.length(); i++)
        if (texto.charAt(i)!=&#39; &#39; &amp;&amp; texto.charAt(i)!=&#39;\n&#39;{
            total += texto.charAt(i);
        } else {
            if(texto.charAt(i)==&#39; &#39;) {
                total = total + &quot;%20&quot;;
            } else {
                if(texto.charAt(i)==&#39;\n&#39;){
                    total = total + &quot;%0D%0A&quot;;
                }
            }
        }
    }
    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(&quot; &quot;, &quot;%20&quot;).replace(&quot;\n&quot;, &quot;%0D%0A&quot;);
}

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 &lt; texto.length(); i++) {
        final char c = texto.charAt(i);
        if (c == &#39; &#39;) {
            sb.append(&quot;%20&quot;);
        } else if (c == &#39;\n&#39;) {
            sb.append(&quot;%0A%0D&quot;);
        } 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=&quot;&quot;;

	for (int i = 0; i &lt; texto.length(); i++)
		if(texto.charAt(i)==&#39; &#39;) {
			total = total + &quot;%20&quot;;
		} else if(texto.charAt(i)==&#39;\n&#39;){
			total = total + &quot;%0D%0A&quot;;
		} else {
			total += texto.charAt(i);
		}
	}
	return total
}

To reduce the number of tests you can inverse the logic: first check on &#39; &#39; and &#39;\n&#39;.

huangapple
  • 本文由 发表于 2020年9月1日 15:37:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63683304.html
匿名

发表评论

匿名网友

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

确定