---
title: "bat 批处理 字符串 替换函数 和 字符串截取函数"
author: "bestsaler"
date: "2010-09-07T15:41:00Z"
tags: []
slug: "bat"
draft: false
lastmod: "2010-09-07T15:41:00Z"
id: 759076
url: "https://bestsaler.iteye.com/blog/759076"
---

、字符串替换 好了，符号说到这，现在说%PATH:str1=str2% 上面语法的意思就是：将字符串变量%PATH%中的str1 替换为str2

@echo off  
@color 02  
set ALL=demo123

echo %ALL%  
set VAT=%ALL:1=aaa%  
echo %VAT%

---结果是demoaaa23  
pause

2、字符串截取 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** 截取功能统一语法格式为：%a:~\[m\[,n]]% \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
从第m为截取n长度的字符串,m 为偏移量（缺省为0），n 为截取长度（缺省为全部）,n可以是负数

%a:~0,n% 相当于函数 left(a,n)   取左边 n 位 %a:~-m%   相当于函数 right （a,m） 取右边 m 位 %a:~m,n% 相当于函数 mid(a,m+1,n) 从 m+1 位开始取 n 位 %a:~m,-n% 相当于函数 mid(a,m+1,len(a)-m-n),从 m+1 位开始，至倒数 n+1 位 %a:~m %   相当于函数 mid(a,m+1,len(a)-m) 或者 right(a,len(a)-m)，从m+1 位开始取右边全部。

%a:~\[m\[,n]]%

%123:~0，1%

结果1

 

&lt;script type="text/javascript"&gt;&lt;!-- google\_ad\_client = "pub-1985112462197986"; /* 728x90, 创建于 10-7-27 \*/ google\_ad\_slot = "9407691718"; google\_ad\_width = 728; google\_ad\_height = 90; //--&gt; &lt;/script&gt;&lt;script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show\_ads.js"&gt; &lt;/script&gt;