Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js string方法

作者:惨美歌   发布日期:2026-06-23   浏览:128

// 示例代码:使用 JavaScript 中的 String 方法

// 1. toUpperCase(): 将字符串转换为大写
let str1 = "hello world";
let result1 = str1.toUpperCase();
console.log(result1); // 输出: HELLO WORLD

// 2. toLowerCase(): 将字符串转换为小写
let str2 = "HELLO WORLD";
let result2 = str2.toLowerCase();
console.log(result2); // 输出: hello world

// 3. substring(start, end): 提取字符串中的一部分
let str3 = "hello world";
let result3 = str3.substring(0, 5);
console.log(result3); // 输出: hello

// 4. replace(searchValue, newValue): 替换字符串中的指定内容
let str4 = "hello world";
let result4 = str4.replace("world", "JavaScript");
console.log(result4); // 输出: hello JavaScript

// 5. split(separator): 将字符串分割成数组
let str5 = "apple,banana,orange";
let result5 = str5.split(",");
console.log(result5); // 输出: ["apple", "banana", "orange"]

// 6. trim(): 去除字符串两端的空白字符
let str6 = "   hello world   ";
let result6 = str6.trim();
console.log(result6); // 输出: hello world

// 7. concat(str1, str2, ..., strX): 连接两个或多个字符串
let str7 = "hello";
let str8 = "world";
let result7 = str7.concat(" ", str8);
console.log(result7); // 输出: hello world

// 8. charAt(index): 返回指定索引处的字符
let str9 = "hello";
let result8 = str9.charAt(0);
console.log(result8); // 输出: h

// 9. includes(searchValue, position): 检查字符串是否包含指定的子字符串
let str10 = "hello world";
let result9 = str10.includes("world");
console.log(result9); // 输出: true

// 10. startsWith(searchValue, position): 检查字符串是否以指定的子字符串开头
let str11 = "hello world";
let result10 = str11.startsWith("hello");
console.log(result10); // 输出: true

// 11. endsWith(searchValue, length): 检查字符串是否以指定的子字符串结尾
let str12 = "hello world";
let result11 = str12.endsWith("world");
console.log(result11); // 输出: true

解释说明:

  • toUpperCase()toLowerCase() 分别用于将字符串转换为全大写或全小写。
  • substring() 用于提取字符串中的一部分,replace() 用于替换字符串中的指定内容。
  • split() 可以将字符串按照指定分隔符分割成数组。
  • trim() 用于去除字符串两端的空白字符。
  • concat() 用于连接两个或多个字符串。
  • charAt() 返回指定索引处的字符。
  • includes()startsWith()endsWith() 分别用于检查字符串是否包含、以指定子字符串开头或结尾。

上一篇:js guid

下一篇:js string转对象

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js fill方法

js 数组连接

js json数组

js 数组复制

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站