请帮忙,一次替换多个不同的子串 [英] please help, replacing more than one different substring at once

查看:94
本文介绍了请帮忙,一次替换多个不同的子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个程序,用vb改变字符串中的每个字符



例如我有我愿意



我将是L,D将是G,O将是R,输出应该是



L GR



你可以帮我这个,我搜索代码,但我发现只换了一个相同的字符。



i已尝试使用此模式



Dim strMyText As String =今日访问巴黎

Dim strNewtext As String



strNewText =替换(strMyText,巴黎,伦敦)

i want to create a program that will change each character in a string using vb

for example i have "I do"

I will be L, D will be G, O will be R and the output supposedly is

"L GR"

can you help me with this one, i search for codes but i found only one same character replaced.

i have tried using this pattern

Dim strMyText As String = "Visit Paris Today"
Dim strNewtext As String

strNewText = Replace ( strMyText, "Paris", "London")

推荐答案

我想不出办法一下子完成它,但你似乎描述的是字母的直接替换。你已经在你的标签中提到了VB6和VB10,所以我不确定你在追求什么。我已经使用过VB.net但是有很多方法可以将它转换成VB6,如果你真的需要的话。



这是我拼凑在一起的东西......它有效,但有更好的方法,这只是为了让你去。注意,我没有使用过,因为你最终可能会替换你已经替换的字符。



I can't think of a way of doing it "all at once", but what you appear to be describing is a straight substitution of letters. You've mentioned VB6 as well as VB10 in your tags so I'm not sure what you're after. I've used VB.net but there are ways to convert this to VB6 if you really have to.

This is something I've cobbled together ... it works but there are better ways, this is just to get you going. Note, I haven't used Replace because you could end up replacing characters you've already substituted.

'These arrays define the one-to-one mappings
Dim SourceChars As String() = New String() {" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
Dim ChangeChars As String() = New String() {" ", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "A", "B", "C"}

'This is the test string
Dim test As String = "I do"

'This will be the output
Dim test2 As StringBuilder = New StringBuilder("")

'Step through the test string replacing each character
For i As Integer = 0 To test.Length - 1
    Dim testString As String = test.Substring(i, 1)
    Dim index As Integer = Array.FindIndex(SourceChars, Function(s) s.Contains(testString.ToUpper()))
    test2.Append(ChangeChars(index))
Next
Debug.Print(test2.ToString())
'Output is L GR



或使用字典


or using a Dictionary

Dim test As String = "I do"
Dim kvp As Dictionary(Of String, String) = New Dictionary(Of String, String)
kvp.Add(" ", " ")
kvp.Add("A", "D")
'... etc
kvp.Add("I", "L")
kvp.Add("D", "G")
kvp.Add("O", "R")

Dim test2 As StringBuilder = New StringBuilder("")

For i As Integer = 0 To test.Length - 1
    test2.Append(kvp(test.Substring(i, 1).ToUpper()))
Next
Debug.Print(test2.ToString())

你应该对列表中没有的字符进行一些检查,并注意查找上的 .ToUpper()

You ought to include some checks for characters not in the list and note that .ToUpper() on the lookup.


好像你想要加密一些Rot-N / Rot13



看看这里 http://www.vbforums.com/showthread.php?345183-Tutorial-Rot13-Rot-N-Encryption [ ^ ]



使用示例代码时,应将n = 13替换为n = 3
It seems like you want to some encryption like Rot-N/Rot13

Take a look here http://www.vbforums.com/showthread.php?345183-Tutorial-Rot13-Rot-N-Encryption[^]

When using the example code you should replace "n=13" with "n=3"


像这样:

Like this:
Dim s As String = "Visit Paris Today"
        s = s.Replace("Paris", "London")
        Console.WriteLine(s)


这篇关于请帮忙,一次替换多个不同的子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆