batch-file - 使用批处理脚本将字母更改为对应的数字

标签 batch-file for-loop

我正在尝试创建一个批处理文件,它将更改一些输入文本并将它们更改为 a=1,b=2,c=3 等等 例如:

@echo off

set /p text=

echo :: %text% ::
echo Is this the expected text? //user can manually typo check
pause
cls

for /f delims=: %%i in ("%text%") do ( 
[something that changes the letters into numbers & store in variable]
)

::do stuff to numbers

最佳答案

这可以通过 cmd 脚本语言的关联数组来完成:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "text=This is my zero text example"
SET "alfa=0abcdefghijklmnopqrstuvwxyz"
FOR /l %%x IN (1,1,26) DO SET "$!alfa:~%%x,1!=%%x"
SET /a count=0
:loop
SET "char=!text:~%count%,1!"
SET "code=!$%char%!
SET /a count+=1
IF DEFINED char SET "line=!line!%code% "&GOTO :loop
ECHO %text%
ECHO %line%

输出:

This is my zero text example
20 8 9 19  9 19  13 25  26 5 18 15  20 5 24 20  5 24 1 13 16 12 5

关于batch-file - 使用批处理脚本将字母更改为对应的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18377204/

相关文章:

python - 使用 bat 文件运行 python 但 'python is not recognized as internal.....'

java - 在Windows bat文件中找到JRE安装以运行java程序的可靠方法

batch-file - 如何在 Windows 中加入两个文本文件,删除重复项

python - 打印循环内位置之间的数据

JavaScript 语法错误 : Unexpected end of input

javascript - 有没有办法为 Node.js (npm) 应用程序创建快捷方式桌面?

windows - 如何使用bat文件在Windows 11终端中启动多个选项卡?

java - 循环中 ArrayList 的错误初始化

c - 为什么我的程序可以编译但没有执行任何操作?

Python 查找列表的所有组合