java 回車替換換行



   
   
  
  
          
  1. //第一種方式
  2. import java.util.regex.Pattern;
  3. import java.util.regex.Matcher;
  4. ...
  5. // 4 different combinaisons
  6. Pattern CRLF = Pattern.compile( "(\r\n|\r|\n|\n\r)");
  7. Matcher m = CRLF.matcher(myString);
  8. if (m.find()) {
  9. newString = m.replaceAll( "<br>");
  10. }
  11. //第二種方式
  12. newString = myString.replaceAll( "(\r\n|\r|\n|\n\r)", "<br>");

測試


   
   
  
  
          
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. public class StringTest {
  4. public static String sss(String myString){
  5. String newString= null;
  6. Pattern CRLF = Pattern.compile( "(\r\n|\r|\n|\n\r)");
  7. Matcher m = CRLF.matcher(myString);
  8. if (m.find()) {
  9. newString = m.replaceAll( "<br>");
  10. }
  11. return newString;
  12. }
  13. /**
  14. * @param args
  15. */
  16. public static void main(String[] args) {
  17. String aaa= "sdfsfdsfsdf\r\n,dsfsdfsdf\r\n";
  18. //aaa.replace("s", "x");
  19. String newStr=sss(aaa);
  20. System.out.println(newStr);
  21. }
  22. }



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM