如何避免大小写敏感的返回

how to avoid return of case sensitive

本文关键字:返回 大小写敏感 何避免      更新时间:2024-06-18

我使用以下代码及其仅用于区分大小写的值,我想要一些如何克服的问题,如果用户使用大写或小写它也会起作用,我该怎么做?

oAc.setFilterFunction(function(sValue, oItem) {
    debugger;
    var val = (oItem.getText().indexOf(sValue) != -1) || (oItem.getAdditionalText().indexOf(sValue) != -1);
    return val;
});

在sValue中,我得到了值,val返回true或false在oItem.getText()中,我得到了要比较的文本,就像我在sValue=I中一样并且oItem.getText()返回索引该方法返回true。

一种简单的方法是在indexOf()操作之前将两个值转换为小写/大写

oAc.setFilterFunction(function (sValue, oItem) {
    debugger;
    sValue = sValue.toLowerCase();
    var val = (oItem.getText().toLowerCase().indexOf(sValue) != -1) || (oItem.getAdditionalText().toLowerCase().indexOf(sValue) != -1);
    return val;
});

另一种解决方案是使用正则表达式来测试值,而不是使用indexOf

if (!RegExp.escape) {
    RegExp.escape = function (value) {
        return value.replace(/['-'[']{}()*+?.,'''^$|#'s]/g, "''$&")
    };
}
oAc.setFilterFunction(function (sValue, oItem) {
    debugger;
    var regex = new RegExp(RegExp.escape(sValue), 'i');
    var val = regex.test(oItem.getText()) || regex.test(oItem.getAdditionalText());
    return val;
});

您可以将文本转换为小写。

oAc.setFilterFunction(function(sValue, oItem) {
    debugger;
    var val = (oItem.getText().toLowerCase().indexOf(sValue.toLowerCase()) != -1) || (oItem.getAdditionalText().toLowerCase().indexOf(sValue.toLowerCase()) != -1);
    return val;
})

您可以在名为toLowerCase()或toUpperCase()的String类中查看Java的内置函数

更多信息点击这里

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html