字符串反转

String.prototype.reverse = function() {
var a = this.split('');
for (var i = 0, j = a.length-1; i < j; i++, j--) {
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join('');
} // test
var s = "Hello world!";
console.log(s.reverse());
// !dlrow olleH
// 去字符串#后面的部分  http://localhost:8090/admin/activity.html#list => list
String.prototype.hash = function() {
var index = this.lastIndexOf("#");
return index < 0 ? "" : this.substring(index);
}; // 字符串宽度 汉字算1个 英文字母数字算半个
String.prototype.width = function() {
var c = 0, inc;
for (var i = 0, n = this.length; i < n; i++) {
inc = this.charCodeAt(i) < 256 ? 0.5 : 1;
c += inc;
}
return c;
} String.prototype.mb_substr = function(w) {
var c = 0, inc, i;
for (i = 0, n = this.length; i < n && c <= w; i++) {
inc = this.charCodeAt(i) < 256 ? 0.5 : 1;
c += inc;
}
return this.substring(0, i);
} // 返回整数数组的最大值
Array.prototype.max = function() {
var max = this[0];
for (var i = 1, n = this.length; i < n; i++) {
if (this[i] > max) {
max = this[i];
}
}
return max;
}; // 去掉html中的标签 <div><p>...
function strip_tags(html) {
var div = document.createElement("div");
div.innerHTML = html;
return (div.textContent || div.innerText || "");
} // 去的当前年月 201806
function get_yyyymm() {
var date = new Date();
var month = date.getMonth()+1;
return (date.getYear() + 1900) + ""+ (month<10 ? "0"+month : month);
} // 引入css(jquery-ui.css), 到head >link:first-child前
// load css before all <link> tags
function css(href) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = href;
var dom = document.head.querySelector("link");
document.head.insertBefore(link, dom);
}

  

* 如果第二个参数 bUnicode255For1 === true,则所有字符长度为 1
否则如果字符 Unicode 编码 > 255 则长度为 2

input: 'hello world, 牛客', false

output: 17

function strLength(s, bUnicode255For1) {
var len = 0;
for (var i = 0, n = s.length; i < n; i++) {
if (s.charCodeAt(i) > 255) {
len += bUnicode255For1 ? 1:2;
} else {
len += 1;
}
}
return len;
}

  

最新文章

  1. java实现快速排序
  2. 谈BFC和haslayout
  3. 实现Div拖拽
  4. 使用WWW获取本地文件夹的XML配置文件
  5. Debian普通用户获取root权限|sudo的安装与配置
  6. grootJsAPI文档
  7. hosts代理
  8. 最简MacOs10.8安装
  9. Java-如何挖取某个网站中的ajax请求信息
  10. windows下安装python,安装框架django。
  11. (转)重磅出击:MongoDB 3.0正式版即将发布
  12. java初学的几个问题
  13. Unity3D基础学习 利用NGUI的Texture播放视频
  14. LDA 线性判别分析
  15. ubuntu 添加右键打开方式,无法添加程序打开方式
  16. Microsoft Edge浏览器下载文件乱码修复方法
  17. asp.netmvc部署到linux(centos)
  18. Feign性能优化注意事项
  19. `&amp;nbsp;` `&amp;emsp;` `&amp;ensp;` `&amp;thinsp;`
  20. latex表格代码

热门文章

  1. DVWA靶场之Command Injection(命令行注入)通关
  2. SQL 练习1
  3. 题解 Walker
  4. 题解 biology
  5. 【spring 注解驱动开发】扩展原理
  6. 未解决的html页面banner对不齐
  7. 初识cookie
  8. linux使用xampp安装MediaWiki环境
  9. Mysql 日期格式化 复杂日期区间查询
  10. Redis(三):新数据类型