美文网首页JavaScript技术
JS-字符串的方法

JS-字符串的方法

作者: coderhzc | 来源:发表于2022-04-08 09:08 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <textarea name="" id="" cols="30" rows="10"></textarea>
  <script>
    // 字符串的系列方法

    // length 属性返回字符串的长度:
    var str = "ABCDEFGHIJKLMNOPQRETUVWXYZ";
    console.log(str.length); // 26

    var str1 = "我要\'转义\'";
    console.log(str1);

    // 2.方法返回字符串中指定文本首次出现的索引(位置)
    var str2 = "ABCDEFG";
    var newStr2 = str2.indexOf("C");
    console.log(newStr2); // 2      字符串中指定文本首次出现的索引 如果改值不存在的话 返回-1
    console.log(str2[0]); // A  取值是和数组时一样的 


    // 3.lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引:
    var str3 = "The full name of China is the People's Republic of China.";
    var pos = str3.lastIndexOf("China");
    console.log(pos); // 51  字符串中指定文本首次出现的索引 如果改值不存在的话 返回-1


    /********如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。******************/
    var str = "The full name of China is the People's Republic of China.";
    var pos = str.search("name");
    console.log(pos); // 9

    var strMethod = "The full name of China is the People's Repulic of China";
    var newStr = strMethod.slice(0, 2)
    console.log(newStr); // Th

    // replace() 字符串的替换 replace() 只替换首个匹配
    var strMethod = "The full name of China is the People's Repulic of China";
    var n = strMethod.replace("China", "胡振楚");
    console.log(n); // The full name of 胡振楚 is the People's Repulic of China

    // 如需替换所有匹配,请使用正则表达式的 g 标志(用于全局搜索):
    var strMethod = "The full name of China is the People's Repulic of China";
    var l = strMethod.replace(/China/g, "楚楚胡") // 这样的话就可以实现全局匹配了
    console.log(l); // The full name of 楚楚胡 is the People's Repulic of 楚楚胡

    // toUpperCase() 把字符串转换为大写
    var text1 = "hello,world";
    var newText = text1.toUpperCase();
    console.log(newText); // HELLO,WORLD

    // 通过 toLowerCase() 把字符串转换为小写:
    var text2 = "Hello,WORld";
    var newText2 = text2.toLowerCase();
    console.log(newText2); // hello,world

    // concat() 连接两个或多个字符串:
    var test1 = "胡振楚的家庭住址:";
    var test2 = "湖北省武汉市";
    var test3 = test1.concat(test2, "1111111");  // 字符串的很多方法和数组的类似 数组也是有这个concat方法的
    console.log(test3);  // 胡振楚的家庭住址:湖北省武汉市

    // trim() 方法删除字符串两端的空白符:
    var StrTrim = " 中国       ";
    console.log(StrTrim.trim());

    // 如需支持 IE 8,您可搭配正则表达式使用 replace() 方法代替:
    var str = "       Hello World!        ";
    console.log(str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''));


    // 这是两个提取字符串字符的安全方法:
    // charAt() 方法 charAt() 方法返回字符串中指定下标(位置)的字符串:
    var u = "hello,world";
    var ss = u.charAt(0)
    console.log(ss); // h 

    // str[0]="A"这种修改时没有意义的,他是不会修改的
    var str = "HELLO WORLD";
    str[0] = "A";             // 不产生错误,但不会工作
    str[0];                   // 返回 H

    // 把字符串转换为数组 可以通过 split() 将字符串转换为数组:
    var strArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var newStrArr = strArr.split("");
    console.log(newStrArr); // ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

    // match() 方法 match() 方法根据正则表达式在字符串中搜索匹配项,并将匹配项作为 Array 对象返回。若未找到匹配项,则为 null。
    // 如果正则表达式不包含 g 修饰符(执行全局搜索),match() 方法将只返回字符串中的第一个匹配项,返回的类型是数组
    var yy = "中国是一个多元国家,中国食物很好吃,中国的牛肉也好吃"
    var YY = yy.match(/中国/g);
    console.log(YY); //  ["中国", "中国", "中国"];

    // 对 "ain" 执行不区分大小写的全局搜索:
    var China = "The rain in SPAIN stays mainly in the plain";
    var newChina = China.match(/ain/gi)
    console.log(newChina); //// 返回数组[ain,AIN,ain,ain]


    // String.includes() 如果字符串包含指定值,includes() 方法返回 true。
    var hello = "Hello world, welcome to the universe.";
    var newIncludes = hello.includes("world");
    console.log(newIncludes); // true
    if (newIncludes) {
      // alert('确实包含了')
    }

    // string.includes(searchvalue, start)   start  可选。默认为 0. 开始搜索的位置。
    var text = "Hello world, welcome to the universe.";
    var isText = text.includes('to', 21); // true

    // startsWith()方法区分大小写
    // String.startsWith() 如果字符串以指定值开头,则 startsWith() 方法返回 true,否则返回 false:
    var start = "如果字符串以指定值开头";
    var isStart = start.startsWith("如");
    console.log(isStart); // true

    //String.endsWith() 如果字符串以指定值结尾,则 endsWith() 方法返回 true,否则返回 false:
    var text = "John Doe";
    text.endsWith("Doe")    // 返回 true

    // string.endswith(searchvalue, length) 可选。要搜索的长度。
    var text = "Hello world, welcome to the universe.";   
    text.endsWith("world", 11) // 返回 true
  </script>
</body>

</html>

相关文章

网友评论

    本文标题:JS-字符串的方法

    本文链接:https://www.haomeiwen.com/subject/ainesrtx.html