【问题标题】:Split the string from first Upper case letter从第一个大写字母拆分字符串
【发布时间】:2021-03-10 00:53:14
【问题描述】:

我想使用 java 从这个文本中提取文件名和时间戳

invoice_12890.pdf2019 年 4 月 10 日上午 8:33:16

期待

  • invoice_12890.pdf
  • 2019 年 4 月 10 日上午 8:33:16

【问题讨论】:

  • split("(?<=\\p{Ll})(?=\\p{Lu})") 即在小写字母和大写字母之间的零宽度空间上分割。
  • 检查第一个大写字符。
  • 观察到有时文件名以大写开头

标签: java string split char character


【解决方案1】:
boolean hadDot=false;//this makes sure we don't split before finding the file extension
String file="",date="";
for(int i=0;i<text.length();i++){
  if(text.charAt(i)=='.'){
    hadDot=true;
    continue;
  }
  if(hadDot&&Character.isUpperCase(text.charAt(i))){
    file=text.substring(0,i);
    date=text.substring(i);
    break;
  }
}

【讨论】:

  • if(hadDot&&Character.isUpperCase(text.charAt(i))) 这个条件总是返回false
  • 修复了我的回答中的问题;我用 break 而不是 continue
  • 你的逻辑很好地解决了这个问题。我希望我能成为像你一样的程序员。
  • 这解决了我的问题,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 2019-08-08
相关资源
最近更新 更多