일반 문자열에서 특수 문자 (Escape Sequence) 처리

황제낙엽 2009.02.20 09:44 조회 수 : 1322 추천:199

sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  

자바프로그램 내에서 문자열을 처리하다보면 escape sequence문자가 사라지는 경우가 있다.

보통 정규식을 사용하는 문자열 처리 함수(replaceAll)에서 이러한 현상이 나타나는데

이렇게 사라지는 현상을 방지하거나 출력 문자열에 원하는 형태로 출력하기 위해서 변형시킬 필요가 있다.

역으로 문자열 형태의 escape sequence를 원형으로 변환하기 위해서는 JDK5부터 지원하는 Formatter 클래스를

이용해야 할 듯 하다. 자세한 것은 링크를 참조한다. -> System.out.printf()

첨부파일은 관련 유틸 클래스이다.

·미리보기 | 소스복사·
  1. /**   
  2.  * source 내의 모든 escape sequence를 문자열로 변환한다.   
  3.  * @param source   
  4.  * @return  
  5.  */   
  6. public static String replaceEscapeSequence(String source) {   
  7.     if (source == null) {   
  8.         return source;   
  9.     }   
  10.   
  11.     StringBuffer newSource = new StringBuffer();   
  12.     char[] sourceChar = source.toCharArray();   
  13.     for (int i = 0; i < sourceChar.length; i++) {   
  14.         if (sourceChar[i] == 'r') { //Carridge Return   
  15.             newSource.append('');   
  16.             newSource.append('r');   
  17.             continue;   
  18.         } else if (sourceChar[i] == 'n') { //New Line   
  19.             newSource.append('');   
  20.             newSource.append('n');   
  21.             continue;   
  22.         } else if (sourceChar[i] == 't') { // Tab   
  23.             newSource.append('');   
  24.             newSource.append('t');   
  25.             continue;   
  26.         } else if (sourceChar[i] == 'b') { //Back space   
  27.             newSource.append('');   
  28.             newSource.append('b');   
  29.             continue;   
  30.         } else if (sourceChar[i] == 'f') { //Form Feed   
  31.             newSource.append('');   
  32.             newSource.append('f');   
  33.             continue;   
  34.         } else if (sourceChar[i] == ''') { //Single Quotation   
  35.             newSource.append('');   
  36.             newSource.append(''');   
  37.             continue;   
  38.         } else if (sourceChar[i] == '"') { //Double Quotation   
  39.             newSource.append('');   
  40.             newSource.append('"');   
  41.             continue;   
  42.         } else if (sourceChar[i] == '') { //Backslash   
  43.             newSource.append('');   
  44.             newSource.append('');   
  45.             continue;   
  46.         }   
  47.         newSource.append(sourceChar[i]);   
  48.     }   
  49.   
  50.     return newSource.toString();   
  51. }  
번호 제목 글쓴이 날짜 조회 수
171 메모리 유출과 약한 참조 황제낙엽 2010.01.26 616
170 Methods of the Matcher Class 황제낙엽 2010.01.19 120
169 Pattern.matches() , Matcher.matches() , Matcher.find() file 황제낙엽 2010.01.19 105
168 java.lang.IllegalArgumentException 황제낙엽 2010.01.18 130509
167 org.apache.commons.fileupload.servlet.ServletFileUpload 를 이용한 파일 업로드 file 황제낙엽 2009.11.19 129
166 Error reading tld listeners java.lang.NullPointerException 황제낙엽 2009.10.14 67
165 Cannot find the tag library descriptor for “http://java.sun.com/jsp/jstl/core 황제낙엽 2009.10.14 1006
164 Transfer-Encoding: chunked VS Content-Length 황제낙엽 2009.09.17 154
163 서블릿 응답 헤더(Response Header) 황제낙엽 2009.09.17 80
162 같은 문자열인데도 정규식에서 해당 문자열을 파싱하지 못하는 경우 황제낙엽 2009.08.08 39
161 MultipartRequest (cos.jar)와 서블릿을 이용한 업로드 file 황제낙엽 2009.06.19 384
160 [대용량 파일 업로드] multipart form parser - http file upload, database 저장 java class 연재2 file 황제낙엽 2009.06.19 1831
159 [대용량 파일 업로드] multipart form parser - http file upload 기능 java class 연재1 file 황제낙엽 2009.06.19 1436
158 [reflection/리플렉션] Class.forName 황제낙엽 2009.05.27 101
157 문자열 내의 공백을 제거하는 간단한 정규식 황제낙엽 2009.05.20 88
» 문자열에서 특수 문자 (Escape Sequence) 처리 file 황제낙엽 2009.02.20 1322
155 정규표현식을 사용하는 String클래스의 replaceAll() 함수 개량 황제낙엽 2009.02.09 219
154 File 복사 함수 황제낙엽 2009.02.08 31
153 JSP session 정보 얻기 황제낙엽 2009.01.21 127
152 서버상의 로컬경로 (실제경로) 관련 환경변수 황제낙엽 2009.01.21 339