有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    使用这个功能怎么样?似乎比正则表达式工作得更快

    public static String trimPoints(String txt)
        {
            char[] cs = txt.toCharArray();
            int index =0;
            for(int x =cs.length-1;x>=0;x )
            {
                if(cs[x]=='.')
                    continue;
                else
                    {
                    index = x+1;
                    break;
                    }
            }
    
            return txt.substring(0,index);
        }
    
  2. # 2 楼答案

    试试这个。它使用正则表达式将字符串末尾的所有点替换为空字符串

    yourString.replaceAll("\\.+$", "");
    
  3. # 3 楼答案

    可以这样做以删除所有.

    String a = "abc.....";
    String new = a.replaceAll("[.]", "");
    

    仅删除后面的.

    String new = a.replaceAll("//.+$","");
    

    编辑:查看评论。要删除最后的n{}

    int dotsToRemove = 5; // whatever value n
    String new = a.substring(0, s.length()-dotsToRemove);