Board logo

标题: [文本处理] 批处理字符串请教 [打印本页]

作者: netdzb    时间: 2020-4-29 13:22     标题: 批处理字符串请教

我学习批处理也有一段时间了,水平就是停留在能看懂别人的代码。
自己想写就是写不出的这个阶段。比如批处理对字符串的处理问题上,
如果有一个文本里面都是英语单词,中间是空格分割的。
我想把各个单词子母倒过来,中间的分割符号还是空格应该怎么写代码。

语言都学的比较多了,批处理,shell脚本,python,vbs脚本,powershell
我都想了解一下,请编写代码能力比我厉害坛友不吝赐教。
作者: Batcher    时间: 2020-4-29 14:00

回复 1# netdzb


    能否把你使用其他语言实现这个功能的代码发出来看看,以便参考你的思路写出BAT代码。
作者: netdzb    时间: 2020-4-29 14:09

本帖最后由 netdzb 于 2020-4-29 14:10 编辑

回复 2# Batcher

python版我发了,不过我的方法好像很烦。如果reverseh后join加复原空格,我是能勉强写出来的代码。
我想看一下有无更好的方法。


ubuntu linux python

变成下面的样子
utnubn xunil nohtyp

代码我自己写了一点

s = "abcde"
def str_reverse1(s):
    return s[::-1]
if __name__ == '__main__':
    rs = str_reverse1(s)
    print(rs)
作者: went    时间: 2020-4-29 18:18

  1. @echo off
  2. set "line=ubuntu linux python"
  3. set "line1="
  4. :loop
  5. set "line1=%line1%%line:~-1%"
  6. set "line=%line:~0,-1%"
  7. if not "%line%"=="" goto :loop
  8. for %%i in (%line1%) do call set "line=%%i %%line%%"
  9. echo %line%
  10. pause&exit
复制代码

作者: netdzb    时间: 2020-4-29 20:38

回复 2# Batcher

我刚才写了2个小时,终于勉强写出来了。
和坛友的代码比较还是有差距啊。
作者: wujunkai    时间: 2020-4-30 20:26

回复 5# netdzb
  1. @echo off & setlocal enabledelayedexpansion
  2. :main
  3.     set "str=utnubn xunil nohtyp"
  4.     echo 原来的字符串是 %str%
  5.     call:deal str
  6.     echo 倒叙字符串是   %str%
  7.     pause
  8. goto:eof
  9. :deal
  10.     set "str_cpy=!%~1!"
  11.     set "str_don="
  12.     call:len str_cpy
  13.     for /L %%i in (0,1,%len%) do (
  14.         set "str_don=!str_cpy:~0,1!!str_don!"
  15.         set "str_cpy=!str_cpy:~1!"
  16.     )
  17.     set "%~1=%str_don%"
  18. goto:eof
  19. :len
  20.     set len_cpy=!%~1!
  21.     set len=0
  22.     for %%i in (128,64,32,16,8,4,2,1) do if not "!len_cpy:~%%i,1!"=="" (
  23.         set /A len+=%%i
  24.         set len_cpy=!len_cpy:~%%i!
  25.     )
  26. goto:eof
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2