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-07-31 12:39
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to get the number of characters and intercept characters View 2,597 Replies 11
Original Poster Posted 2007-03-17 09:39 ·  中国 广东 茂名 电信
中级用户
★★
Credits 261
Posts 123
Joined 2006-06-06 19:23
20-year member
UID 56648
Status Offline
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
Floor 2 Posted 2007-03-17 11:40 ·  中国 山东 济南 电信
初级用户
Credits 125
Posts 44
Joined 2007-01-24 15:31
19-year member
UID 77555
Gender Female
Status Offline
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:


@ECHO %DBG% OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET STR=一二三四五六七八九十1234,.FJSFJSFGDFG
CALL :JIS 0 1
GOTO :EOF

:JIS
FOR /L %%i IN (%1,1,%2) DO (
IF "TMP!STR:~%%i!"=="TMP" ECHO %%i
IF "TMP!STR:~%%i!" NEQ "TMP" (
SET /A g=%%i+1,h=%%i+2
CALL :JIS !g! !h!
)
GOTO :EOF
)


--------------------------------------------
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 ]
Floor 3 Posted 2007-03-18 03:50 ·  中国 广东 茂名 电信
中级用户
★★
Credits 261
Posts 123
Joined 2006-06-06 19:23
20-year member
UID 56648
Status Offline
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"?
Floor 4 Posted 2007-03-18 04:38 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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
Floor 5 Posted 2007-03-18 04:54 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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
Floor 6 Posted 2007-03-18 05:15 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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 ]
Floor 7 Posted 2007-03-18 05:50 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
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???
Floor 8 Posted 2007-03-18 07:14 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
Okay;
set var=%var:~1%
Floor 9 Posted 2007-03-18 09:06 ·  中国 广东 茂名 电信
中级用户
★★
Credits 261
Posts 123
Joined 2006-06-06 19:23
20-year member
UID 56648
Status Offline
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
Floor 10 Posted 2007-03-18 09:18 ·  中国 广东 茂名 电信
中级用户
★★
Credits 261
Posts 123
Joined 2006-06-06 19:23
20-year member
UID 56648
Status Offline
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
Floor 11 Posted 2007-03-19 00:40 ·  中国 浙江 杭州 华数宽带
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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;
Floor 12 Posted 2007-03-19 01:56 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
A simplified arbitrary-length character
@echo off
set var=
set /p var=Please enter:
set var_bak=%var%
set count=0

:loop
if not "%var%"=="" set var=%var:~1%&set /a count+=1&goto loop

set /a count=%count%/2*2
if %count% GTR 1 (call echo Truncated to: %%var_bak:~0,%count%%%) else echo Must enter two or more characters
pause
exit
Forum Jump: