将字符串中的所有字符转换为不同的转义格式(Java)
我正在寻找将字符串中的字符转换为不同的转义格式,如下所示,其中字母“a”是要转换的字符串:
hex-url: %61
hex-html: a
decimal-html: a
我搜索过使用各种内置方法,但它们只是取出了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定内置库,但自己编写一个方法来执行此操作非常容易。您需要做的就是逐个字符循环遍历字符串,并对每个字符执行如下操作:
"&#"+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.
现有的库方法不太可能满足您的要求:
您需要自己实现这一点。 (代码很简单......我假设你有能力。)
There is unlikely to be an existing library method that does what you want:
You will need to implement this yourself. (The code is trivial ... and I'm assuming that you are capable.)