【问题标题】:Remove the String value using Java [closed]使用 Java 删除字符串值 [关闭]
【发布时间】:2016-07-14 10:29:25
【问题描述】:

我需要从 String 变量中删除像 cat 和 fish 这样的 String 值。 我做了一些示例 Java 代码来删除字符串值。

public static void deleteAccNode(String refId){
    String refId [] = {"Fish","Dog","Dolphins","rowboat","Fish","sailor","Cat","cats","Cat","Fish",
        "Fish","Tree","Fishes","Cat","Cat","CAT","Cat"};

        try {
            System.out.println("GeneralLength"+refId.length);
            for (int n = refId.length; n <= 1; ){
            if(refId.equals("Cat")){
               System.out.println("cat : :"+refId);
                How to remove the String value?remove("Cat");
                }else if(refId.equals("Fish")){
                    System.out.println("Fish : :"+refId);
                    How to remove the String value?remove("Fish");
                }
            n--;
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
}

需要的输出:Dog,Dolphins,rowboat,sailor,cats, Tree, Fishes,CAT 但是,当前输出:NullPointerException

【问题讨论】:

  • 那么...是什么阻止了你?
  • 到底是什么问题?
  • String = "Fish","Dog","Dolphins","rowboat","Fish","sailor","Cat","cats","Cat","Fish", +"Fish","Tree","Fishes","Cat","Cat","CAT","Cat"; 以前从未见过这样的事情。我不认为它编译?
  • @ifly6 不,不会的。

标签: java string-matching


【解决方案1】:

这只是一种方法,还有很多,你可以用谷歌搜索一下。

  String[] stringArray = { "Cat", "Fish", "CatFish", "Elephant" };
  final List<String> newArrayList = new ArrayList<>();
  for ( final String string : stringArray )
  {
    if ( !(string.equals( "Cat" ) || string.equals( "Fish" )) )
    {
      newArrayList.add( string );
    }
  }

  stringArray = (String[]) newArrayList.toArray();

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 1970-01-01
    • 2017-12-30
    • 2015-11-04
    • 1970-01-01
    • 2013-04-16
    • 2015-08-29
    • 2012-10-02
    相关资源
    最近更新 更多