String[] specialChars = {"!","@","#","$","%","^","&","*","(","}"};
for (String special : specialChars) {
original.repalceAll(special, ""); // original is your original string
}
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.
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:
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:
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).
发布评论
评论(2)
可能会有一个更快的解决方案立即出现,但是我想到的第一件事是创建一系列特殊字符,然后循环循环以删除它们,例如
:数组是字符串而非字符的原因,是因为使用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:
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.
因此,经过进一步的检查,URI的唯一问题是无断空间(%C2%A0),因此我只需要从URI中删除这些空间即可。感谢@AndrewJames链接到一个问题,该问题显示了如何揭露解决方案关键部分的特殊字符。这是我想到的解决方案。
因此,首先,我将URI传递到Cleanstring()方法以删除特殊字符。乌里(URI)通过时具有以下格式:
,我将URI编码为UTF-8,该UTF-8揭露了基础特殊字符。编码后,编码具有以下格式:
然后我在编码的URI上进行了一根绳子替换,以替换无断裂字符的子带(%C2%) A0)带有加号(这是UTF-8中的空间),并将结果存储在 cleaneduri 变量中,然后具有以下格式:
%5C%5CMYCPU%5CGO+NOW% 5CHARDEN '全部曾经看过看起来像这样的样子(最初看起来完全像它一样,除了构成其中一个空间之一的基础空间已被基本的UTF-8空间替换)。
这做得很好!谢谢大家的帮助!每个评论将我的道路介绍得更多(请参见下面的代码)。
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:
Then I encoded the URI into UTF-8 which exposed the underlying special characters. After being encoded, the encodedUri had the following format:
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:
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).
This did the trick handsomely! Thank you all for your help! Each comment illuminated my path a bit more (see code below).