如何在批处理文件中反转字符串

如何在批处理文件中反转字符串

本文介绍了如何在批处理文件中反转字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个字符串t1=3t2=1t3=5.

想要反转这些字符串,以便t1=5t2=1t3=3.

want to reverse these strings so that t1=5, t2=1 and t3=3.

它们共同构成一个数字(输入:315和输出:513).

Together they form a number (input:315 and output:513).

我需要颠倒它们形成的数字.

I need to reverse the number they form.

我试图这样做:

set "rest2=%t1%"
set "t1=%t3%"
set "t3=%rest2%"

当我尝试时,它从形成的数字中删除了t1(输入:315和输出:13).

when I tried it, it removed t1 from the number they formed (input:315 and output: 13).

我不知道为什么会这样.

I dont know why that happend.

推荐答案

我猜想您的set命令全部都在括号内的代码块(例如for循环)中,并且您没有采用延迟当你应该扩张.在这种情况下,您可以通过以下方式修复现有代码:

I'm guessing your set commands all live within a parenthetical code block (such as a for loop), and you aren't employing delayed expansion when you should be. If that's the case, you can probably fix your existing code this way:

setlocal enabledelayedexpansion
set "rest2=!t1!"
set "t1=!t3!"
set "t3=!rest2!"

仅此而已,我觉得自己已经做了一些事情,下面是一个实用程序函数,它将反转一个可以解决您的问题的字符串.为了缩短执行时间,此字符数限制为100个字符.我想这在大多数情况下都可以.

Just so I feel like I've done something, here's a utility function that'll reverse a string that might solve your issue. This one has a limit of 100 characters for the sake of small execution time. I'm guessing this will be fine for most situations.

@echo off
setlocal

set /P "input=Input? "
call :reverse flipped "%input%"

echo %input% reversed is %flipped%
rem // end main runtime
goto :EOF

rem // functions

:reverse <return_var> <string>
setlocal enabledelayedexpansion
set "ret=" & set "str=%~2"
for /L %%I in (0,1,100) do (
    if "!str!"=="" for %%a in ("!ret!") do (
        endlocal & set "%~1=%%~a" & exit /b
    )
    set "ret=!str:~0,1!!ret!"
    set "str=!str:~1!"
)


如果您希望函数工作到一个变量可以携带的字符数上限,可以将100增加到8192,但这将导致for /L循环循环8192次,无论是否被破坏exit /b .确实没有一种优雅的方法可以摆脱for /L(尽管它比goto循环要快得多).


If you'd rather have the function work up to the limit of characters a variable can carry, you can increase the 100 to 8192, but that'll cause the for /L loop to loop 8192 times regardless of whether broken by exit /b. There's not really a graceful way to break out of for /L (although it is much faster than a goto loop).

添加一个函数以在触发for /L循环之前获取字符串的长度,可以使您的代码对长字符串更有效,而不会显着影响短字符串.尽管代码很多,但比上面的脚本要快.

Adding a function to get the length of the string before firing the for /L loop can make your code more efficient for long strings while not significantly impacting short ones. Although this is a lot more code, it is faster than the script above.

@echo off
setlocal

set /P "input=Input? "
call :reverse flipped "%input%"

echo %input% reversed is %flipped%
goto :EOF

:reverse <return_var> <string>
setlocal enabledelayedexpansion
set "ret=" & set "str=%~2" & call :length len "%~2"
for /L %%I in (0,1,%len%) do (
    if "!str!"=="" for %%a in ("!ret!") do (
        endlocal & set "%~1=%%~a" & exit /b
    )
    set "ret=!str:~0,1!!ret!"
    set "str=!str:~1!"
)

:length <return_var> <string>
setlocal enabledelayedexpansion
if "%~2"=="" (set ret=0) else set ret=1
set "tmpstr=%~2"
for %%I in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if not "!tmpstr:~%%I,1!"=="" (
                set /a ret += %%I
                set "tmpstr=!tmpstr:~%%I!"
        )
)
endlocal & set "%~1=%ret%"
exit /b 0

for /L循环中遇到goto :EOFexit /b时,尽管do之后的内容将被忽略,计数仍将继续.

When goto :EOF or exit /b is encountered within a for /L loop, the counting continues, although the stuff after do is ignored.

这篇关于如何在批处理文件中反转字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:39