|
yangzhiyi
中级用户
  
积分 261
发帖 123
注册 2006-6-6
状态 离线
|
『楼 主』:
[求助]如何获得字符的字数和截取字符
使用 LLM 解释/回答一下
截取偶字数且最大为八个字数应该怎么做?
例如输入:一二三四五六七八九十 截取为:一二三四五六七八
例如输入:一二三四五六七八 截取为:一二三四五六七八
例如输入:一二三四五六七 截取为:一二三四五六
例如输入:一二三四五六 截取为:一二三四五六
例如输入:一二三四五 截取为:一二三四
例如输入:一二三四 截取为:一二三四
例如输入:一二三 截取为:一二
例如输入:一二 截取为:一二
例如输入:一 提示为:必须输入两个字以上
例如输入: 提示为:必须输入且两个字以上
截取和提示我都会做,只是不会得到字符个数
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
|
|
2007-3-17 09:39 |
|
|
qjbm
初级用户
 
积分 125
发帖 44
注册 2007-1-24
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
对于在批处理中计算变量中字符个数需要编写一整套的"函数"来实现
特殊字符是批处理永远说不出的痛......
下面是一个拙例:
@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
)
--------------------------------------------
呵呵:)~~
看样子果然是我把问题复杂化了,
还是做看客悄悄学习的比较少受批评:)
当时见此贴浏览次数超过70+无人回复,
且有讨论价值,沉了比较可惜才顶了一下,真是不好意思了...
-------------------------------------------
天天来,天天哭...........
歇段时间再说吧........
Last edited by qjbm on 2007-3-18 at 03:07 PM ]
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 ]
|
|
2007-3-17 11:40 |
|
|
yangzhiyi
中级用户
  
积分 261
发帖 123
注册 2006-6-6
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
能解释一下吗,看不明白
我运行后得到的是 27 啊,不对啊
我想做的只有中文,没特殊字符
再问下两个字符串连接起来如: 字符:一三 和字符:12 得到 一三12 ?
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"?
|
|
2007-3-18 03:50 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
二楼的数字符的也太复杂了吧;
@echo off
set /p var=请输入:
set count=0
:loop
if "%var%"=="" goto jump
set var=%var:~0,-1%
set /a count=%count%+1
goto loop
:jump
echo %count%
pause
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
|
|
2007-3-18 04:38 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
回答lz的问题
对于任意长度的字符的代码
@echo off
set /p var=请输入:
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 必须输入两个字以上 && goto end
call echo 截取为:%%var_bak:~0,%x%%%
:end
对于lz要求长度的代码
@echo off
set /p var=请输入:
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 必须输入两个字以上 && goto end
call echo 截取为:%%var_bak:~0,%x%%%
:end
两个的差别只有
if %x% GEQ 8 set x=8
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
|
|
2007-3-18 04:54 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
对于特殊字符有这样的有趣的现象
例如
请输入:^1245
4 这里是数的字符个数;前面加了^后面居然数成了4
截取为:1245
请输入:1234567&89。 这里中间加了个&后面出了这么一大片错
'89' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
'8' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
8 这里是数的字符个数,显然&被算入在内了
截取为:1234567
请输入:1234567&
8
截取为:1234567 本来程序是输出偶数个的输出了7个;也就是说&算"隐藏"了
Last edited by bjsh on 2007-3-17 at 04:16 PM ]
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 ]
|
|
2007-3-18 05:15 |
|
|
xycoordinate
中级用户
  
积分 493
发帖 228
注册 2007-2-16 来自 安徽
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by bjsh at 2007-3-17 15:38:
二楼的数字符的也太复杂了吧;
@echo off
set /p var=请输入:
set count=0
:loop
if "%var%"=="" (
echo %count%
goto :eof
)
set var=%var:~0,-1%
set /a count+=1
goto loop
pause
的确比2楼的容易理解!
我想问:
能从var变量前面截取,然后再判断吗???
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???
|
|
2007-3-18 05:50 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
|
2007-3-18 07:14 |
|
|
yangzhiyi
中级用户
  
积分 261
发帖 123
注册 2006-6-6
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
谢谢解答
设置 count 有什么用啊,没看明白,能解释详细点吗
@echo off
set /p var=请输入:
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 必须输入两个字以上 && goto end
call echo 截取为:%%var_bak:~0,%x%%%
:end
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
|
|
2007-3-18 09:06 |
|
|
yangzhiyi
中级用户
  
积分 261
发帖 123
注册 2006-6-6
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
一开始我写的是这个样,想想下太长,还是五楼的代码好。
@echo off
:start
set var=
set /p var=请输入:
:0
if "%var%"=="" echo 不能为空且要两个字以上 & pause >nul & goto start
set var=%var:~0,4%
:1
set var2=%var:~0,1%
if "%var%"=="%var2%" echo 必须输入两个字以上 & 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 输入参数异常
pause
goto start
:end
echo %mane%
下午再写成这样,
@echo off
:start
set var=
set /p var=请输入两个或四个中文字(会自动截取成最大四个字的偶字数):
if "%var%"=="" echo 不能为空且要两个字以上 & pause >nul & goto start
set var=%var:~0,4%
echo %var% | find "一二三" >nul && echo 保留字符不允许使用 && pause >nul && goto start
set var1=%var%一二三
set var2=%var1:~1,3%
if "%var2%"=="一二三" echo 必须输入两个字以上 & 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%
没办法我完全不会 for 命令
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
|
|
2007-3-18 09:18 |
|
|
bjsh
银牌会员
    
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by yangzhiyi at 2007-3-17 08:06 PM:
谢谢解答
别客气;大家相互学习相互交流吗
设置 count 有什么用啊,没看明白,能解释详细点吗
设置count的作用是为了数变量中字符的个数;
利用set x=%count%/2*2 取偶数
利用 call echo %%var:~0,%x%%%来试现你要的效果;
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;
|
|
2007-3-19 00:40 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
来个简化些的任意长字符
@echo off
set var=
set /p var=请输入:
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 截取为: %%var_bak:~0,%count%%%) else echo 必须输入两个字以上
pause
exit
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
|
|
2007-3-19 01:56 |
|