创建不区分大小写的if语句的最佳方法是什么?
例如,如果我有<img src="https://cdn2.iconfinder.com/data/icons/mixed-vol-7/74/Untitled-1-11-08-07-17-512.png" onClick="maximizeorMinimize(this)" width="15" height="15" style="margin-left: 55%;" title="Maximize">
<table id="maximizableTable">
<tr><td>Text</td><td>Text</td></tr>
<tr><td>Text</td><td>Text</td></tr>
<tr><td>Text</td><td>Text</td></tr>
<tr><td>Text</td><td>Text</td></tr>
</table>
<script>
var table = document.getElementById("maximizableTable");
var maximized = false;
function maximizeorMinimize(elem){
if(!maximized){
table.style.width = "100%";
table.style.height = "100%";
table.style.top = "20%";
table.style.left = "0";
table.style.fontSize = "2em";
elem.style.marginLeft = "60%";
elem.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAY1BMVEX///8AAADo6OgwMDB0dHTCwsI3NzdwcHAgICDU1NRhYWGWlpZLS0va2tqSkpLHx8e5ublZWVknJyfe3t5BQUGjo6NmZmYXFxf19fVTU1NcXFympqavr68YGBgODg62trZ7e3vm1eqLAAACtUlEQVR4nO3d7VLiQBBGYT5CICQIsqIssuj9X+UWtaXlTK2ka7rdd9Y6zwUMc0xiBn/YkwkAAAAAAAAAAMB/aNs3QzvXaoem335N3mI9rcd6Ed53uKijMpdDaF+3Ugf9xX4TF3injvlEHxVY0wOYeowJfFB33DBEBNZ7Ba8CrmKtz+Ab97PYqQtGeX+j7tUBo1pf4LN6/wZnV6F69xYrT+CLevcmnjPqkC922nUzrW53n29qXR54zJba/3D8tOJs8lNy+VKLdKHVMW6XLsfsi05XvFL2tq/jCl5lb+nyt356YHPc7uF+Jjt7LV6nTdbZhe3P75zsrPz8Pa/0Js1v0/JjTVo4C9yh1yzZ2bx4HQp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCKwp1KLSiUIdCK3tht4h367+S/vvC5TTesqrC5gsKGwoppJBCCimkkEIKgwu//7n0sGyiLW8NVuP7oRWFOhRaUahDoRWFOhRaUahDoRWFOhRaUahDoRWFOlGF6YRH73joSJtkZ+XzD9O58S+BO/RKBxU/FK+T/p33FLhDr3Qm8K2/Hd/WJ+tUNKYzvUkds2S36UL1zAN+Sje2LV8qXegbznTOxu5OK53Lfe8o3OWLVckzW31yGV9f7pcnMJudXKezq3CyV+9/VPmR7Y9u/CPE3IfJu/HPkCp/279bj3+K0KM/MD9/16V8bnyi3qsYcgWvan0WA57BN11+EqzBPvYr+UHdk3t6Du272p3UUR+sXWfRz237ZmjnWu3Q9I7vgwAAAAAAAAAAADq/AeDROLr9IlTcAAAAAElFTkSuQmCC";
elem.width = "50";
elem.height = "50";
elem.title = "Minimize";
maximized = true;
} else {
table.style.width = "";
table.style.height = "";
table.style.top = "";
table.style.left = "";
table.style.fontSize = "";
elem.style.marginLeft = "55%";
elem.title="Maximize";
elem.width = "15";
elem.height = "15";
elem.src = "https://cdn2.iconfinder.com/data/icons/mixed-vol-7/74/Untitled-1-11-08-07-17-512.png";
maximized = false;
}
}
</script>
var = 'ignore'
将失败。
一些可以通过的选项包括:
if(var == 'Ignore'){print("pass")} else{print("fail")}
还有其他好的选择吗?有没有最佳选择?
我不确定应该如何衡量“最佳”
答案 0 :(得分:2)
我们也可以将grepl
与ignore.case = T
一起使用
var <- "ignore"
if (grepl("Ignore", var, ignore.case = T)) "Pass" else "Fail"
#[1] "Pass"
或将stringr::str_detect
与fixed(pattern, ignore_case = T)
一起使用
library(stringr)
if (str_detect(var, fixed("Ignore", ignore_case = T))) "Pass" else "Fail"
#[1] "Pass"