在Java中,如何暴露和删除URI中的非断开字符

发布于 2025-01-23 16:08:06 字数 1456 浏览 2 评论 0原文

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

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

发布评论

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

评论(2

我爱人 2025-01-30 16:08:07

可能会有一个更快的解决方案立即出现,但是我想到的第一件事是创建一系列特殊字符,然后循环循环以删除它们,例如

String[] specialChars = {"!","@","#","$","%","^","&","*","(","}"};
for (String special : specialChars) {
    original.repalceAll(special, ""); // original is your original string
}

:数组是字符串而非字符的原因,是因为使用chars会要求您用一个空间替换特殊字符,这可能是您想要的,也可能不是您想要的。

There may be a quicker solution that didn't come to my head immediately but the first thing that comes to my mind is creating an array of the special characters, and then looping through the array to remove every one of them, like such:

String[] specialChars = {"!","@","#","
quot;,"%","^","&","*","(","}"};
for (String special : specialChars) {
    original.repalceAll(special, ""); // original is your original string
}

The reason the array is of strings and not chars is that using chars would require you to replace special characters with a space, which may or may not be what you want.

沫雨熙 2025-01-30 16:08:06

因此,经过进一步的检查,URI的唯一问题是无断空间(%C2%A0),因此我只需要从URI中删除这些空间即可。感谢@AndrewJames链接到一个问题,该问题显示了如何揭露解决方案关键部分的特殊字符。这是我想到的解决方案。

因此,首先,我将URI传递到Cleanstring()方法以删除特殊字符。乌里(URI)通过时具有以下格式:

\\ mycpu \现在\ harden,James Jr.& Allen \ 2021

,我将URI编码为UTF-8,该UTF-8揭露了基础特殊字符。编码后,编码具有以下格式:

%5C%5CMyCPU%5CGO+NOW%5Charden%2C+JAMES%C2%A0+Jr。+%26+RENE%5C2021

然后我在编码的URI上进行了一根绳子替换,以替换无断裂字符的子带(%C2%) A0)带有加号(这是UTF-8中的空间),并将结果存储在 cleaneduri 变量中,然后具有以下格式:

%5C%5CMyCPU%5CGO+NOW%5Charden%2C+James ++ Jr。+%26+RENE%5C2021

%5C%5CMYCPU%5CGO+NOW% 5CHARDEN '全部曾经看过看起来像这样的样子(最初看起来完全像它一样,除了构成其中一个空间之一的基础空间已被基本的UTF-8空间替换)。

\\ mycpu \现在\ harden,James Jr.& Allen \ 2021

这做得很好!谢谢大家的帮助!每个评论将我的道路介绍得更多(请参见下面的代码)。

private String cleanString(String uri){
        try {
            String encodedUri = URLEncoder.encode(uri, StandardCharsets.UTF_8.name());
            String cleanedUri = encodedUri.replace("%C2%A0", "+");
            return URLDecoder.decode(cleanedUri, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(ParseCSVFileImpl.class.getName()).log(Level.SEVERE, null, ex);
            return uri;
        }
    }

So, after further examination, the only issue in the uri's were the no break spaces (%C2%A0) so I just needed to remove those from the uri's. Thanks to @andrewjames for linking to a question that showed how to expose the special characters which was a critical part of the solution. Here's the solution I came up with.

So, first I passed the uri into a cleanString() method to remove the special characters. The uri, when passed in, had the following format:

\\mycpu\go now\Harden, James Jr. & Allen\2021

Then I encoded the URI into UTF-8 which exposed the underlying special characters. After being encoded, the encodedUri had the following format:

%5C%5Cmycpu%5Cgo+now%5CHarden%2C+James%C2%A0+Jr.+%26+Rene%5C2021

Then I ran a String replace on the encoded URI to replace the substring of no break characters (%C2%A0) with a plus sign (which is a space in UTF-8) and stored the result in the cleanedUri variable which then had the following format:

%5C%5Cmycpu%5Cgo+now%5CHarden%2C+James++Jr.+%26+Rene%5C2021

Lastly, I then decoded the cleanedUri back into the standard user-friendly URI format that we're all used to seeing which looked like this again (which appears exactly as it did initially except that the underlying no break space which made up one of the spaces has been replaced with a basic UTF-8 space).

\\mycpu\go now\Harden, James Jr. & Allen\2021

This did the trick handsomely! Thank you all for your help! Each comment illuminated my path a bit more (see code below).

private String cleanString(String uri){
        try {
            String encodedUri = URLEncoder.encode(uri, StandardCharsets.UTF_8.name());
            String cleanedUri = encodedUri.replace("%C2%A0", "+");
            return URLDecoder.decode(cleanedUri, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(ParseCSVFileImpl.class.getName()).log(Level.SEVERE, null, ex);
            return uri;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文