[英]How remove dynamic substring from string in selenium java
I want to remove sub string from my string which is dynamic date time.我想从我的字符串中删除子字符串,这是动态日期时间。
Example:例子:
Nov 19, 2019 05:41:08 AM EST美国东部时间 2019 年 11 月 19 日 05:41:08
I need:我需要:
Nov 19, 2019 05 AM EST this kind of string Nov 19, 2019 05 AM EST 这种字符串
I want to remove minutes and second from the string.我想从字符串中删除分钟和秒。
You may try a regex string replacement:您可以尝试正则表达式字符串替换:
String input = "Nov 19, 2019 05:41:08 AM EST";
String output = input.replaceAll("\\b(\\d{2}):\\d{2}:\\d{2}\\b", "$1");
System.out.println(output);
This prints:这打印:
Nov 19, 2019 05 AM EST
A perhaps more robust approach would be to go back to the Date
, LocalDate/LocalDateTime
which generated the current output and instead format using the new mask you want.一种可能更强大的方法是将 go 返回到Date
, LocalDate/LocalDateTime
,它生成了当前的 output ,而是使用您想要的新掩码进行格式化。
You can use regex to remove everything between the first :
and the first blank space您可以使用正则表达式删除第一个:
和第一个空格之间的所有内容
String original = "Nov 19, 2019 05:41:08 AM EST";
String stripped = original.replaceAll(":.*? ", " ");
System.out.print(stripped); // prints Nov 19, 2019 05 AM EST
If you are not comfortable with RegEx, you can use the following solution:如果您对 RegEx 不满意,可以使用以下解决方案:
public class Main {
public static void main(String[] args) {
String dateString="Nov 19, 2019 05:41:08 AM EST";
String requiredString=dateString.replace(dateString.substring(dateString.indexOf(':'),dateString.indexOf(' ',dateString.indexOf(':'))),"");
System.out.println(requiredString);
}
}
Output: Output:
Nov 19, 2019 05 AM EST
There are many other ways as well eg using DateTimeFormatter还有许多其他方法,例如使用DateTimeFormatter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.