博客
关于我
JavaScript 字符串(String)对象
阅读量:214 次
发布时间:2019-02-28

本文共 841 字,大约阅读时间需要 2 分钟。

JavaScript String(字符串)对象 实例

字符串对象用于处理已有的字符块。如何使用长度属性来计算字符串的长度。

字符串对象的length属性可以用来获取字符串的长度。例如:

var txt="Hello world!"

通过执行上述代码,可以输出字符串的长度。

如何为字符串添加样式。

在JavaScript中,可以使用字符串的replace方法来为字符添加样式。例如:

var txt="Hello world!"

通过执行上述代码,可以将所有逗号替换为空格。

如何使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置。

indexOf方法可以用来查找字符串中某个字符的位置。例如:

var txt="Hello world!", searchChar="w"

通过上述代码,可以找到"world!"中的"w"的位置。

如何使用 match() 来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。

match方法可以用来查找字符串中的特定字符。例如:

var txt="Hello world!", regex=/w/

通过上述代码,可以找到字符串中的所有字母字符。

如何使用 replace() 方法在字符串中用某些字符替换另一些字符。

replace方法可以用来替换字符串中的字符。例如:

var txt="Hello, world!", replaceChar=","

通过上述代码,可以将所有逗号替换为空格。

完整的 String 对象参考手册

本手册详细介绍了所有与字符串对象相关的属性和方法。

字符串对象

字符串对象用于处理已有的字符块。

例子

下面的例子使用字符串对象的length属性来计算字符串的长度:

var txt="Hello world!"

输出为:12

下面的例子使用字符串对象的toUpperCase()方法将字符串转换为大写:

var txt="Hello world!"

输出为:HELLO WORLD!

转载地址:http://lxes.baihongyu.com/

你可能感兴趣的文章
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>