[英]How to encode the ampersand if it is not already encoded?
我需要 ac# 方法來編碼&符號,如果它們尚未編碼或另一個編碼的 epxression 的一部分
例如
"tom & jill" should become "tom & jill"
"tom & jill" should remain "tom & jill"
"tom € jill" should remain "tom € jill"
"tom <&> jill" should become "tom <&> jill"
"tom "&" jill" should become "tom "&" jill"
您真正想要做的是首先解碼字符串,然后再次對其進行編碼。 不要費心嘗試修補編碼的字符串。
任何編碼只有在可以輕松解碼的情況下才有價值,因此請重用該邏輯以使您的生活更輕松。 並且您的軟件不易出錯。
現在,如果您不確定字符串是否已編碼 - 問題肯定不是字符串本身,而是產生字符串的生態系統。 你從哪里得到的? 在它到達你面前之前,它經過了誰? 你相信嗎?
如果你真的不得不求助於創建一個magic-fix-weird-data函數,那么考慮建立一個“編碼”及其對應字符的表:
& -> &
€ -> €
< -> <
// etc.
然后,首先根據表解碼所有遇到的編碼,然后重新編碼整個字符串。 當然,在不先解碼的情況下摸索時,您可能會得到更有效的方法。 但明年你就不會理智了。 這是你的運營商,對吧? 你需要保持頭腦清醒! 如果你太聰明,你就會失去理智。 當你發瘋時,你會失去工作。 那些讓維護他們的黑客破壞他們思想的人會發生可悲的事情......
編輯:當然,使用 .NET 庫會讓你免於瘋狂:
我剛剛對其進行了測試,解碼字符串中只有&符號似乎沒有問題。 所以請繼續:
string magic(string encodedOrNot)
{
var decoded = HttpUtility.HtmlDecode(encodedOrNot);
return HttpUtility.HtmlEncode(decoded);
}
編輯#2 :事實證明,解碼器HttpUtility.HtmlDecode
將適用於您的目的,但編碼器不會,因為您不希望對尖括號( <
, >
)進行編碼。 但是編寫編碼器真的很容易:
define encoder(string decoded):
result is a string-builder
for character in decoded:
if character in encoding-table:
result.append(encoding-table[character])
else:
result.append(character)
return result as string
這應該做得很好:
text = Regex.Replace(text, @"
# Match & that is not part of an HTML entity.
& # Match literal &.
(?! # But only if it is NOT...
\w+; # an alphanumeric entity,
| \#[0-9]+; # or a decimal entity,
| \#x[0-9A-F]+; # or a hexadecimal entity.
) # End negative lookahead.",
"&",
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.