퇴근5분전

"문자"를 "_"로 변경할때 그냥 문자열에 Replace를 하면 조건에 걸리는것 모두를 변경..

훈스 게시판에 처음 조건에 걸리는것만 바꾸는 메서드가 자바에서 지원이 된다기에... 

만들어봤다. 

// 사용예!
   string ss = "바뀔문자,바뀐문자,바꿔봐요!".ReplaceFirst("문자", "_");

결과 : 바뀔_,바뀐문자,바꿔봐요!".
  

// 확장 메서드 구현

  public static class StringEX
    {
        public static string ReplaceFirst(this string s, string re, string ch)
        {
            int offsetIndex = s.IndexOf(re) + re.Length;
            string first = s.Substring(0, s.IndexOf(re));
            string midle = ch;
            string last = s.Substring(s.IndexOf(re) + re.Length, s.Length - offsetIndex);
            return string.Concat(first, midle, last);
        }
    }


'# 1) 프로그래밍' 카테고리의 다른 글

ActiveDirectory] Query  (0) 2011.04.19
HWP 바이너리 파일...  (0) 2011.03.23
일자가 해당 월에 몇주차인지 구하는 법.  (0) 2010.09.30
DateTime관련..  (0) 2010.09.09
피벗 메서드  (0) 2010.08.26