如果在Fortran中检查涉及多个字符串,则压缩 [英] Compact if check involving multiple strings in Fortran
本文介绍了如果在Fortran中检查涉及多个字符串,则压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有办法一起检查一个字符串是否等于列表中的任何一个,而不是分别用==
进行显式检查?例如,
Is there a way to check if a string is equal to any of the list together instead of individually explicitly check with a ==
? For example,
if(color=='violet' .or. color=='indigo' .or. color=='blue' .or.&
color=='green' .or. color=='yellow' .or. color=='orange' .or. color=='red') then
print *, "It is a rainbow color"
end if
有没有一种紧凑的方法可以做到这一点?类似于if(color=='violet|indigo|blue|green|yellow|orange|red')
吗?
Is there a way compact way to do this? Something like if(color=='violet|indigo|blue|green|yellow|orange|red')
?
推荐答案
您可以将颜色放入数组中并使用any
.
You can put the colors into an array and use any
.
if (any(color == [character(6) :: "violet","indigo","blue","green","yellow","orange","red"]))
这篇关于如果在Fortran中检查涉及多个字符串,则压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文