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

황제낙엽 2009.02.20 09:44 조회 수 : 1643 추천: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. }  
번호 제목 글쓴이 날짜 조회 수
163 서블릿 응답 헤더(Response Header) 황제낙엽 2009.09.17 479
162 같은 문자열인데도 정규식에서 해당 문자열을 파싱하지 못하는 경우 황제낙엽 2009.08.08 392
161 MultipartRequest (cos.jar)와 서블릿을 이용한 업로드 file 황제낙엽 2009.06.19 662
160 [대용량 파일 업로드] multipart form parser - http file upload, database 저장 java class 연재2 file 황제낙엽 2009.06.19 2279
159 [대용량 파일 업로드] multipart form parser - http file upload 기능 java class 연재1 file 황제낙엽 2009.06.19 1684
158 [reflection/리플렉션] Class.forName 황제낙엽 2009.05.27 482
157 문자열 내의 공백을 제거하는 간단한 정규식 황제낙엽 2009.05.20 407
» 문자열에서 특수 문자 (Escape Sequence) 처리 file 황제낙엽 2009.02.20 1643
155 정규표현식을 사용하는 String클래스의 replaceAll() 함수 개량 황제낙엽 2009.02.09 526
154 File 복사 함수 황제낙엽 2009.02.08 424
153 JSP session 정보 얻기 황제낙엽 2009.01.21 450
152 서버상의 로컬경로 (실제경로) 관련 환경변수 황제낙엽 2009.01.21 641
151 java.net.URL 생성시 로컬 파일에 접근 황제낙엽 2009.01.20 382
150 자바로 구현하는 Web-to-web 프로그래밍 황제낙엽 2009.01.20 436
149 Using Java's Net::URL Class To Access URLs (java.net.URL) 황제낙엽 2009.01.20 627
148 입력받은 문자열의 의미가 숫자인지 단순 텍스트인지 판별해야 할 때 황제낙엽 2009.01.09 383
147 사용팁 황제낙엽 2008.07.24 306
146 문자열 처리 - StringTokenizer 와 String.split() 황제낙엽 2008.07.08 474
145 숫자의 형식화 #1(Part-1)-java.text.NumberFormat 황제낙엽 2008.07.08 306
144 숫자 에 대응 되는 문자의 형식화 #2 황제낙엽 2008.07.08 363