/* ============== String ==============*/ /* substr(), substring() - 문자열의 하위 문자열 반환 - 1번째 매개 변수 : 시작 위치 - 2번째 매개 변수 : substr() - 문자 개수 / substring() - 끝 위치(반환 문자열에 포함X) - 2번째 매개 변수 생략 가능 */ private function test1() : void { var str:String = "Hello from Paris, Texas!!!"; trace( "str.substr( 2, 5 ) :", str.substr( 2, 5 ) ); trace( "str.substring( 2, 5 ) :", str.substring( 2, 5 ) ); /* str.substr( 2, 5 ) : llo f str.substring( 2, 5 ) : llo */ trace( "str.substr( 2 ) :", str.substr( 2 ) ); trace( "str.substring( 2 ) :", str.substring( 2 ) ); /* str.substr( 2 ) : llo from Paris, Texas!!! str.substring( 2 ) : llo from Paris, Texas!!! */ } /* slice() - substring()와 유사함 - 매개 변수로 음의 정수 사용 가능 */ private function test2() : void { var str:String = "Hello from Paris, Texas!!!"; trace( "str.substring( 2, 5 ) :", str.substring( 2, 5 ) ); trace( "str.slice( 2, 5 ) :", str.slice( 2, 5 ) ); /* str.substring( 2, 5 ) : llo str.slice( 2, 5 ) : llo */ trace( "str.slice( -2, -5 ) :", str.slice( -2, -5 ) ); trace( "str.slice( -5, -2 ) :", str.slice( -5, -2 ) ); /* str.slice( -2, -5 ) : str.slice( -5, -2 ) : as! */ trace( "str.slice( -5, 23 ) :", str.slice( -5, 23 ) ); //str.slice( -5, 23 ) : as } /* indexOf(), lastIndexOf() - 일치하는 하위 문자열 검색 - 2번째 매개 변수 : 검색 시작 위치 */ private function test3() : void { var str:String = "The moon, the stars, the sea, the land"; trace( 'str.indexOf( "the" ) :', str.indexOf( "the" ) ); //str.indexOf( "the" ) : 10 trace( 'str.indexOf( "the", 11 ) :', str.indexOf( "the", 11 ) ); //str.indexOf( "the", 11 ) : 21 trace( 'str.lastIndexOf( "the" ) :', str.lastIndexOf( "the" ) ); //str.lastIndexOf( "the" ) : 30 trace( 'str.lastIndexOf( "the", 29 ) :', str.lastIndexOf( "the", 29 ) ); //str.lastIndexOf( "the", 29 ) : 21 } /* split() - 2번째 파라미터 지정 가능(옵션) - RegExp 가능 */ private function test4() : void { var str:String = "first=joe&last=cheng&title=manager&StartDate=3/6/65"; var arr:Array = str.split( "&" ); trace( "arr :", arr ); //arr : first=joe,last=cheng,title=manager,StartDate=3/6/65 arr = str.split( "&", 2 ); trace( "arr :", arr ); //arr : first=joe,last=cheng var str2:String = "Give me\t5." var arr2:Array = str2.split( "\t" ); trace( "arr2 :", arr2 ); } /* search() - 대소문자 구분 - RegExp 가능 (g플래그 불가) - 일치하는 한 항목만 찾고, 시작 인덱스 반환 */ private function test5() : void { var str:String = "The more the merrier. The more the merrier."; trace( 'str.search( "the" ) :', str.search( "the" ) ); //str.search( "the" ) : 9 trace( 'str.search( /the/ ) :', str.search( /the/ ) ); //str.search( "the" ) : 9 trace( 'str.search( /the/i ) :', str.search( /the/i ) ); //str.search( /the/i ) : 0 trace( 'str.search( /the/ig ) :', str.search( /the/ig ) ); //str.search( /the/ig ) : 0 } /* match() - 일치하는 하위 문자열 검색 - RegExp 가능 - g 전역 플래그 사용 가능 -> 일치하는 하위 문자열 배열 반환 */ private function test6() : void { var str:String = "The more the merrier. The more the merrier."; trace( 'str.match( "the" ) :', str.match( "the" ) ); //str.match( "the" ) : the trace( 'str.match( /the/ ) :', str.match( /the/ ) ); //str.match( /the/ ) : the trace( 'str.match( /the/i ) :', str.match( /the/i ) ); //str.match( /the/i ) : The trace( 'str.match( /the/ig ) :', str.match( /the/ig ) ); //str.match( /the/ig ) : The,the,The,the } /* replace() - 지정한 패턴 검색 - 일치한 부분은 지정한 대체 문자열로 바꿈 */ private function test7() : void { // var str:String = "She sells seashells by the seashore."; var str:String = "The more the merrier. The more the merrier."; trace( 'str.replace( "the", "THE" ) :', str.replace( "the", "THE" ) ); trace( "str :", str ); /* str.replace( "the", "THE" ) : The more THE merrier. The more the merrier. str : The more the merrier. The more the merrier. */ trace( 'str.replace( /the/, "THE" ) :', str.replace( /the/, "THE" ) ); trace( "str :", str ); /* str.replace( /the/, "THE" ) : The more THE merrier. The more the merrier. str : The more the merrier. The more the merrier. */ trace( 'str.replace( /the/i, "THE" ) :', str.replace( /the/i, "THE" ) ); trace( "str :", str ); /* str.replace( /the/i, "THE" ) : THE more the merrier. The more the merrier. str : The more the merrier. The more the merrier. */ trace( 'str.replace( /the/ig, "THE" ) :', str.replace( /the/ig, "THE" ) ); trace( "str :", str ); /* str.replace( /the/ig, "THE" ) : THE more THE merrier. THE more THE merrier. str : The more the merrier. The more the merrier. */ } /* ============== Array ==============*/ /* push(), pop() */ private function test1() : void { var arr:Array = [ 1, 2, 3 ]; trace( "arr :", arr ); //arr : 1,2,3 arr.push( "a" ); trace( "arr :", arr ); //arr : 1,2,3,a arr.push( 5, 6, 7 ); trace( "arr :", arr ); //arr : 1,2,3,a,5,6,7 trace( "arr.pop() :", arr.pop() ); //arr.pop() : 7 } /* shift(), unshift() */ private function test2() : void { var arr:Array = [ 1, 2, 3 ]; trace( "arr :", arr ); //arr : 1,2,3 trace( 'arr.unshift( "a" ) :', arr.unshift( "a" ) ); //arr.unshift( "a" ) : 4 trace( "arr :", arr ); //arr : a,1,2,3 trace( 'arr.unshift( 5, 6, 7 ) :', arr.unshift( 5, 6, 7 ) ); //arr.unshift( 5, 6, 7 ) : 7 trace( "arr :", arr ); //arr : 5,6,7,a,1,2,3 trace( "arr.shift() :", arr.shift() ); //arr.shift() : 5 } /* splice() AS3 function splice( startIndex:int, deleteCount:uint, ... values) : Array */ private function test3() : void { var arr:Array = [ 1, 2, 3 ]; trace( "arr :", arr ); //arr : 1,2,3 arr.splice( 1, 0, "a", "b", "c" ); trace( "arr :", arr ); //arr : 1,a,b,c,2,3 var arr2:Array = arr.splice( 2, 3 ); trace( "arr :", arr ); //arr : 1,a,3 trace( "arr2 :", arr2 ); //arr2 : b,c,2 } /* reverse() */ private function test4() : void { var arr:Array = [ 1, 2, 3 ]; trace( "arr :", arr ); //arr : 1,2,3 arr.reverse(); trace( "arr :", arr ); //arr : 3,2,1 var arr2:Array = [ 27, 3, 100, 1, 38 ]; trace( "arr2 :", arr2 ); //arr2 : 27,3,100,1,38 arr2.sort( Array.NUMERIC ); trace( "arr2 :", arr2 ); //arr2 : 1,3,27,38,100 arr2.sort( Array.NUMERIC ); arr2.reverse(); trace( "arr2 :", arr2 ); //arr2 : 100,38,27,3,1 arr2.sort( Array.NUMERIC|Array.DESCENDING ); trace( "arr2 :", arr2 ); //arr2 : 100,38,27,3,1 } /* sort(), sortOn() */ private function test5() : void { var arr:Array = [ 27, 3, 100, 1, 38 ]; trace( "arr :", arr ); //arr : 27,3,100,1,38 arr.sort(); trace( "arr :", arr ); //arr : 1,100,27,3,38 arr.sort( Array.NUMERIC ); trace( "arr :", arr ); //arr : 1,3,27,38,100 } /* join(), toString() */ private function test6() : void { var arr:Array = [ 27, 3, 100, 1, 38 ]; trace( "arr.toString() :", arr.toString() ); //arr.toString() : 27,3,100,1,38 trace( "arr.join() :", arr.join() ); //arr.join() : 27,3,100,1,38 trace( 'arr.join( "-" ) :', arr.join( "-" ) ); //arr.join( "-" ) : 27-3-100-1-38 var str:String = "2011-08-25"; var arr2:Array = str.split( "-" ); var _date:Date = new Date( Number( arr2[0] ), Number( arr2[1] ) -1, Number( arr2[2] ) ); trace( "_date :", _date ); //_date : Thu Aug 25 00:00:00 GMT+0900 2011 } /* slice() */ private function test7() : void { var arr:Array = [ 1, 2, 3, 4, 5 ]; trace( "arr.slice() :", arr.slice() ); //arr.slice() : 1,2,3,4,5 trace( "arr :", arr ); //arr : 1,2,3,4,5 trace( "arr.slice( 1, 4 ) :", arr.slice( 1, 4 ) ); //arr.slice( 1, 4 ) : 2,3,4 trace( "arr.slice( 1 ) :", arr.slice( 1 ) ); //arr.slice( 1 ) : 2,3,4,5 }
프로그래밍/ActionScript