Replace funktioniert, aber merkwürdig

  • VB.NET

Es gibt 5 Antworten in diesem Thema. Der letzte Beitrag () ist von Unwesen.

    Replace funktioniert, aber merkwürdig

    Hallo,
    ich habe ein Problem mit der Replace-Funktion. Ich habe z.B. die Buchstaben A bis E, nun will ich diese ersetzt haben. Zum Beispiel A = 1, B = 2, C = 3, D = 4. Wenn ich nun als Input "A" eingebe bekomme ich als Output irgendwie "1AAA", wodran liegt das? Was hab ich falsch gemacht, hier ist der Code:

    VB.NET-Quellcode

    1. Dim repstring As String
    2. Dim readstring As String
    3. readstring = TextBox1.Text
    4. repstring = readstring.Replace("A", "1") _
    5. + readstring.Replace("B", "2") _
    6. + readstring.Replace("C", "3") _
    7. + readstring.Replace("D", "4") _
    8. + readstring.Replace("E", "5") _


    Ich habe es auch schon so versucht indem ich die + mit & ersetzt hatte aber es kam immer das selbe raus:
    1AAAA


    Wodran liegt das? Ich hab hier nicht nur die 5, es sind über 30 ?(

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „dussl“ ()

    readstring.replace gibt dir den gesamten String zurück. Diesen fügst du hier fünfmal hintereinander an.
    Entweder:

    VB.NET-Quellcode

    1. repstring=readstring.replace("A", "1")
    2. repstring=repstring.replace("B", "2")
    3. repstring=repstring.replace("C", "3")
    4. repstring=readstring.replace("D", "4")
    5. ...usw


    oder:

    VB.NET-Quellcode

    1. Repstring=Readstring.Replace("A", "1").Replace("B", "2").Replace("C", "3") ...


    In deinem Fall solltest du es mit einer Schleife versuchen. "A" ist gleich chr(65), damit lässt sich in einer Schleife arbeiten.
    Nach 3 Edits gehts immer noch nicht, hab alles ausprobiert was ihr vorgeschlagen habt. Hier ist der echte Code, vielleicht hab ich da einen Fehler reingehauen.

    VB.NET-Quellcode

    1. Dim readstring As String = ""
    2. Dim repstring As String = ""
    3. readstring = Form1.freebox.Text
    4. repstring = readstring.Replace("A", "0a1")
    5. repstring = readstring.Replace("B", "0a2")
    6. repstring = readstring.Replace("C", "0a3")
    7. repstring = readstring.Replace("D", "0a4")
    8. repstring = readstring.Replace("E", "0a5")
    9. repstring = readstring.Replace("F", "0a6")
    10. repstring = readstring.Replace("G", "0a7")
    11. repstring = readstring.Replace("H", "0a8")
    12. repstring = readstring.Replace("I", "0a9")
    13. repstring = readstring.Replace("J", "0b1")
    14. repstring = readstring.Replace("K", "0b2")
    15. repstring = readstring.Replace("L", "0b3")
    16. repstring = readstring.Replace("M", "0b4")
    17. repstring = readstring.Replace("N", "0b5")
    18. repstring = readstring.Replace("O", "0b6")
    19. repstring = readstring.Replace("P", "0b7")
    20. repstring = readstring.Replace("Q", "0b8")
    21. repstring = readstring.Replace("R", "0b9")
    22. repstring = readstring.Replace("S", "0c1")
    23. repstring = readstring.Replace("T", "0c2")
    24. repstring = readstring.Replace("U", "0c3")
    25. repstring = readstring.Replace("V", "0c4")
    26. repstring = readstring.Replace("W", "0c5")
    27. repstring = readstring.Replace("X", "0c6")
    28. repstring = readstring.Replace("Y", "0c7")
    29. repstring = readstring.Replace("Z", "0c8")
    30. repstring = readstring.Replace("a", "0d1")
    31. repstring = readstring.Replace("b", "0d2")
    32. repstring = readstring.Replace("c", "0d3")
    33. repstring = readstring.Replace("d", "0d4")
    34. repstring = readstring.Replace("e", "0d5")
    35. repstring = readstring.Replace("f", "0d6")
    36. repstring = readstring.Replace("g", "0d7")
    37. repstring = readstring.Replace("h", "0d8")
    38. repstring = readstring.Replace("i", "0d9")
    39. repstring = readstring.Replace("j", "0e1")
    40. repstring = readstring.Replace("k", "0e2")
    41. repstring = readstring.Replace("l", "0e3")
    42. repstring = readstring.Replace("m", "0e4")
    43. repstring = readstring.Replace("n", "0e5")
    44. repstring = readstring.Replace("o", "0e6")
    45. repstring = readstring.Replace("p", "0e7")
    46. repstring = readstring.Replace("q", "0e8")
    47. repstring = readstring.Replace("r", "0e9")
    48. repstring = readstring.Replace("s", "0f1")
    49. repstring = readstring.Replace("t", "0f2")
    50. repstring = readstring.Replace("u", "0f3")
    51. repstring = readstring.Replace("v", "0f4")
    52. repstring = readstring.Replace("w", "0f5")
    53. repstring = readstring.Replace("x", "0f6")
    54. repstring = readstring.Replace("y", "0f7")
    55. repstring = readstring.Replace("z", "0f8")
    56. repstring = readstring.Replace("1", "ZaA")
    57. repstring = readstring.Replace("2", "ZaB")
    58. repstring = readstring.Replace("3", "ZaC")
    59. repstring = readstring.Replace("4", "ZaD")
    60. repstring = readstring.Replace("5", "ZaE")
    61. repstring = readstring.Replace("6", "ZaF")
    62. repstring = readstring.Replace("7", "ZaG")
    63. repstring = readstring.Replace("8", "ZaH")
    64. repstring = readstring.Replace("9", "ZaI")
    65. repstring = readstring.Replace("0", "ZaJ")
    66. Form1.cryptbox.Text = repstring
    67. repstring = ""
    68. readstring = ""

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „dussl“ ()

    Nimm nur einen String:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Dim readstring As String = ""
    2. readstring = Form1.freebox.Text
    3. readstring = readstring.Replace("A", "0a1")
    4. readstring = readstring.Replace("B", "0a2")
    5. readstring = readstring.Replace("C", "0a3")
    6. readstring = readstring.Replace("D", "0a4")
    7. readstring = readstring.Replace("E", "0a5")
    8. readstring = readstring.Replace("F", "0a6")
    9. readstring = readstring.Replace("G", "0a7")
    10. readstring = readstring.Replace("H", "0a8")
    11. readstring = readstring.Replace("I", "0a9")
    12. readstring = readstring.Replace("J", "0b1")
    13. readstring = readstring.Replace("K", "0b2")
    14. readstring = readstring.Replace("L", "0b3")
    15. readstring = readstring.Replace("M", "0b4")
    16. readstring = readstring.Replace("N", "0b5")
    17. readstring = readstring.Replace("O", "0b6")
    18. readstring = readstring.Replace("P", "0b7")
    19. readstring = readstring.Replace("Q", "0b8")
    20. readstring = readstring.Replace("R", "0b9")
    21. readstring = readstring.Replace("S", "0c1")
    22. readstring = readstring.Replace("T", "0c2")
    23. readstring = readstring.Replace("U", "0c3")
    24. readstring = readstring.Replace("V", "0c4")
    25. readstring = readstring.Replace("W", "0c5")
    26. readstring = readstring.Replace("X", "0c6")
    27. readstring = readstring.Replace("Y", "0c7")
    28. readstring = readstring.Replace("Z", "0c8")
    29. readstring = readstring.Replace("a", "0d1")
    30. readstring = readstring.Replace("b", "0d2")
    31. readstring = readstring.Replace("c", "0d3")
    32. readstring = readstring.Replace("d", "0d4")
    33. readstring = readstring.Replace("e", "0d5")
    34. readstring = readstring.Replace("f", "0d6")
    35. readstring = readstring.Replace("g", "0d7")
    36. readstring = readstring.Replace("h", "0d8")
    37. readstring = readstring.Replace("i", "0d9")
    38. readstring = readstring.Replace("j", "0e1")
    39. readstring = readstring.Replace("k", "0e2")
    40. readstring = readstring.Replace("l", "0e3")
    41. readstring = readstring.Replace("m", "0e4")
    42. readstring = readstring.Replace("n", "0e5")
    43. readstring = readstring.Replace("o", "0e6")
    44. readstring = readstring.Replace("p", "0e7")
    45. readstring = readstring.Replace("q", "0e8")
    46. readstring = readstring.Replace("r", "0e9")
    47. readstring = readstring.Replace("s", "0f1")
    48. readstring = readstring.Replace("t", "0f2")
    49. readstring = readstring.Replace("u", "0f3")
    50. readstring = readstring.Replace("v", "0f4")
    51. readstring = readstring.Replace("w", "0f5")
    52. readstring = readstring.Replace("x", "0f6")
    53. readstring = readstring.Replace("y", "0f7")
    54. readstring = readstring.Replace("z", "0f8")
    55. readstring = readstring.Replace("1", "ZaA")
    56. readstring = readstring.Replace("2", "ZaB")
    57. readstring = readstring.Replace("3", "ZaC")
    58. readstring = readstring.Replace("4", "ZaD")
    59. readstring = readstring.Replace("5", "ZaE")
    60. readstring = readstring.Replace("6", "ZaF")
    61. readstring = readstring.Replace("7", "ZaG")
    62. readstring = readstring.Replace("8", "ZaH")
    63. readstring = readstring.Replace("9", "ZaI")
    64. readstring = readstring.Replace("0", "ZaJ")
    65. Form1.cryptbox.Text = readstring
    66. readstring = ""
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    dussl schrieb:


    Nach 3 Edits gehts immer noch nicht, hab alles ausprobiert was ihr vorgeschlagen habt. Hier ist der echte Code, vielleicht hab ich da einen Fehler reingehauen.


    Hmja, das wäre möglich..

    VB.NET-Quellcode

    1. ...
    2. repstring = readstring.Replace("A", "0a1")'ersetzt alle A und schreibt in REPstring
    3. repstring = readstring.Replace("B", "0a2")'ersetzt alle B und schreibt in REPstring
    4. ...



    Was ist mit dem zweiten Vorschlag von der_Kurt ? Das sieht doch ganz brauchbar aus?

    der_Kurt schrieb:


    Repstring=Readstring.Replace("A", "1").Replace("B", "2").Replace("C", "3") ...



    // Edit 1
    Abgesehen davon hab ich aber leichte Zweifel, ob das so klappt.

    Du ersetzt A gegen 0a1. Irgendwann 0 gegen Zaj, a gegen 0d1, 1 gegen ZaA und so weiter.. (Denk mal drüber nach... Bereits ersetzte Zeichen werden mit dieser Methode bei der nächsten Anweisung erneut geprüft und gegen neue Zeichen ersetzt)

    Ich glaube ja schon, dass das ganze dann schön verschlüsselt ist, allerdings wird es das dann wohl auch bleiben, sofern ich net was übersehen habe.


    //Edit 2
    Meine Lösung sähe etwa so aus:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Dim Original As String
    3. Dim Encrypted As String
    4. Dim CurrentChar As Char
    5. Dim CryptedPart As String
    6. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7. Original = TextBox1.Text
    8. Encrypted = ""
    9. For i As Integer = 0 To Original.Length - 1
    10. CurrentChar = Original.Substring(i, 1)
    11. Select Case CurrentChar
    12. Case "A"
    13. CryptedPart = "0a1"
    14. Case "B"
    15. CryptedPart = "0a2"
    16. Case "C"
    17. CryptedPart = "0a3"
    18. Case "D"
    19. CryptedPart = "0a4"
    20. End Select
    21. Encrypted = Encrypted & CryptedPart
    22. Next
    23. TextBox2.Text = Encrypted
    24. End Sub
    25. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    26. 'Rufe Form2 aus (Entschlüsseln)
    27. Form2.ShowDialog()
    28. End Sub
    29. End Class


    Und der Decrypter folgt hier:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class Form2
    2. Dim Original2 As String
    3. Dim Encrypted2 As String
    4. Dim CurrentChar2 As String
    5. Dim CryptedPart2 As String
    6. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7. Original2 = TextBox1.Text
    8. Encrypted2 = ""
    9. For i As Integer = 0 To Original2.Length - 1 Step 3
    10. CurrentChar2 = Original2.Substring(i, 3)
    11. Select Case CurrentChar2
    12. Case "0a1"
    13. CryptedPart2 = "A"
    14. Case "0a2"
    15. CryptedPart2 = "B"
    16. Case "0a3"
    17. CryptedPart2 = "C"
    18. Case "0a4"
    19. CryptedPart2 = "D"
    20. End Select
    21. Encrypted2 = Encrypted2 & CryptedPart2
    22. Next
    23. TextBox2.Text = Encrypted2
    24. End Sub
    25. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    26. TextBox1.Text = Form1.TextBox2.Text
    27. End Sub
    28. End Class


    Sry für die vielen Edits^^ Aber is ja ATM keiner im Thread ausser mir

    Dieser Beitrag wurde bereits 7 mal editiert, zuletzt von „Unwesen“ ()