学习笔记
点击(此处)折叠或打开
- /**
- *2015-09-19
- *String 功能扩展方法
- **/
- /**
- *打印字符串
- **/
- function print(val){
- document.write(val);
- }
- /**
- *打印字符串,并在结尾添加换行符。
- **/
- function println(val){
- document.write(val + "<br/>");
- }
- /**
- *去除字符串两端的空格
- **/
- String.prototype.trim = function(){
- return this.replace(/^\s*|\s*$/g, '');
- }
- /**
- *字符串转数组
- **/
- String.prototype.toCharArr = function(){
- var cha = [];
- for(var i = 0; i < this.length; i++){
- cha[i] = this[i];
- }
- return cha;
- }
- /**
- *反转字符串
- **/
- String.prototype.reversalStr = function(){
- var arr = this.toCharArr();
- function swap(arr, a, b){
- var temp = arr[a];
- arr[a] = arr[b];
- arr[b] = temp;
- }
- for(var start = 0, end = this.length; start < end; start++, end--){
- swap(arr, start, end);
- }
- return arr.join("");
- }