中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-14 17:22
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » [Help] How to get the number of characters and intercept characters
Printable Version  2,729 / 11
Floor1 yangzhiyi Posted 2007-03-17 09:39
中级用户 Posts 123 Credits 261
How to intercept an even number of characters and the maximum is eight characters?

For example, input: one two three four five six seven eight nine ten intercept as: one two three four five six seven eight
For example, input: one two three four five six seven eight intercept as: one two three four five six seven eight
For example, input: one two three four five six seven intercept as: one two three four five six
For example, input: one two three four five six intercept as: one two three four five six
For example, input: one two three four five intercept as: one two three four
For example, input: one two three four intercept as: one two three four
For example, input: one two three intercept as: one two
For example, input: one two intercept as: one two
For example, input: one prompt: Must enter two or more characters
For example, input: prompt: Must enter and two or more characters

I can do interception and prompting, but I don't know how to get the number of characters
Floor2 qjbm Posted 2007-03-17 11:40
初级用户 Posts 44 Credits 125
To count the number of characters in a variable in a batch script, a complete set of "functions" needs to be written to implement it.

Special characters are always a headache for batch processing...

The following is a humble example:



--------------------------------------------
Hehe:)~~

It seems that I really complicated the problem.

It is still better to be a spectator and learn quietly to avoid more criticism:)

At that time, seeing that the number of views of this post exceeded 70 and no one replied, and it was worthy of discussion, it was a pity to sink, so I just bumped it up. I'm really sorry...


-------------------------------------------
Come every day, cry every day...........
I'll talk about it after taking a break for a while........

[ Last edited by qjbm on 2007-3-18 at 03:07 PM ]
Floor3 yangzhiyi Posted 2007-03-18 03:50
中级用户 Posts 123 Credits 261
Can you explain it? I don't understand.
What I got after running is 27, which is wrong.
I only want Chinese, no special characters.
Another question: If two strings are concatenated, like character: "一三" and character: "12", get "一三12"?
Floor4 bjsh Posted 2007-03-18 04:38
银牌会员 Posts 621 Credits 2,000
The number of characters on the second floor is too complicated;


@echo off
set /p var=Please enter:
set count=0
:loop
if "%var%"=="" goto jump
set var=%var:~0,-1%
set /a count=%count%+1
goto loop
:jump
echo %count%
pause
Floor5 bjsh Posted 2007-03-18 04:54
银牌会员 Posts 621 Credits 2,000
Answer the LZ's question

For code of any length of characters

@echo off
set /p var=Please enter:
set var_bak=%var%
set count=0
:loop
if "%var%"=="" goto jump
set var=%var:~0,-1%
set /a count=%count%+1
goto loop
:jump
set /a x=%count%/2*2
if "%x%"=="0" echo Must enter two or more characters && goto end
call echo Intercepted as:%%var_bak:~0,%x%%%
:end


For code of the length required by LZ

@echo off
set /p var=Please enter:
set var_bak=%var%
set count=0
:loop
if "%var%"=="" goto jump
set var=%var:~0,-1%
set /a count=%count%+1
goto loop
:jump
set /a x=%count%/2*2
if %x% GEQ 8 set x=8
if "%x%"=="0" echo Must enter two or more characters && goto end
call echo Intercepted as:%%var_bak:~0,%x%%%
:end


The difference between the two is only

if %x% GEQ 8 set x=8
Floor6 bjsh Posted 2007-03-18 05:15
银牌会员 Posts 621 Credits 2,000
There is such an interesting phenomenon for special characters.

For example:

Please input: ^1245
4 Here is the number of character counts; adding ^ in front actually counts it as 4
Intercepted as: 1245

Please input: 1234567&89. Here adding an & in the middle caused so many errors

'89' is not an internal or external command, nor is it a runnable program or batch file.
'8' is not an internal or external command, nor is it a runnable program or batch file.
8 Here is the number of character counts, obviously & is included
Intercepted as: 1234567

Please input: 1234567&
8
Intercepted as: 1234567 Originally the program was to output an even number but output 7; that is to say & is "hidden"

[ Last edited by bjsh on 2007-3-17 at 04:16 PM ]
Floor7 xycoordinate Posted 2007-03-18 05:50
中级用户 Posts 228 Credits 493 From 安徽
Originally posted by bjsh at 2007-3-17 15:38:
The number of characters on the second floor is too complicated;
@echo off
set /p var=Please enter:
set count=0

:loop
if "%var%"=="" (
echo %count%
goto :eof
)
set var=%var:~0,-1%
set /a count+=1
goto loop

pause


Indeed easier to understand than the second floor!

I want to ask:
Can we intercept from the front of the var variable and then judge???
Floor8 bjsh Posted 2007-03-18 07:14
银牌会员 Posts 621 Credits 2,000
Okay;
set var=%var:~1%
Floor9 yangzhiyi Posted 2007-03-18 09:06
中级用户 Posts 123 Credits 261
Thanks for the answer.

What is the use of setting "count"? I don't understand it. Can you explain it in detail?

@echo off
set /p var=Please enter:
set var_bak=%var%
set count=0
:loop
if "%var%"=="" goto jump
set var=%var:~0,-1%
set /a count=%count%+1
goto loop
:jump
set /a x=%count%/2*2
if %x% GEQ 8 set x=8
if "%x%"=="0" echo Must enter more than two characters && goto end
call echo Cut to:%%var_bak:~0,%x%%%
:end
Floor10 yangzhiyi Posted 2007-03-18 09:18
中级用户 Posts 123 Credits 261
At first I wrote it like this, thinking it was too long, still the code on the fifth floor is better.
@echo off
:start
set var=
set /p var=Please enter:
:0
if "%var%"=="" echo Cannot be empty and must be more than two characters & pause >nul & goto start
set var=%var:~0,4%
:1
set var2=%var:~0,1%
if "%var%"=="%var2%" echo Must enter more than two characters & pause >nul & goto start
:2
set var2=%var:~0,2%
if not "%var%"=="%var2%" goto 3
set mane=%var2%
goto end
:3
set var2=%var:~0,3%
if not "%var%"=="%var2%" goto 4
set var2=%var:~0,2%
set mane=%var2%
goto end
:4
set var2=%var:~0,4%
if not "%var%"=="%var2%" goto 5
set mane=%var2%
goto end
:5
echo Input parameter exception
pause
goto start
:end
echo %mane%

Wrote like this in the afternoon,
@echo off
:start
set var=
set /p var=Please enter two or four Chinese characters (will be automatically truncated to the largest even number of four characters):
if "%var%"=="" echo Cannot be empty and must be more than two characters & pause >nul & goto start
set var=%var:~0,4%
echo %var% | find "一二三" >nul && echo Reserved characters are not allowed && pause >nul && goto start

set var1=%var%一二三
set var2=%var1:~1,3%
if "%var2%"=="一二三" echo Must enter more than two characters & pause >nul & goto start

set var=%var:~0,4%
set var2=%var1:~0,4%
if "%var%"=="%var2%" set mane=%var2% & goto end

set var=%var:~0,2%
set var2=%var1:~0,2%
if "%var%"=="%var2%" set mane=%var2% & goto end

:end
echo %mane%

No way I completely don't know the for command
Floor11 bjsh Posted 2007-03-19 00:40
银牌会员 Posts 621 Credits 2,000
Originally posted by yangzhiyi at 2007-3-17 08:06 PM:
Thanks for the answer

You're welcome; everyone learns from each other and exchanges ideas


What's the use of setting count? I don't understand. Can you explain it in detail?

The role of setting count is to count the number of characters in the variable;

Use set x=%count%/2*2 to take even numbers

Use call echo %%var:~0,%x%%% to achieve the effect you want;
Floor12 zh159 Posted 2007-03-19 01:56
金牌会员 Posts 1,467 Credits 3,687
A simplified arbitrary-length character
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023