zoukankan      html  css  js  c++  java
  • js常用字符串函数

     1 // JS字符串
     2 //1.replace字符串替换,只能换第一部分,就是说多个字符相同,只能换下最先的
     3 var str='helloworld!';
     4 alert(str.replace('llo','9'));//he9world!
     5 
     6 //2.split字符串分隔符,把字符分成数组
     7 alert(str.split('llo'));//he,world! 数组
     8 
     9 //3.substr字符截取(起始位置0);
    10 var str='¥11.60';
    11 alert(str.substr(1));//11.60 从第一个位置开始截取到最后
    12 alert(str.substr(-4,1));//1 从右边数第四个位置开始向右截取1个字符
    13 //从第2个位置开始截取到最后。包括第二位置的字符
    14 alert(str.substring(2));//1.60
    15 //从第2个位置开始截取到第四位置截止(不包含第四位置)
    16 alert(str.substring(2,4));//1.
    17 
    18 //4.通过字符获取编码
    19 alert("a".charCodeAt(0));//97
    20 //5.通过编码获取字符
    21 String.fromCharCode(20013);//
    22 
    23 //6.获取字符串指定字符的位置
    24 var str="http://www.234.com/admin/index.html";
    25 str.indexOf("/");//5 第一个
    26 str.lastIndexOf("/");//30 最后一个
    程序世界#网络时代
  • 相关阅读:
    二、js基本语法
    一、js概览
    浅析URL
    css动画总结
    HTML常用标签
    从头学习HTML1
    了解HTTP协议
    常用ES6语法
    集合框架
    让图片铺满整个页面,自适应拉伸;限制文本行数,多余的显示...(省略号)
  • 原文地址:https://www.cnblogs.com/tmrgd/p/5778900.html
Copyright © 2011-2022 走看看