我正在製作一個自定義的加密應用程序,而我卡在替換部分。 例如我有字符串"AB12AB12"
,我想加密它,以便每個A變成1,每個B a 2,每1個A和每2個B.如果一切順利,輸出應該是12AB12AB,但這是什麼發生:VB如何一次替換多個字符?
Private Sub Encrypt()
Dim str As String = "AB12AB12"
str = str.Replace("A", "1") 'now the output is "1B121B12"'
str = str.Replace("B", "2") 'now the output is "12121212"'
str = str.Replace("1", "A") 'this is where it goes wrong, output is now "A2A2A2A2"
str = str.Replace("2", "B") 'now the output is "ABABABAB" and it should be "12AB12AB"
output = str
End Sub
問題是,使用此代碼,你不斷取代所有包括以前的替換。也許有一個選項喜歡同時替換每個字符,以便替換不會被替換...
不要做直替換英寸按字符進行。 – durbnpoisn
這裏有一個相當有Doh因子。不要使用已經出現在原始字符串中的字符進行編碼,您無法再次恢復原始字符。這樣做根本沒有意義,一個簡單的替代密碼被頻繁的表格打亂了。使用真正的加密,System.Security.Cryptography命名空間。 –
這不是任何加密夥伴,不要自己滾動至少任何私人... – Codexer