我不知道 gnuplot 提供大写或小写功能。下面的尝试不是一个函数,而是一个宏,它可以将字符串变量w
转换为大写或小写,而无需 OP 所需的外部工具。未在列表中找到的字符保持不变。可能还有改进的余地,但也许它仍然会对某人有所帮助。
### gnuplot implementation of uppercase and lowercase
reset session
Cases= "ABCDEFGHIJKLMNOPQRSTUVWXYZ".\
"abcdefghijklmnopqrstuvwxyz"
uppercase = 'CaseLen=strlen(Cases)/2;\
wc = ""; \
do for [i=1:strlen(w)] { \
tmp1 = substr(w,i,i); \
tmp2 = strstrt(Cases,tmp1); \
wc = (tmp2 == 0) ? wc = wc.tmp1 : \
(tmp2 > CaseLen) ? (tmp2=tmp2-CaseLen, \
wc.substr(Cases,tmp2,tmp2)) : wc.substr(Cases,tmp2,tmp2);\
}; w = wc'
lowercase = 'CaseLen=strlen(Cases)/2;\
wc = ""; \
do for [i=1:strlen(w)] { \
tmp1 = substr(w,i,i); \
tmp2 = strstrt(Cases,tmp1); \
wc = (tmp2 == 0) ? wc.tmp1 : \
(tmp2 < CaseLen) ? (tmp2=tmp2+CaseLen, \
wc.substr(Cases,tmp2,tmp2)) : wc.substr(Cases,tmp2,tmp2);\
}; w = wc'
# put your string into variable w
w = "...thE qUick brOWn foX jUmPs oVeR The LazY Dog!"
print w
# run the macro uppercase and variable w will we converted to uppercase
@uppercase
print w
# run the macro lowercase and variable w will we converted to lowercase
@lowercase
print w
### end of code
输出:
...thE qUick brOWn foX jUmPs oVeR The LazY Dog!
...THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!
...the quick brown fox jumps over the lazy dog!
补充:改进版
我想这就是 SO 的精神......一起找到最好的解决方案:-)。@mjp,使用您关于在末尾添加字符的好主意以及我将求和表达式“滥用”sum
为循环的“新”想法,从而避免递归。以下解决方案没有递归解决方案具有的字符串的 247 个字符的限制(至少在我的计算机上是 247)。我希望这个解决方案不会有严重的限制。
### gnuplot implementation of uppercase and lowercase
# theozh, 14.01.2019
reset session
UpperCases= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LowerCases= "abcdefghijklmnopqrstuvwxyz"
# upper/lowercase for characters
ucchar(c) = substr( UpperCases.c, ucchar_tmp=strstrt(LowerCases.c, c), ucchar_tmp)
lcchar(c) = substr( LowerCases.c, lcchar_tmp=strstrt(UpperCases.c, c), lcchar_tmp)
# upper/lowercase for strings
uc(s) = ((sum[uc_i=1:strlen(s)] (uc_tmp=ucchar(substr(s,uc_i,uc_i)), uc_i>1 ? (uc_w=uc_w.uc_tmp,1):uc_w=uc_tmp,1)),uc_w)
lc(s) = ((sum[lc_i=1:strlen(s)] (lc_tmp=lcchar(substr(s,lc_i,lc_i)), lc_i>1 ? (lc_w=lc_w.lc_tmp,1):lc_w=lc_tmp,1)),lc_w)
s = "...thE qUick brOWn foX jUmPs oVeR The LazY Dog!"
print s
print uc(s)
print lc(s)
### end of code
编辑:(2022 年 1 月)我再次偶然发现我的旧帖子,并认为它可以进一步简化。并且更多字符被添加到查找字符串中。
代码:
### uppercase/lowercase function for gnuplot
reset session
UpperCases= "ABCDEFGHIJKLMNOPQRSTUVWXYZÄÁÂÀËÉÊÈÏÍÎÌÖÓÔÒÜÚÛÙŸÝ"
LowerCases= "abcdefghijklmnopqrstuvwxyzäáâàëéêèïíîìöóôòüúûùÿý"
# upper/lowercase for characters
ucc(c) = ((ucc_i=strstrt(LowerCases, c)) ? UpperCases[ucc_i:ucc_i] : c)
lcc(c) = ((lcc_i=strstrt(UpperCases, c)) ? LowerCases[lcc_i:lcc_i] : c)
# upper/lowercase for strings
uc(s) = (uc_s='', sum[uc_i=1:strlen(s)] (uc_s=uc_s.ucc(s[uc_i:uc_i]),1),uc_s)
lc(s) = (lc_s='', sum[lc_i=1:strlen(s)] (lc_s=lc_s.lcc(s[lc_i:lc_i]),1),lc_s)
s = "...thE qUick brOWn foX jUmPs oVeR The LazY Dog!"
print "Input: ", s
print "Uppercase: ", uc(s)
print "Lowercase: ", lc(s)
### end of code
结果:
Input: ...thE qUick brOWn foX jUmPs oVeR The LazY Dog!
Uppercase: ...THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG!
Lowercase: ...the quick brown fox jumps over the lazy dog!