JavaScript String Object

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

PropertyDescription
constructorReturns the function that created the String object's prototype
lengthReturns the length of a string
prototypeAllows you to add properties and methods to an object

String Object Methods

MethodDescription
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
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
toLowerCase()Converts a string to lowercase letters
toUpperCase()Converts a string to uppercase letters
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.

MethodDescription
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


concat() Method

 JavaScript String Object

Definition and Usage

The concat() method is used to join two or more strings.

This method does not change the existing strings, it only returns a copy of the joined strings.

Syntax

string.concat(string2, string3, ..., stringX)

ParameterDescription
string2, string3, ..., stringXRequired. The strings to be joined


Browser Support

The concat() method is supported in all major browsers.


Examples

Example 1

Join two strings:

<script type="text/javascript">

var str1="Hello ";
var str2="world!";
document.write(str1.concat(str2));

</script>

The output of the code above will be:

Hello world!

Try it yourself »

Example 2

Join three strings:

<script type="text/javascript">

var str1="Hello ";
var str2="world!";
var str3=" Have a nice day!";
document.write(str1.concat(str2,str3));

</script>

The output of the code above will be:

Hello world! Have a nice day!

Try it yourself »


JavaScript match() Method

 JavaScript String Object

Definition and Usage

The match() method searches for a match between a regular expression and a string, and returns the matches.

This method returns an array of matches, or null if no match is found.

Syntax

string.match(regexp)

ParameterDescription
regexpRequired. A regular expression. Read more about the RegExp object


Browser Support

The match() method is supported in all major browsers.


Example

Example

Perform a global, case-insensitive search for "ain":

<script type="text/javascript">

var str="The rain in SPAIN stays mainly in the plain"; 
var patt1=/ain/gi;
document.write(str.match(patt1));

</script>

The output of the code above will be:

ain,AIN,ain,ain

Try it yourself »



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值