如何去掉小数点后的space而不去掉其他的space?
How to remove space after the decimal point without removing other spaces?
我有一个 String
在单个字符串中有多个值。我只想删除小数点后的 space,而不从字符串中删除其他 space。
String testString = "EB:3668. 19KWh DB:22. 29KWh";
testString = testString.trim();
String beforeDecimal = testString.substring(0, testString.indexOf("."));
String afterDecimal = testString.substring(testString.indexOf("."));
afterDecimal = afterDecimal.replaceAll("\s+", "");
testString = beforeDecimal + afterDecimal;
textView.setText(testString);
在我的字符串中,单个字符串中有两个值
EB:3668. 19KWh
和 DB:22. 29KWh
。
我只想删除小数点后的 space 并使 String
像这样:
EB:3668.19KWh DB:22.29KWh
只需使用string.replaceAll("\. ", ".");
感谢 Henry 指出我必须逃离 .
您可以使用 2 个捕获组并匹配它们之间的 space。在替换中使用没有 space.
的 2 组
(\d+\.)\h+(\d+)
String testString="EB:3668. 19KWh DB:22. 29KWh";
String afterDecimal = testString.replaceAll("(\d+\.)\h+(\d+)","");
System.out.println(afterDecimal);
输出
EB:3668.19KWh DB:22.29KWh
或者更具体的模式可以包括千瓦时:
\b(\d+\.)\h+(\d+KWh)
我现在不在编辑器前,但是你不能在一行中使用 replaceAll 方法而不打断它吗?
var text = testString.replaceAll(". ", ".");
您可以删除小数点和小数部分之间不必要的空格,如下所示。此代码还删除了其他额外的空格:
String testString = " EB:3668. 19KWh DB:22. 29KWh ";
String test2 = testString
// remove leading and trailing spaces
.trim()
// replace non-empty sequences of space
// characters with a single space
.replaceAll("\s+", " ")
// remove spaces between the decimal
// point and the fractional part
// regex groups:
// (\d\.) - - digit and point
// ( ) - - space
// (\d) - - digit
.replaceAll("(\d\.)( )(\d)", "");
System.out.println(test2); //EB:3668.19KWh DB:22.29KWh
另请参阅:How do I remove all whitespaces from a string?
我有一个 String
在单个字符串中有多个值。我只想删除小数点后的 space,而不从字符串中删除其他 space。
String testString = "EB:3668. 19KWh DB:22. 29KWh";
testString = testString.trim();
String beforeDecimal = testString.substring(0, testString.indexOf("."));
String afterDecimal = testString.substring(testString.indexOf("."));
afterDecimal = afterDecimal.replaceAll("\s+", "");
testString = beforeDecimal + afterDecimal;
textView.setText(testString);
在我的字符串中,单个字符串中有两个值
EB:3668. 19KWh
和 DB:22. 29KWh
。
我只想删除小数点后的 space 并使 String
像这样:
EB:3668.19KWh DB:22.29KWh
只需使用string.replaceAll("\. ", ".");
感谢 Henry 指出我必须逃离 .
您可以使用 2 个捕获组并匹配它们之间的 space。在替换中使用没有 space.
的 2 组(\d+\.)\h+(\d+)
String testString="EB:3668. 19KWh DB:22. 29KWh";
String afterDecimal = testString.replaceAll("(\d+\.)\h+(\d+)","");
System.out.println(afterDecimal);
输出
EB:3668.19KWh DB:22.29KWh
或者更具体的模式可以包括千瓦时:
\b(\d+\.)\h+(\d+KWh)
我现在不在编辑器前,但是你不能在一行中使用 replaceAll 方法而不打断它吗?
var text = testString.replaceAll(". ", ".");
您可以删除小数点和小数部分之间不必要的空格,如下所示。此代码还删除了其他额外的空格:
String testString = " EB:3668. 19KWh DB:22. 29KWh ";
String test2 = testString
// remove leading and trailing spaces
.trim()
// replace non-empty sequences of space
// characters with a single space
.replaceAll("\s+", " ")
// remove spaces between the decimal
// point and the fractional part
// regex groups:
// (\d\.) - - digit and point
// ( ) - - space
// (\d) - - digit
.replaceAll("(\d\.)( )(\d)", "");
System.out.println(test2); //EB:3668.19KWh DB:22.29KWh
另请参阅:How do I remove all whitespaces from a string?