【问题标题】:I wanted to get sub string我想得到子字符串
【发布时间】:2014-11-19 19:25:55
【问题描述】:

目前我正在使用这样的代码

    while (fileName.endsWith(".csv")) {
        fileName = fileName.substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV));
        if (fileName.trim().isEmpty()) {
            throw new IllegalArgumentException();
        }
    }

当用户以小写字母(.csv)指定扩展名时,上面的代码工作正常,但是 windows 接受扩展名区分大小写,所以他可以给出 .CSV ,.CSV 等。我怎样才能改变上面的代码?

提前致谢

【问题讨论】:

标签: java string rcp


【解决方案1】:

为什么不把它转成小写呢?

while (fileName.toLowerCase().endsWith(".csv")) {
    fileName = fileName.substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
    if (fileName.trim().isEmpty()) {
        throw new IllegalArgumentException();
    }
}

【讨论】:

  • filename.lastIndexOf(...) 有同样的问题。这也必须转换为小写。除此之外:这就是答案!
  • @Seelenvirtuose 这是答案,除非您需要在文件名中保留大小写,这很有可能。
  • @ThomasStets 使用fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV),您不会更改变量。您只是在更改索引的计算。变量fileName 仍被原始 字符串(fileName.substring(...)) 的子字符串替换。
  • @ThomasStets:确实,这个答案不会颠覆 fileName 变量,因为字符串是不可变的。 1+ 到答案。
【解决方案2】:

深夜正则表达式解决方案:

Pattern pattern = Pattern.compile(".csv", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(fileName);
while (matcher.find()) {
    fileName = fileName.substring(0, matcher.start());
    if (fileName.trim().isEmpty()) {
        throw new IllegalArgumentException();
    }
}

Matcher 只会find() 一次。然后它可以将它的start 位置报告给substring 原始文件名。

【讨论】:

    【解决方案3】:

    你可以试试这个方法

     int lastIndexOfDot=fileName.lastIndexOf("\\.");
     String fileExtension=fileName.substring(lastIndexOfDot+1,fileName.length()); 
     while(fileExtension.equalsIgnoreCase(".csv")){
    
     } 
    

    或者

    while(fileName.toUpperCase().endsWith(".CSV"){}
    

    【讨论】:

      【解决方案4】:

      请转成小写再比较。

        while (fileName.toLowerCase().endsWith(".csv")) {
              fileName = fileName.toLowerCase().substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
              if (fileName.toLowerCase().trim().isEmpty()) {
                  throw new IllegalArgumentException();
              }
          }
      

      【讨论】:

        【解决方案5】:

        您可以将两者都转换为大写。

        所以改变这一行

        fileName = fileName.substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV));
        

        fileName = fileName.toUpperCase().substring(0, fileName.lastIndexOf(FILE_SUFFIX_CSV.toUpperCase()));
        

        【讨论】:

          【解决方案6】:

          使用这个实用功能:

          public static boolean endsWithIgnoreCase(String str, String suffix)
          {
              int suffixLength = suffix.length();
              return str.regionMatches(true, str.length() - suffixLength, suffix, 0, suffixLength);
          }
          

          现在你可以这样做了:

          while (endsWithIgnoreCase(fileName, ".csv")) {
              fileName = fileName.substring(0, fileName.toLowerCase().lastIndexOf(FILE_SUFFIX_CSV));
              if (fileName.trim().isEmpty()) {
                  throw new IllegalArgumentException();
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-11-12
            • 2019-07-09
            • 1970-01-01
            • 2017-03-11
            • 2016-06-26
            • 1970-01-01
            • 2019-05-02
            • 1970-01-01
            相关资源
            最近更新 更多