gpt4 book ai didi

Java:字符串:是否有更好的方法来比较字符串

转载 作者:行者123 更新时间:2023-12-03 05:53:51 24 4
gpt4 key购买 nike

今天早上我感到很好奇,并且想知道是否有人有更好的方法来做到这一点

if(TAG_PLAY.equalsIgnoreCase(e.getActionCommand())
||TAG_PASSWORD.equalsIgnoreCase(e.getActionCommand())
||...
){

我有预感,这可以通过做类似的事情来改进创建 1 个大字符串并在其中查找 e.getActionCommand()但我不知道这样是否会更有效率

注意:这与 getActionCommand 无关,我纯粹对逻辑、性能和做同样事情的新方法/模式感兴趣

<小时/>

编辑:我不考虑大小写的争论^^

<小时/>

编辑:

这个怎么样:

s = TAG_PLAY+","+TAG_PASSWORD;
//compareToIgnoreCase is not optimal since it will go through all the String
if(0!=s.compareToIgnoreCase(anotherString)){

最佳答案

您是否考虑过使用 Set.contains(Object)?

例如:

  Set<String> cases = new HashSet<String>();
cases.add( TAG_PLAY.toLowerCase() );
cases.add( TAG_PASSWORD.toLowerCase() );

...
if ( cases.contains( e.getActionCommand().toLowerCase() ) {
...

关于Java:字符串:是否有更好的方法来比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4446582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com