查找字母更改为不同字母的所有出现次数 [英] Find ALL Ocurences of Letter change to Diffrent Letter

查看:28
本文介绍了查找字母更改为不同字母的所有出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种将所有字母更改为一串数字的加密方法,示例如下:

I am working on a encryption method that changes all letters to a string of numbers an Example Follows:

set a=a
set b=b
set c=c
set aa=543241254
set bb=785214185
set cc=842501445

我的问题是如何扫描用户输入的文件并将所有字母更改为数字.(被更改的文件文本类似于hihowareyou"没有空格或大写)我试图在所述文件中搜索a"并将其替换为数字,但我的部分不起作用.

My issue is How can I scan a file entered by the user and change all letters to the numbers. (The File being changed text is like "hihowareyou" No spaces or capitals) What I tried was to search for an 'a' in said file and replace it with the numbers but my section did not work.

(for /f "delims=" %%i in ('findstr /n "^" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%b%=%bb%!"
    echo(!line!
    endlocal
))>"%newfile%"
type "%newfile%" >>%newfile%

我不知道如何扫描文本文件中的字母并将其替换为 %a%、%b%、%c% 等.

I am not sure what to do to scan for the letter in the text file and replace it with %a%,%b%,%c% etc.

推荐答案

首先你应该意识到这些命令:

You should realize at first that these commands:

set a=a
set b=b
set c=c

...根据定义"是无用的(也就是说,没有进一步的解释),尽管很明显,在这种情况下,写入 %a% 将始终与仅写入相同a(其余字母相同).

... are useless "by definition" (that is, with no further explanation), although it should be evident that in this case writting %a% will always be the same than writting just a (the same happen for the rest of letters).

如果你想用一串数字来改变一个字母,那么正确的方法应该是这样的:

If you want to change one letter by a string of numbers, then the right way should be this:

set a=543241254
set b=785214185
set c=842501445

然后,为了通过变量的内容改变'a'字母,你可以在你的for 命令:

Then, in order to change the 'a' letter by the contents of a variable, you may do something like this inside your for command:

set "line=!line:a=%a%!"

再次:这将 a 字母更改为 a 变量的内容.

Again: this change the a letter by the contents of a variable.

现在,您应该意识到您需要对单个a字母/变量执行之前的替换,而是对每个一个表示的系列变量进行替换信.以相同方式处理一系列变量的标准方法(即使用相同的代码")是通过for命令改变下标数组结合使用.例如,如果我们选择字母"作为数组的名称,并在数组的不同元素中定义所需的替换值,我们肯定会这样做:

Now, you should realize that you need to perform the previous replacement not on the single a letter/variable, but on a series of variables represented each one by one letter. The standard way to process a series of variables in the same way (that really means "using the same code") is via a for command that vary the value of a subscript in combination with an array. For example, if we choose "letter" for the name of the array and define the desired replacement values in different elements of the array, surely we do something like this:

set letter[a]=543241254
set letter[b]=785214185
set letter[c]=842501445

在此之后,我们只需要知道如何操作这些变量/值.关于这一点的完整细节在 cmd.exe(批处理)脚本中的数组、链表等数据结构,这里不再赘述.但是,解决此问题所需的阵列管理比该链接中给出的完整说明要简单.

After this, we just need to know how these variables/values may be manipulated. Full details on this point are given at Arrays, linked lists and other data structures in cmd.exe (batch) script, so I will not repeat they here again. However, the array management required to solve this problem is simpler than the full explanation given at that link.

如果你执行这个命令:set letter,所有以letter"开头的变量都会显示出来,包括它们的值;例如:

If you execute this command: set letter, all the variables that start with "letter" will be shown, including their values; for example:

letter[a]=543241254
letter[b]=785214185
letter[c]=842501445

如果我们使用 for/F "tokens=2,3 delims=[]=" %%a in ('set letter') do ... 命令,则 %%a 可替换参数将包含数组的每一个下标,即字母abc 等,%%b 参数将包含相应的数值.

If we use a for /F "tokens=2,3 delims=[]=" %%a in ('set letter') do ... command, then the %%a replaceable parameter will contain each one of the subscripts of the array, that is, the letters a, b, c, etc., and the %%b parameter will contain the corresponding numeric values.

这样,为了完成您的程序并替换所有定义的字母,您只需要将 set "line=!line:%b%=%bb%!" 行放在上一个for 命令并正确地将表示字母%b%部分改为%%a;并将表示数值%bb%部分替换为%%b:

This way, in order to complete your program and replace all defined letters, you just need to place your set "line=!line:%b%=%bb%!" line inside previous for command and correctly change the %b% part, that represent the letter, by %%a; and replace the %bb% part, that represent the numeric value, by %%b:

for /F "tokens=2,3 delims=[]=" %%a in ('set letter') do (
   set "line=!line:%%a=%%b!"
)

当然,你还必须定义程序开头的letter数组...

Of course, you must also define the letter array at beginning of the program...

这篇关于查找字母更改为不同字母的所有出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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