简体   繁体   English

如何将字符串转换为不同的字符?

[英]How can I convert a string into different characters?

I have looked on the web and I cannot find anything that helps me, all I can find is changing the characters into ASCII or Hexadecimal. 我在网上浏览时,找不到任何对我有帮助的东西,我所能找到的只是将字符更改为ASCII或十六进制。 However I would like to do it a different way. 但是,我想以其他方式来做。 For example, say the string that got passed in was abcdef , I would like to have a key which changes these characters into another string such as qwpolz . 例如,假设传入的字符串是abcdef ,我想拥有一个将这些字符更改为另一个字符串(例如qwpolz Is there an easier way than declaring each character in the alphabet to be another character like: 是否有比将字母表中的每个字符声明为另一个字符更简单的方法,例如:

Dim sText As String = "Hello"
Dim sEncode As String = ""
Dim iLength As Integer
Dim i As Integer
iLength = Len(sText)

For i = 1 To iLength
    sEncode = sEncode ????
Next
    Return sEncode

And then have a very lengthy loop which checks for these loops? 然后有一个很长的循环来检查这些循环? There must be a much simpler way. 必须有一种更简单的方法。 Can anybody help by pointing me in the right direction? 有人可以向正确的方向指点我吗?

Edit: Why downvote? 编辑:为什么要投票? Seriously, it's a legitimate question. 认真地说,这是一个合理的问题。 Instead of downvoting for no reason, just move onto another question. 不要无缘无故地投票,只需提出另一个问题。

Sounds as if you want to modify a string by replacing each character with a different character according to a mapping table. 听起来好像您想根据映射表通过用不同的字符替换每个字符来修改字符串。 An efficient approach is to use a Dictionary(Of Char, Char) . 一种有效的方法是使用Dictionary(Of Char, Char) But easier to write and maintain is something like this: 但是更容易编写和维护是这样的:

Shared ReadOnly replaceChars As String = "abcdef"
Shared ReadOnly withChars As String = "qwpolz"

Public Shared Function ReplaceAll(input As String) As String
    Dim newChars = From c In input
                   Let index = replaceChars.IndexOf(c)
                   Select If(index >= 0, withChars(index), c)
    Return String.Concat(newChars)
End Function

So the first string contains the chars that you want to replace and the second the replacement characters. 因此,第一个字符串包含要替换的字符,第二个字符串包含替换字符。 Both strings must have the same length. 两个字符串的长度必须相同。

If you want to support case insensitivity: 如果要支持不区分大小写:

Public Shared Function ReplaceAll(input As String, comparison As StringComparison) As String
    Dim newChars = From c In input
                   Let index = replaceChars.IndexOf(c.ToString(), comparison)
                   Select If(index >= 0, withChars(index), c)
    Return String.Concat(newChars)
End Function

Note that this is also a loop. 请注意,这也是一个循环。 There is no way to avoid some kind of loops if you want to replace multiple characters or strings. 如果要替换多个字符或字符串,则无法避免某种循环。

Well actually, this sounds like a Caesar sipher 实际上,这听起来像凯撒大帝

在此处输入图片说明

Private Overloads Shared Function Encrypt(ByVal ch As Char, ByVal code As Integer) As Char
    If Not Char.IsLetter(ch) Then
        Return ch
    End If
    Dim offset As Char = IIf(Char.IsUpper(ch), "A", "a")
    Return CType((((ch + (code - offset)) Mod 26) + offset),Char)
End Function

Private Overloads Shared Function Encrypt(ByVal input As String, ByVal code As Integer) As String
    Return New String(input.ToCharArray.Select(() => {  }, Encrypt(ch, code)).ToArray)
End Function

Private Shared Function Decrypt(ByVal input As String, ByVal code As Integer) As String
    Return Encrypt(input, (26 - code))
End Function

Note that this assumes, that you use English alphabet. 请注意,这假定您使用英语字母。 In general case where for example you have 'ä', 'ö', 'š', 'ž', 'ß', 'õ', 'ü' etc. this would not work. 在一般情况下,例如您有“ä”,“ö”,“š”,“ž”,“ß”,“õ”,“ü”等。这将不起作用。 In that case it is simpler to just create a list/dictionary of your ordered alphabet and use it. 在这种情况下,仅创建已排序字母的列表/字典并使用它会更简单。

Example use: 使用示例:

encrypted = Encrypt(sText, 5)
decypted = Decrypt(encrypted, 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM