- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章批处理中字符串分割实现代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
批处理 字符串分割 实例 。
使用for命令可以对字符串进行分段处理.
分割字符串 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@
echo
off
::定义一个以分号作为分隔的字符串
set
str=AAA;BBB;CCC;DDD;EEE;FFF
::str的副本
set
remain=%str%
:loop
for /f
"tokens=1* delims=;"
%%a in (
"%remain%"
) do (
::输出第一个分段(令牌)
echo
%%a
rem 将截取剩下的部分赋给变量remain,其实这里可以使用延迟变量开关
set
remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
pause
|
主要解释for语句
delims=;表示以分号作为分隔符,对remain字符串进行分割处理。 tokens=1*,tokens表示分段的方式,tokens=1*表示第一个分隔符;之前的作为一部分,剩下的(*表示)作为一部分。这两部分在循环体总可以用%%a表示第一部分,%%b表示第二部分.
批处理 遍历path环境变量 。
我们知道path环境变量也是以分号作为分隔符的,批处理中,所以同样可以用上面的代码来遍历path环境变量.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@
echo
off
setlocal enabledelayedexpansion
::定义一个以分号作为分隔的字符串
set
str=%path%
::str的副本
set
remain=%str%
:loop
for /f
"tokens=1* delims=;"
%%a in (
"%remain%"
) do (
::输出第一个分段(令牌)
echo
%%a
rem 将截取剩下的部分赋给变量remain,其实这里可以使用延迟变量开关
set
remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
pause
|
运行结果
D:\dev\workspace\MarkdownTools ...... C:\windows\system32 D:\dev\java\jdk1.8.0_91\bin F:\Program Files\nodejs\node_global F:\Program Files\Git\bin D:\dev\apache-maven-3.5.4\bin ...... 请按任意键继续. . . 。
批处理 判断path环境变量中是否有某个目录 。
例如查找系统path环境变量中是否存在D:\dev\workspace\MarkdownTools这个目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@
echo
off
setlocal enabledelayedexpansion
::定义一个以分号作为分隔的字符串
::
set
str=AAA;BBB;CCC;DDD;EEE;FFF
set
str=%path%
::str的副本
set
remain=%str%
set
toFind=D:\dev\workspace\MarkdownTools
set
isFind=false
:loop
for /f
"tokens=1* delims=;"
%%a in (
"%remain%"
) do (
if
"%toFind%"
==
"%%a"
(
::设置标记,以便后续使用
set
isFind=true
::找到了就不找了
goto :finded
)
rem 将截取剩下的部分赋给变量remain,其实这里可以使用延迟变量开关
set
remain=%%b
)
::如果还有剩余,则继续分割
if defined remain goto :loop
:finded
echo
%isFind%
pause
|
运行结果
true 。
请按任意键继续. . . 。
参考资料 。
最近有有个小需求需要将shell 脚本的功能挪到windows中,但发现shell中有数组概念,但windows中却没有,同时shell中有很多方式处理字符串分割,但bat中就显得比较鸡肋,经过一番查找,终于有了方案(Stack Overflow:http://stackoverflow.com/questions/1707058/how-to-split-a-string-in-a-windows-batch-file):
方案: 通过for循环处理,而处理的方式又可以分两种,一种是普通for,一种是for的文件处理方式:
方案一:
1
2
3
4
5
|
@
echo
off & setlocal
rem 注意这里的s定义,其值不是使用双引号引起来的
rem also works for comma
-separated
lists, e.g. ABC,DEF,GHI,JKL
set
s=AAA BBB CCC DDD EEE FFF
for %%a in (%s%) do
echo
%%a
|
方案二:is the best for (most) arbitrary delimiter characters. 。
1
2
3
4
5
6
7
8
9
10
|
@
echo
off & setlocal
set
s=AAA BBB CCC DDD EEE FFF
set
t=%s%
:loop
for /f
"tokens=1*"
%%a in (
"%t%"
) do (
echo
%%a
rem 将截取剩下的部分赋给t,其实这里可以使用延迟变量开关
set
t=%%b
)
if defined t goto :loop
|
有个老兄给了个更完整的(用到了延迟变量):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
@
echo
off
setlocal ENABLEDELAYEDEXPANSION
REM
Set
a string with an arbitrary number of substrings separated by semi colons
set
teststring=The;rain;in;spain
REM Do something with each substring
:stringLOOP
REM Stop when the string is empty
if
"!teststring!"
EQU
""
goto END
for /f
"delims=;"
%%a in (
"!teststring!"
) do
set
substring=%%a
REM Do something with the substring -
REM we just
echo
it for the purposes of demo
echo
!substring!
REM Now strip off the leading substring
:striploop
set
stripchar=!teststring:~0,1!
set
teststring=!teststring:~1!
if
"!teststring!"
EQU
""
goto stringloop
if
"!stripchar!"
NEQ
";"
goto striploop
goto stringloop
)
:END
endlocal
|
还有这样的:
1
2
3
4
|
set
input=AAA BBB CCC DDD EEE FFF
set
nth=4
for /F
"tokens=%nth% delims= "
%%a in (
"%input%"
) do
set
nthstring=%%a
echo
%nthstring%
|
其实Powershell里可能有更多的内置函数可以使用:
PS C:\> "AAA BBB CCC DDD EEE FFF".Split() 。
还有人提出用vbscrip代替bat:
1
2
3
4
5
6
7
8
9
10
|
Set
objFS = CreateObject(
"Scripting.FileSystemObject"
)
Set
objArgs = WScript.Arguments
str1 = objArgs(0)
s=Split(str1,
" "
)
For
i=LBound(s)
To
UBound(s)
WScript.Echo s(i)
WScript.Echo s(9)
' get the 10th element
Next
usage:
c:\test> cscript /nologo test.vbs
"AAA BBB CCC"
|
最后来一个bat中的小难点: 变量延迟(自上而下,逐条(简单语句、复合语句(for、if 语句块只算一条))执行,而非逐行执行) 。
变量延迟详解 call setlocal 。
以上就是批处理中字符串分割实现代码的详细内容,更多关于批处理字符串分割的资料请关注我其它相关文章! 。
原文链接:https://blog.51cto.com/langlichong/1851961 。
最后此篇关于批处理中字符串分割实现代码的文章就讲到这里了,如果你想了解更多关于批处理中字符串分割实现代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试制作一个基于文本的批处理游戏。但是我刚开始写我以前从未遇到过的问题时遇到了一个问题。 :menu :: the game menu - opens when the game starts
我正在构建一个社交媒体应用程序,用户需要发布一些内容,然后将发布的内容传播给他/她的 4 个圈子内的所有成员。这意味着查询进入循环。它就像一个家谱。逻辑工作得很好。但现在,当每个圈子中的成员数量不断增
1. DECLARE TYPE ref_cursor_type IS ref CURSOR; v_mycursor ref_cursor_type;
我想在这里做的是循环直到按下“x”。我知道 CHOICE 带有 /T 选项。 但是 CHOICE 对我要播放的动画的超时时间太长。这是一个例子: @echo off cls set frame=2 :
我已经寻找解决方案,但我仍然遇到问题。我有两个文件: File1.txt 1111 2222 3333 File2.txt 1111 2222 3333 4444 我想要一个只有差异的输
我正在做一个批处理脚本,必须检查计算机上是否安装了一些程序。为此,我执行 programName --version我将输出存储在一个变量中。问题是当我尝试与正则表达式进行比较时(只知道该程序是否存在
我知道如何从同一个批处理文件中的函数返回值,但我发现从不同的批处理文件返回值时存在一些问题。下面是一个例子: 文件 1.cmd SETLOCAL ENABLEEXTENSIONS SETLOCAL E
我相信这个问题的答案应该很简单。我从一个地方获取目录列表并将它们存储到文本文档中。然后我读取文本文档名称并将它们存储到一个数组中。在此过程结束时,我希望删除数组中的所有条目。 我想这样做的原因是因为我
我家有两个摄像头,几乎每天都在创建图像。他们将它们保存到我的FTP服务器(Fritz.Box\Nas驱动器)。 文件夹结构如下: +-2016-08-24 +-+Subfolder +----+Ano
在Windows Batch中执行此操作。我有一个名称列表,并要求用户输入其名称。 我想检查该名称是否已经存在于列表中,如果存在,则直接进入goto,否则它将名称添加到列表中。 @echo off s
我正在编写一个批处理文件,我想运行一个 for 循环,将它的第一个值设置为一个变量。我只需要命令的第一个值,但我找不到另一种方法来做到这一点。我设置它的方式是使用一个 for 循环,然后是一个 do
我需要创建一个批处理文件,使用tracert命令跟踪一些IP,并将跟踪写入txt文件。我希望它很快,所以我想为每个跟踪启动一个新命令,以使所有跟踪请求立即启动。 这是我的 ping.bat: @ech
我想在批处理文件中使用PowerShell命令发送电子邮件。为此,我实现了一个名为 sendMail 的函数。我这样称呼它: setlocal enabledelayedexpansion call:
想要使用 java 执行 selenium 脚本/批处理脚本。根据输入参数调用脚本/批处理脚本。 了解如何使用 java 代码运行脚本/批处理。 请帮帮我。 最佳答案 要运行 java 项目中文件中包
我正在练习 JDBC 批处理并遇到错误: 错误1:不支持的功能错误2:执行不能为空或为null Property files include: itemsdao.updateBookName = Up
我从 json 文件中得到了以下字符串: 39468856, 现在,我想用这些数字进行计算..因此,我必须删除末尾的 , 。 此时我使用以下代码: for /f "skip=24 tokens=2"
我有一堆 SQL 查询作为文件存储在磁盘上。 它们都是纯 SELECT 查询,换句话说,它们只做读操作。 我正在连接到 Oracle 11g 数据库,我想测量所有这些查询的大致执行时间。有没有办法以编
我正在使用 java 来存储属性文件的目录路径。 然后在 bat 文件中我使用属性作为变量。 问题出在 Java 中,文件路径存储为 SOME_VAR=D\:\\Madhan\\Program Fil
我想用“%”替换字符串中的“mod”:set string=%string:mod=x%我应该输入什么作为“x”? 最佳答案 您可以通过启用延迟扩展来做到这一点,以便您可以使用 !作为分隔符。然后,将
在我目前正在处理的批处理文件中,我遇到了一个小问题。我有一个名为 Dimensions(number from 1-5, defined in a for /l loop).txt 的文件,其中包含
我是一名优秀的程序员,十分优秀!