How can I format string like this:
我该如何格式化这样的字符串:
"8:00- 8:45"
to
"08:00-08:45"
I think I should use something like data formatter but I don't know how to use it.
我想我应该使用数据格式化程序,但我不知道如何使用它。
1 个解决方案
#1
1
Try to use regex like this :
尝试使用这样的正则表达式:
String str = "8:00- 8:45";
str = str.replace(" ", "").replaceAll("(\\b\\d\\b)", "0$1");
System.out.println(str);//outputs 08:00-08:45
details
- first
.replace(" ", "")
will remove all the spaces - second you can use Word Boundaries to put 0 before each unique degit, in your case before
8
->08
in strings like8:3- 08:5
->08:03-08:05
regex demo
首先.replace(“”,“”)将删除所有空格
第二,你可以使用Word Boundaries在每个唯一的degit之前放0,在你的情况下8 - > 08之前的字符串如8:3- 08:5 - > 08:03-08:05 regex demo
#1
1
Try to use regex like this :
尝试使用这样的正则表达式:
String str = "8:00- 8:45";
str = str.replace(" ", "").replaceAll("(\\b\\d\\b)", "0$1");
System.out.println(str);//outputs 08:00-08:45
details
- first
.replace(" ", "")
will remove all the spaces - second you can use Word Boundaries to put 0 before each unique degit, in your case before
8
->08
in strings like8:3- 08:5
->08:03-08:05
regex demo
首先.replace(“”,“”)将删除所有空格
第二,你可以使用Word Boundaries在每个唯一的degit之前放0,在你的情况下8 - > 08之前的字符串如8:3- 08:5 - > 08:03-08:05 regex demo