網站首頁 編程語言 正文
通常情況下,任何一種語言對于字符串的操作都是重中之重。這里稍微介紹下bat批處理中對于字符串的操作。
對于字符串的截取
set testStr=abcdefghijklmnopqrstuvwxyz0123456789 echo 原始字符串 %testStr% echo 提取前五個字符串:%testStr:~0,5% echo 提取最后五個字符串:%testStr:~-5% echo 提取第一個到倒數第六個字符串:%testStr:~0,-5% echo 提取五個字符串,從第四個字符開始:%testStr:~3,5%
字符串的替換
echo 替換之前:%repStr% echo 替換后:%repStr:aa=zz% echo repStr=%repStr% set "repStr=%aa:aa=zz%" echo repStr=%repStr%
字符串合并
set aa=aabbcc set bb=ddeeff echo %aa%%bb% echo aa=%aa% echo bb=%bb% set "aa=%aa%%bb%" echo aa=%aa%
字符串查找
Setlocal ENABLEDELAYEDEXPANSION ::啟用命令擴展,參加setlocal /?命令 set str1=This is a test string set ch1=t ::注意,這里是區分大小寫的! set str=%str1% ::復制字符串,用來截短,而不影響源字符串 :next if not "%str%"=="" ( set /a num+=1 if "!str:~0,1!"=="%ch1%" goto last ::比較首字符是否為要求的字符,如果是則跳出循環 set "str=%str:~1%" goto next ) set /a num=0 ::沒有找到字符時,將num置零 :last echo 字符'%ch1%'在字符串"%str1%"中的首次出現位置為%num% echo 輸出完畢,按任意鍵退出&&pause>nul&&exit
這里直接貼上對應的bat批處理代碼及其運行結果
bat 批處理代碼
echo off echo 完全路徑:%0 echo 去掉引號:%~0 echo 所在分區:%~d0 echo 所在路徑:%~p0 echo 文件名:%~n0 echo 拓展名:%~x0 echo 文件屬性:%~a0 echo 修改時間:%~t0 echo 文件大小:%~z0 echo %d0% echo dp0 : %~dp0 echo sdp0:%~dp0 echo fo:%~f0 echo cd %cd% rem set 目標字符串=%源字符串:~起始值,截取長度% set testStr=abcdefghijklmnopqrstuvwxyz0123456789 echo 原始字符串 %testStr% echo 提取前五個字符串:%testStr:~0,5% echo 提取最后五個字符串:%testStr:~-5% echo 提取第一個到倒數第六個字符串:%testStr:~0,-5% echo 提取五個字符串,從第四個字符開始:%testStr:~3,5% echo ================================================ echo ================ ? ?字符串替換 ? ? ? =========== echo ================================================ set repStr=aaabbbcccdddeeefff echo 替換之前:%repStr% echo 替換后:%repStr:aa=zz% echo repStr=%repStr% set "repStr=%aa:aa=zz%" echo repStr=%repStr% echo ================================================ echo ================ ? ?字符串合并 ? ? ? =========== echo ================================================ set aa=aabbcc set bb=ddeeff echo %aa%%bb% echo aa=%aa% echo bb=%bb% set "aa=%aa%%bb%" echo aa=%aa% Setlocal ENABLEDELAYEDEXPANSION ::啟用命令擴展,參加setlocal /?命令 set str1=This is a test string set ch1=t ::注意,這里是區分大小寫的! set str=%str1% ::復制字符串,用來截短,而不影響源字符串 :next if not "%str%"=="" ( set /a num+=1 if "!str:~0,1!"=="%ch1%" goto last ::比較首字符是否為要求的字符,如果是則跳出循環 set "str=%str:~1%" goto next ) set /a num=0 ::沒有找到字符時,將num置零 :last echo 字符'%ch1%'在字符串"%str1%"中的首次出現位置為%num% echo 輸出完畢,按任意鍵退出&&pause>nul&&exit
相應的運行結果
原文鏈接:https://blog.csdn.net/peng_cao/article/details/74170979
相關推薦
- 2022-08-16 Go單元測試工具gomonkey的使用_Golang
- 2023-07-13 替換字符串中的任意字符及正則隱藏手機號中間四位
- 2022-01-20 localStorage 和 sessionStorage 及其用法 對象屬性操作方式
- 2022-04-14 詳細聊聊C#的并發機制優秀在哪_C#教程
- 2022-12-04 如何對csv文件數據分組,并用pyecharts展示_python
- 2022-06-02 Z-Order加速Hudi大規模數據集方案分析_服務器其它
- 2023-10-14 JAXB解析xml節點既有屬性又有值
- 2024-02-17 阻塞IO、非阻塞IO、IO多路復用、AIO的區別
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支