js string object

本文详细介绍了JavaScript中的字符串对象,包括其创建方式、属性及各种方法的使用,如concat(), indexOf(), replace()等,并提供了HTML包装方法的概览。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from http://www.w3schools.com/jsref/jsref_obj_string.asp

String Object

The String object is used to manipulate a stored piece of text.

String objects are created with new String().

Syntax

var txt = new String(" string");

or more simply:

var txt = " string";

For a tutorial about the String object, read our JavaScript String Object tutorial.


String Object Properties

Property Description
constructor Returns the function that created the String object's prototype
length Returns the length of a string
prototype Allows you to add properties and methods to an object

String Object Methods

Method Description
charAt() Returns the character at the specified index
charCodeAt() Returns the Unicode of the character at the specified index
concat() Joins two or more strings, and returns a copy of the joined strings
fromCharCode() Converts Unicode values to characters
indexOf() Returns the position of the first found occurrence of a specified value in a string
lastIndexOf() Returns the position of the last found occurrence of a specified value in a string
localeCompare() Compares two strings in the current locale
match() Searches for a match between a regular expression and a string, and returns the matches
replace() Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring
search() Searches for a match between a regular expression and a string, and returns the position of the match
slice() Extracts a part of a string and returns a new string
split() Splits a string into an array of substrings
substr() Extracts the characters from a string, beginning at a specified start position, and through the specified number of character
substring() Extracts the characters from a string, between two specified indices
toLocaleLowerCase() Converts a string to lowercase letters, according to the host's locale
toLocaleUpperCase() Converts a string to uppercase letters, according to the host's locale
toLowerCase() Converts a string to lowercase letters
toString() Returns the value of a String object
toUpperCase() Converts a string to uppercase letters
trim() Removes whitespace from both ends of a string
valueOf() Returns the primitive value of a String object

String HTML Wrapper Methods

The HTML wrapper methods return the string wrapped inside the appropriate HTML tag.

These are not standard methods, and may not work as expected in all browsers.

Method Description
anchor() Creates an anchor
big() Displays a string using a big font
blink() Displays a blinking string
bold() Displays a string in bold
fixed() Displays a string using a fixed-pitch font
fontcolor() Displays a string using a specified color
fontsize() Displays a string using a specified size
italics() Displays a string in italic
link() Displays a string as a hyperlink
small() Displays a string using a small font
strike() Displays a string with a strikethrough
sub() Displays a string as subscript text
sup() Displays a string as superscript text
JavaScript中,可以使用JSON.parse()方法将JSON字符串转换为对象。该方法将一个符合JSON格式的字符串作为参数,并返回一个JavaScript对象。可以使用以下代码将js String转换为object: var jsonString = '{"testArray":[1,2,3,4,5],"name":"CSS3","date":"8 May, 2011"}'; var obj = JSON.parse(jsonString); 通过使用JSON.parse()方法,将JSON字符串解析为JavaScript对象。在这个例子中,我们将jsonString字符串解析为了一个名为obj的对象。可以根据需要访问该对象的属性和方法。 注意:在使用JSON.parse()方法转换JSON字符串为对象时,JSON字符串必须符合JSON格式的要求,包括使用双引号引起来的属性名和字符串值,以及正确的语法和格式。如果JSON字符串不符合JSON格式要求,将会抛出异常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JS 对象(Object)和字符串(String)互转方法](https://download.youkuaiyun.com/download/weixin_38523618/13136856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [JavaScript中的object转换成number或string规则介绍](https://download.youkuaiyun.com/download/weixin_38748207/13609093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [javascript中将Object转换为String函数代码 (json str)](https://download.youkuaiyun.com/download/weixin_38559203/13620306)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值