China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-09 05:17
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original] A New Method for Calculating String Length View 1,895 Replies 10
Original Poster Posted 2008-07-11 11:52 ·  中国 江苏 苏州 常熟市 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
I'm no good at anything else, but sometimes I get sudden ideas.
Please test the following code, so as to inspire everyone to dig into the potential of certain commands.

@echo off
Title Calculating String Length
set /p string=Please enter a string:

REM ""%string%" is for compatibility with input containing spaces

echo "%string%">%temp%\temp.txt
echo eof>>%temp%\temp.txt
for /f "delims=:" %%i in ('findstr /o /c:"eof" %temp%\temp.txt') do set /a len=%%i-4
echo String length:%len%
pause
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 2 Posted 2008-07-11 12:26 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
What you get is not the string length, but... the byte count...

If it's Chinese characters...

PS: If you're using the file size method, it seems you can use for's %~zI
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 3 Posted 2008-07-11 13:14 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Can't really count as a new method, right? People brought up the offset method a long time ago.

@echo off
set str=abc
for /f "skip=1 delims=:" %%a in ('^(echo "%str%"^&echo.^)^|findstr /o ".*"') do set /a length=%%a-5
echo %length%
Floor 4 Posted 2008-07-11 14:16 ·  中国 广西 玉林 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
21-year member
UID 44210
Status Offline
If you're generating a temporary file, then just use for's %~zI directly, otherwise use the method from post #3
Floor 5 Posted 2008-07-11 15:00 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
If you also calculate Chinese characters by offset, you'll get the wrong result, hehe.

@echo off
set str=我是abc
set length=0
:begin
set char=%str:~0,1%
set /a length+=1
set str=%str:~1%
if "%str%" equ "" (
echo %length%
goto :eof
) else (
goto :begin
)
Floor 6 Posted 2008-07-11 18:35 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
But I did think of this independently. I didn't know someone had already brought it up. What a pity, I originally thought it was the first of its kind.

If I'd known earlier, I wouldn't have taken it out and made a fool of myself.

By the way, can anyone give some detailed examples of findstr? List each parameter, especially things in regular expressions like $ and \X and the like. I really haven't seen any concrete examples.
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 7 Posted 2008-07-11 18:38 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
TO 6

This is the FINDSTR usage info I collected. It's very helpful, let's learn it together!~

Basic usage of FINDSTR regular expressions
Code:
1.findstr . 2.txt or Findstr "." 2.txt
Search for any character in file 2.txt, excluding null characters or blank lines
====================


2.findstr .* 2.txt or findstr ".*" 2.txt
Search for any character in file 2.txt, including blank lines and null characters
====================


3.findstr "" 2.txt
Search in file 2.txt for strings or lines containing digits 0-9
====================


4.findstr "" 2.txt
Search in file 2.txt for strings or lines containing any letters
====================


5.findstr "" 2.txt
Search in file 2.txt for strings or lines containing the letters a b c e z y
====================


6.findstr "" 2.txt
Search in file 2.txt for strings containing lowercase characters a-f and l-z, but not the letters g h I j k.
====================


7.findstr "MY" 2.txt
In file 2.txt it can match MahY, MbiY, MahY, etc.....
====================


8. The use of the symbols ^ and $
^ indicates the beginning of a line, "^step" only matches the first word in "step hello world"
$ indicates the end of a line, "step$" only matches the last word in "hello world step"
====================


9.finstr "" 2.txt
If it is a purely numeric string or line, then it will be filtered out, for example a string like 2323423423. But if it is in a form like 345hh888 then it won't work.
====================


10.findstr "" 2.txt
Same as above, if it is a purely alphabetic string or line, then it will be filtered out, for example characters like sdlfjlkjlksjdklfjlskdf. But if it is in a form like sdfksjdkf99999, mixed with digits, then it won't work.
====================


11.The role of the * sign
As already mentioned above, ".*" means the search condition is any character. The role of * in regular expressions is not any character, but indicates the repetition count of the character or expression on the left side. * indicates the repetition count is zero or more times.
====================


12.findstr "^*$" 2.txt
This matches purely numeric text that is found, for example 234234234234. If it is 2133234kkjl234 then it gets filtered out.
Findstr "^*$" 2.txt
This matches purely alphabetic text that is found, for example sdfsdfsdfsdf. If it is 213sldjfkljsdlk then it gets filtered out.
If there is no * sign in the search condition, that means the search condition on the left side is not repeated, that is , then it can only match the first character of the string, and only this one character, because there is the restriction of beginning and end of line. With "^$", if the first character is a digit then it matches, if not then it is filtered out. If the string is 9 then it matches, but if it is 98 or 9j and the like then it won't work.
=====================


13. The role of the "\<…\>" expression
This means searching exactly for a string. \<sss indicates the starting position of the word, and sss\> indicates the ending position of the word.
echo hello world computer|findstr "\<computer\>" works in this form
echo hello worldcomputer|findstr "\<computer\>" in this form won't work. What it wants to find is the string "computer", so that won't do.
echo hello worldcomputer|findstr ".*computer\>" this way it can match
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 8 Posted 2008-07-11 19:10 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
For reference on the function of symbols in CMD/DOS
http://www.cn-dos.net/forum/viewthread.php?tid=30985&fpage=4

If you still don't understand, you can post the findstr command you saw, and naturally someone will explain it, hehe.
Floor 9 Posted 2008-07-12 11:04 ·  中国 江苏 苏州 昆山市 电信
初级用户
Credits 26
Posts 13
Joined 2007-11-15 21:09
18-year member
UID 102773
Gender Male
From 江苏无锡
Status Offline
OP, I just looked at your program for calculating character length, and there's a question: why do you subtract 4 at the end, and not 3? eof is only three characters. Thanks for clearing that up for me.
Floor 10 Posted 2008-07-12 11:20 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Subtract 4, namely: two double quotation marks, one newline character \n, and one text end marker eof
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
whitegod +1 2008-07-12 11:34
Floor 11 Posted 2008-07-12 11:33 ·  中国 江苏 苏州 昆山市 电信
初级用户
Credits 26
Posts 13
Joined 2007-11-15 21:09
18-year member
UID 102773
Gender Male
From 江苏无锡
Status Offline
Thanks, brother above
Forum Jump: