在 java 中使用正则表达式将 space 放在整数和 '/' 之间
putting space between integer and '/' using regex in java
我有一个字符串 S= "grep -iRl 2020062312213461801003087/var/seamless/spool/tdr/Reconciliation"
我想在最后一个数字和 "/"
之间添加 space
就像我希望我的输出是 grep -iRl 2020062312213461801003087 /var/seamless/spool/tdr/Reconciliation
试试这个。
String S= "grep -iRl 2020062312213461801003087/var/seamless/spool/tdr/Reconciliation";
String replaced = S.replaceAll("(\d)(/)", " ");
System.out.println(replaced);
结果:
grep -iRl 2020062312213461801003087 /var/seamless/spool/tdr/Reconciliation
您可以通过 yourstring.indexOf("/"); 找到第一个“/”的索引。并通过 yourString.insert(“/”的索引); 将 char 插入到字符串中。
你的代码将是这样的
`
int index = yourString.indexOf("/");
yourString.insert(index, " ");
我有一个字符串 S= "grep -iRl 2020062312213461801003087/var/seamless/spool/tdr/Reconciliation"
我想在最后一个数字和 "/"
就像我希望我的输出是 grep -iRl 2020062312213461801003087 /var/seamless/spool/tdr/Reconciliation
试试这个。
String S= "grep -iRl 2020062312213461801003087/var/seamless/spool/tdr/Reconciliation";
String replaced = S.replaceAll("(\d)(/)", " ");
System.out.println(replaced);
结果:
grep -iRl 2020062312213461801003087 /var/seamless/spool/tdr/Reconciliation
您可以通过 yourstring.indexOf("/"); 找到第一个“/”的索引。并通过 yourString.insert(“/”的索引); 将 char 插入到字符串中。 你的代码将是这样的 `
int index = yourString.indexOf("/");
yourString.insert(index, " ");