2015-01-08 5 views
0

문자열의 태그를 java의 다른 문자열의 내용으로 바꾸려고합니다.java 줄 바꿈 문자열을 모두 바꾸기

searchString은 텍스트 파일에서 읽은 헤더입니다.

String replaceString = Integer.toString(bodyString.length()); 
System.out.println("****************************************"); 
System.out.println("Body String: " + bodyString); 
System.out.println("\nBody Length: " + replaceString); 
System.out.println("\nSearch String: " + searchString); 
if (searchString.contains("ContentLength")){ 
    System.out.println("\nfound ContentLength in string"); 
    searchString.replaceAll("ContentLength", replaceString); 
} 
System.out.println("\nSearch String: " + searchString); 
System.out.println("****************************************"); 

그것은 출력 :

**************************************** 
Body String: 0425742196,1234,13:36:47, 

Body Length: 26 

Search String: HTTP/1.1 200 OK 
Date: Mon, 02 Dec 2013 08:57:36 GMT 
Server: IBM_HTTP_Server 
Cache-Control: no-cache 
Content-Length: ContentLength 
Set-Cookie: JSESSIONID=0000hzYFq3Sa-rl7ywJMrGTF0he:186g6992j; Path=/ 
Set-Cookie: PLAY_ERRORS=""; Path=/locationsapi/ 
Expires: Thu, 01 Dec 1994 16:00:00 GMT 
Connection: close 
Content-Type: application/text; charset=utf-8 
Content-Language: en-AU 

found content in string 

Search String: HTTP/1.1 200 OK 
Date: Mon, 02 Dec 2013 08:57:36 GMT 
Server: IBM_HTTP_Server 
Cache-Control: no-cache 
Content-Length: ContentLength 
Set-Cookie: JSESSIONID=0000hzYFq3Sa-rl7ywJMrGTF0he:186g6992j; Path=/ 
Set-Cookie: PLAY_ERRORS=""; Path=/locationsapi/ 
Expires: Thu, 01 Dec 1994 16:00:00 GMT 
Connection: close 
Content-Type: application/text; charset=utf-8 
Content-Language: en-AU 
**************************************** 

내가 여러 줄에 문제가있어 의심 하고 ReplaceString 다른 가변 길이 문자열의 길이 (bodyString)

코드는입니다 하지만 그것을 극복하는 것 같습니다.

감사 어떤 조언을 ...

+0

'문자열'이 불변이므로 작동하지 않습니다. 나는 이것이 복제물이어야한다고 느낀다. 그러나 그것의 복제물이라는 질문을 찾을 수 없다. – Vitruvius

+0

[String.replace()가 문자를 대체하지 않음] 가능한 복제본 (http://stackoverflow.com/questions/12734721/string-replace-not-replacing-characters) – smac89

+0

[Hints for java.lang.String. 문제를 대체 하시겠습니까?] (http://stackoverflow.com/questions/1166905/hints-for-java-lang-string-replace-problem) –

답변

0

당신은 자바에서

searchString = searchString.replaceAll("ContentLength", replaceString) 

를 원 String불변이다. 새로운 String을 원한다면 생성자 호출 또는 많은 팩토리 메소드 중 하나에 의해 새로운 String을 가져와야합니다.