2015년 12월 23일 수요일

스크립트 정리(1)

1. 값을 넘겼을때 null 또는 undefined면 공백값 리턴
function aaaa(obj){
   if(obj == null || (typeof obj == "undefined")){
       return "";
   }else{
       return obj;
   }
}

2. 문자사이에 공백이 있을때 제거
function aaaa(obj){
    if(obj == null || (typeof obj == "undefined")){
       return "";
   }else{
       return obj.replace(/\s/g, "");
   }
}
///////////////////////////////////////////
3. 년월일(YYYY.MM.DD) 값일경우 . . 부분 제거
function aaaa(obj){
    if(obj == null || (typeof obj == "undefined")){
       return "";
   }else{
       return obj.replace("..", "");
   }
}
///////////////////////////////////////////////
4. replacAll
//obj : 전체 문자열, currchar:바꿀 대상 문자열, repchar:변경할 문자열
function aaaa(obj, currchar, repchar){
    if(obj == null || (typeof obj == "undefined")){
       return "";
   }else{
       return obj.split(currchar).join(repchar);
   }
}
/////////////////////////////////////////////////////
5. StringBuffer를 사용할 수 있도록 함
var StringBuffer = function(){
    this.buffer = new Array();
};
StringBuffer.prototype.append = function(str){
     this.buffer[this.buffer.length] = str;
};
StringBuffer.prototype.toString = function(){
     return this.buffer.join("");
}
///////////////////////////////////////////////////////

댓글 없음:

댓글 쓰기