就第一个字符此code运行 [英] Make this code run on first character

查看:175
本文介绍了就第一个字符此code运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一些批处理code,需要一个变量,并将其转换为大写字符。也就是说,约书亚将成为JOSHUA。我一直在阅读中使用的命令的文档,我不能工作了相当如何code的作品,但我想修改code,因此只大写第一个字符。即,约书亚约书亚。我想这仅仅是修改循环的问题(?)的code使用。任何帮助或建议AP preciated。

I was given some batch code that takes a variable and converts it to uppercase characters. ie, joshua will become JOSHUA. I've been reading the documentation for the commands used and I cannot work out quite how the code works, but I would like to amend the code so it only capitalizes the first character. ie, joshua to Joshua. I assume it is just a matter of modifying the 'loop' (?) that the code uses. Any help or advice appreciated.

code -

 :toUpper str -- converts lowercase character to uppercase
 if not defined %~1 EXIT /b
 for %%a in ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I"
        "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R"
        "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z" "ä=Ä"
        "ö=Ö" "ü=Ü") do (
call set %~1=%%%~1:%%~a%%
)
EXIT /b

请注意:我找不到的SET帮助文档中的多个%招牌的含义任何引用。我猜这是哪里的关键问题所在。

Note: I could not find any reference to the meaning of the multiple % signs in the SET help documentation. I am guessing this is where the key to the problem lies.

干杯

编辑:如果有人能够给什么是在code那将是非常美妙也将上一个非常简短的解释!据我所知,只有部分被替换小写字母为大写字母

If someone could give a very brief explanation of what is going on in the code that would be fantastic also! The only part I understand is it is substituting the lower case letters for an uppercase letter

推荐答案

资本化的第一个字母,需要不同的方法:

Capitalizing first letter will require different approach:

@echo off


set "name=npocmaka"
echo before firstToUpper - %name%

call ::firstToUpper name

echo after firstToUpper - %name%
exit /b 0


:firstToUpper var
setlocal enableDelayedExpansion
set "name=!%~1!"

set first_letter=%name:~0,1%
set last_letters=%name:~1%

for %%# in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
    set first_letter=!first_letter:%%#=%%#!
)

set "name=%first_letter%%last_letters%"

endlocal && (
    set "%~1=%name%"
)
exit /b %errorlevel%

要了解这一点,你可能需要看看推迟扩张,的变量子,的字符串替换。请注意,当更换搜索信不区分大小写,但替代本身就是

To understand this you might need to look at delayed expansion, variable substring , string replacement. Notice that when replacing search of the letter is not case sensitive but replacement itself is

这篇关于就第一个字符此code运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆