首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在批处理文件中获取字符串长度?

如何在批处理文件中获取字符串长度?
EN

Stack Overflow用户
提问于 2011-04-30 05:08:17
回答 18查看 173.3K关注 0票数 70

似乎没有一种简单的方法来获取批处理文件中字符串的长度。例如,

代码语言:javascript
复制
SET MY_STRING=abcdefg
SET /A MY_STRING_LEN=???

如何找到MY_STRING的字符串长度?

如果string length函数处理字符串中所有可能的字符,包括转义字符,就会得到加分,比如:!%^^()^!

EN

Stack Overflow用户

发布于 2015-04-14 17:50:52

代码语言:javascript
复制
@echo off
::   warning doesn't like * ( in mystring

setlocal enabledelayedexpansion 

set mystring=this is my string to be counted forty one

call :getsize %mystring%
echo count=%count% of "%mystring%" 

set mystring=this is my string to be counted

call :getsize %mystring%

echo count=%count% of "%mystring%" 

set mystring=this is my string
call :getsize %mystring%

echo count=%count% of "%mystring%" 
echo.
pause
goto :eof

:: Get length of mystring line ######### subroutine getsize ########

:getsize

set count=0

for /l %%n in (0,1,2000) do (

    set chars=

    set chars=!mystring:~%%n!

    if defined chars set /a count+=1
)
goto :eof

:: ############## end of subroutine getsize ########################
票数 0
EN
查看全部 18 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5837418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档