分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

js字符串操作

发布时间:2023-09-06 01:25责任编辑:胡小海关键词:js

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>字符串练习</title>
</head>
<body>
<script>
//字符串的长度
var hello = ‘helloword‘;
console.log(hello.length)

//indexof定位字符串中某一个指定的字符首次出现的位置
var hellos = ‘helloword‘;
console.log(hellos.indexOf(‘l‘));

//match() 来查找字符串中特定的字符,并且如果找到的话,则返回这个字符 没找到 返回null
var s = ‘helloword‘;
alert(s.match(‘b‘))

//替换字符串 (不会改变原来的字符串)
var ss = ‘helloword‘
ss.replace(/hello/,‘改变‘);
console.log(ss.replace(/hello/,‘改变‘));

//把字符串分割成数组 (split()里面不占数组内容)
var string1 = ‘这是字符串1‘;
string1.split(‘是‘);
console.log(string1.split(‘是‘));

//把字符串显示为下标
var string2 = ‘这是字符串2‘;
console.log(string2.sub());
document.write(‘下标:‘+string2.sub())

//把字符串显示为下标
var string3 = ‘这是字符串2‘;
console.log(string3.sub());
document.write(‘上标:‘+string3.sup());


//字符串大写
var string4= ‘DaXie‘;
console.log(string4.toLocaleUpperCase());

//字符串小写
var string5 = ‘XiaoXie‘
console.log(string5.toLocaleLowerCase());

//从字符串中指定下标提取字符
var string6 = ‘这是字符串6‘;
console.log(string6.substr(3))

//字符串颜色、
var string7 = ‘改变我的颜色‘;
document.write(string7.fontcolor(‘red‘));

//连接2个字符
console.log(string7.concat(string6));

//返回在指定位置的字符
var string8 = ‘指定位置的字符‘;
console.log(string8.charAt(2));

//提取字符串
var string9 ?= ‘提取字符串‘
console.log(string9.slice(0,2));



</script>
</body>
</html>

js字符串操作

原文地址:http://www.cnblogs.com/yangjiansong/p/7865324.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved