将字符串中的所有字符转换为不同的转义格式(Java)

发布于 2024-11-25 02:39:36 字数 236 浏览 0 评论 0原文

我正在寻找将字符串中的字符转换为不同的转义格式,如下所示,其中字母“a”是要转换的字符串:

hex-url: %61
hex-html: a
decimal-html: &#97

我搜索过使用各种内置方法,但它们只是取出了 url 编码指定的字符(如“<”)并转义它们。我想转义整个字符串。有什么方法可以将字符串转换为java中的上述格式(最好使用内置库)?

I'm looking to convert characters in a string to different escaped formats like the following, where the letter 'a' is the string being converted:

hex-url: %61
hex-html: a
decimal-html: a

I've searched used various built-in methods, but they merely take out the url-encoding specified chars(like '<') and escape them. I want to escape the ENTIRE string. Is there any way to convert a string into the formats above in java(using built in libraries, preferrably)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

執念 2024-12-02 02:39:36
public class StringEncoders
{
    static public void main(String[] args)
    {
        System.out.println("hex-url: " + hexUrlEncode("a"));
        System.out.println("hex-html: " + hexHtmlEncode("a"));
        System.out.println("decimal-html: " + decimalHtmlEncode("a"));
    }
    static public String hexUrlEncode(String str)   {
        return encode(str, hexUrlEncoder);
    }
    static public String hexHtmlEncode(String str)  {
        return encode(str, hexHtmlEncoder);
    }
    static public String decimalHtmlEncode(String str)  {
        return encode(str, decimalHtmlEncoder);
    }
    static private String encode(String str, CharEncoder encoder)
    {
        StringBuilder buff = new StringBuilder();
        for ( int i = 0; i < str.length(); i++)
            encoder.encode(str.charAt(i), buff);
        return ""+buff;
    }
    private static class CharEncoder
    {
        String prefix, suffix;
        int radix;
        public CharEncoder(String prefix, String suffix, int radix)        {
            this.prefix = prefix;
            this.suffix = suffix;
            this.radix = radix;
        }
        void encode(char c, StringBuilder buff)     {
            buff.append(prefix).append(Integer.toString(c, radix)).append(suffix);
        }
    }
    static final CharEncoder hexUrlEncoder = new CharEncoder("%","",16);
    static final CharEncoder hexHtmlEncoder = new CharEncoder("&#x",";",16);
    static final CharEncoder decimalHtmlEncoder = new CharEncoder("&#",";",10); 
}
public class StringEncoders
{
    static public void main(String[] args)
    {
        System.out.println("hex-url: " + hexUrlEncode("a"));
        System.out.println("hex-html: " + hexHtmlEncode("a"));
        System.out.println("decimal-html: " + decimalHtmlEncode("a"));
    }
    static public String hexUrlEncode(String str)   {
        return encode(str, hexUrlEncoder);
    }
    static public String hexHtmlEncode(String str)  {
        return encode(str, hexHtmlEncoder);
    }
    static public String decimalHtmlEncode(String str)  {
        return encode(str, decimalHtmlEncoder);
    }
    static private String encode(String str, CharEncoder encoder)
    {
        StringBuilder buff = new StringBuilder();
        for ( int i = 0; i < str.length(); i++)
            encoder.encode(str.charAt(i), buff);
        return ""+buff;
    }
    private static class CharEncoder
    {
        String prefix, suffix;
        int radix;
        public CharEncoder(String prefix, String suffix, int radix)        {
            this.prefix = prefix;
            this.suffix = suffix;
            this.radix = radix;
        }
        void encode(char c, StringBuilder buff)     {
            buff.append(prefix).append(Integer.toString(c, radix)).append(suffix);
        }
    }
    static final CharEncoder hexUrlEncoder = new CharEncoder("%","",16);
    static final CharEncoder hexHtmlEncoder = new CharEncoder("&#x",";",16);
    static final CharEncoder decimalHtmlEncoder = new CharEncoder("&#",";",10); 
}
多孤肩上扛 2024-12-02 02:39:36

我不确定内置库,但自己编写一个方法来执行此操作非常容易。您需要做的就是逐个字符循环遍历字符串,并对每个字符执行如下操作:

"&#"+Integer.toHexString(character)+";";

然后附加它到您正在制作的包含所有字符编码的新字符串。

I'm not sure about built in libraries, but it's pretty easy to write a method to do this yourself. All you need to do is loop through the string character by character and for each character do something like this:

"&#"+Integer.toHexString(character)+";";

and then append it to a new string you are making that has all the characters encoded.

禾厶谷欠 2024-12-02 02:39:36

现有的库方法不太可能满足您的要求:

  • 在每个示例中,转义都是不必要的;例如字母“a”。进行转义的库方法仅在必要时才进行转义。
  • 允许您进行 HTML / XML 转义的库不允许您选择特定的转义语法 (AFAIK)。
  • 你的第三个例子被错误地转义了。

您需要自己实现这一点。 (代码很简单......我假设你有能力。)

There is unlikely to be an existing library method that does what you want:

  • In each of those examples, the escaping is unnecessary; e.g. for the letter 'a'. Library methods that do escaping only do it if it is necessary.
  • Libraries that allow you to do HTML / XML escaping don't allow you to chose the specific escaping syntax (AFAIK).
  • Your third example is incorrectly escaped.

You will need to implement this yourself. (The code is trivial ... and I'm assuming that you are capable.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文