2014-01-29 5 views
0

문제 그럼이 코드는 예상대로 작동합니다자바 .replaceAll와 줄 바꿈

String test = "ONE: this is a\ntest string."; 
System.out.println(test); 
test = test.replaceAll("\n", " "); 
System.out.println(test); 

출력 :

ONE: this is a 
test string. 
ONE: this is a test string. 

내가 볼 때 내가있는 javamail와 이메일과 어떤 이유로 읽고있다 그 안에 줄 바꿈이있는 메시지. 그러나 어떤 이유로 replaceAll ("\ n", "")이 나를 위해 작동하지 않습니다. 그것은 인코딩 문제 또는 뭔가 있는지 궁금해. 개행을하는 또 다른 방법이 있습니까? Gmail에서 이메일을 보면 줄 바꿈이없는 것 같지만 출력하거나 메시지를 내보낼 때 약 70자가 넘는 줄이 단어 경계로 나뉘어집니다. 어떤 아이디어?

Properties props = System.getProperties(); 
props.setProperty("mail.store.protocol", "imaps"); 
Session session = Session.getDefaultInstance(props, null); 
Store store = session.getStore("imaps"); 
store.connect("imap.gmail.com", "[email protected]", "password"); 

Folder inbox = store.getFolder("Inbox"); 
inbox.open(Folder.READ_WRITE); 
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); 
Message messages[] = inbox.search(ft); 
for(Message msg:messages) { 
    String message = mailman.getContent(msg); 
    System.out.println("ONE:"+ message); 
    message = message.replaceAll("\n",""); 
    System.out.println("TWO: "+message); 
} 

출력 :

ONE: We just wanted to remind you about the start of our Underclassmen Academy. 
This program is intended for all freshmen and sophomores who are in the 
initial stages of your career preparation. Juniors are also invited to attend 
if you'd like to gain more exposure and/or are still recruiting. 

TWO: We just wanted to remind you about the start of our Underclassmen Academy. 
This program is intended for all freshmen and sophomores who are in the 
initial stages of your career preparation. Juniors are also invited to attend 
if you'd like to gain more exposure and/or are still recruiting. 
+3

아마도'\ n'으로 줄만 구분되지 않을 수도 있습니다. 어쩌면 그것은'\ r \ n'입니까? 'replaceAll ("\ r? \ n", "")'시도해 주시겠습니까? – Pshemo

+0

당신은 훌륭합니다. 답으로 게시물을 작성하면 내가 받아 들일 것입니다. –

+1

'replaceAll ("\\ n? \\ r", "");'과 같이 두 개의 슬래시가 실제로 필요하다고 확신했지만 Pshemo의 솔루션처럼 작동했습니다. 작동하지 않는다면 2 개의 슬래시를 사용해보십시오. – Rainbolt

답변

3

선이 \n으로하지만 \r\n로 구분되지 않는 것 같다. 또한 \r\n\n\r 받아

replaceAll("\r\n"," ") 

또는

replaceAll("\r?\n|\r"," ") 

을 사용하는 것이 좋습니다.