这里是 String details
String details;
System.out.println(details); // gives the following :
"Address: 100 Main Street
City: CHICAGO State: IL Zip: 624324
Department ID: 890840809 ........
........................ "
我需要对其进行转换,State
然后Zip
从新行开始
Address: 100 Main Street
City: CHICAGO
State: IL
Zip: 624324
Department ID: 890840809 ........
这是我尝试过的
try {details = details.replaceAll(" State:.*", "\nState:.*");
} catch (Exception e) {}
try {details = details.replaceAll(" Zip:.*", "\nZip:.*");
} catch (Exception e) {}
您几乎完全正确,您需要进行一些小的修改:
try {details = details.replaceAll(" State:(.*)", "\nState:$1");
^^^^ ^^
} catch (Exception e) {}
try {details = details.replaceAll(" Zip:(.*)", "\nZip:$1");
^^^^ ^^
} catch (Exception e) {}
注意更改,您需要使用捕获组来捕获值,()
以便可以通过来在替换字符串中使用它们$1
。
这是一个使用PHP的Regex101演示,但是概念是相同的,请注意现在一切正常。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句