- //第一种方式
- import java.util.regex.Pattern;
- import java.util.regex.Matcher;
-
- ...
- // 4 different combinaisons
- Pattern CRLF = Pattern.compile("(
|
|
|
)");
- Matcher m = CRLF.matcher(myString);
-
- if (m.find()) {
- newString = m.replaceAll("<br>");
- }
- //第二种方式
-
- newString = myString.replaceAll("(
|
|
|
)", "<br>");
测试
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- public class StringTest {
-
- public static String sss(String myString){
- String newString=null;
- Pattern CRLF = Pattern.compile("(
|
|
|
)");
- Matcher m = CRLF.matcher(myString);
- if (m.find()) {
- newString = m.replaceAll("<br>");
- }
- return newString;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- String aaa="sdfsfdsfsdf
,dsfsdfsdf
";
- //aaa.replace("s", "x");
- String newStr=sss(aaa);
- System.out.println(newStr);
- }
-
- }