0% found this document useful (0 votes)
9 views2 pages

JavaScript String Methods

Uploaded by

rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

JavaScript String Methods

Uploaded by

rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

JavaScript String Properties & Methods

Properties
Feature Description Example

length Returns number of characters in a string. "Hello".length // 5

prototype Allows adding new properties/methods to all stringString.prototype.sayHi


objects. = function(){ return "Hi " + this; }; "R

Common String Methods


Method Description Example

charAt(index) Returns the character at a given index. "Hello".charAt(1) // "e"

charCodeAt(index) Returns Unicode of the character at index. "A".charCodeAt(0) // 65

concat() Joins two or more strings. "Hello".concat(" ", "World") // "Hello World"

indexOf(value) Returns index of first occurrence. "hello world".indexOf("o") // 4

lastIndexOf(value) Returns index of last occurrence. "hello world".lastIndexOf("o") // 7

localeCompare(str) Compares two strings by locale. "a".localeCompare("b") // -1

match(regex) Matches string with regex. "abc123".match(/\d+/) // ["123"]

replace(search, value) Replaces part of a string. "hello".replace("l","x") // "hexlo"

search(regex) Searches and returns index. "abc123".search(/\d/) // 3

slice(start,end) Extracts part of a string. "Hello".slice(1,4) // "ell"

split(separator) Splits string into array. "a,b,c".split(",") // ["a","b","c"]

substr(start,len) Extracts part of string (deprecated). "Hello".substr(1,3) // "ell"

substring(start,end) Extracts chars between indexes. "Hello".substring(1,4) // "ell"

toLocaleLowerCase() Converts string to lowercase by locale. "TÜRK".toLocaleLowerCase("tr") // "türk"

toLocaleUpperCase() Converts string to uppercase by locale. "i".toLocaleUpperCase("tr") // "■"

toLowerCase() Converts to lowercase. "Hello".toLowerCase() // "hello"

toUpperCase() Converts to uppercase. "Hello".toUpperCase() // "HELLO"

toString() Returns string representation. (123).toString() // "123"

valueOf() Returns primitive value of string. new String("Hi").valueOf() // "Hi"

String HTML Wrappers


Method Description Example

anchor(name) Creates an <a name> anchor. "link".anchor("myAnchor") // '<a name="myAnchor">link</a

big() Displays string in big text. "Hello".big() // '<big>Hello</big>'

blink() Makes text blink (deprecated). "Hi".blink() // '<blink>Hi</blink>'

bold() Makes text bold. "Hello".bold() // '<b>Hello</b>'


fixed() Fixed-width font text. "Hi".fixed() // '<tt>Hi</tt>'

fontcolor(color) Sets font color. "Hi".fontcolor("red") // '<font color="red">Hi</font>'

fontsize(size) Sets font size. "Hi".fontsize(5) // '<font size="5">Hi</font>'

italics() Makes text italic. "Hi".italics() // '<i>Hi</i>'

link(url) Creates hyperlink. "Google".link("https://google.com") // '<a href="https://goog

small() Displays string in small text. "Hi".small() // '<small>Hi</small>'

strike() Strikethrough text. "Hi".strike() // '<strike>Hi</strike>'

sub() Displays as subscript. "H2O".sub() // '<sub>H2O</sub>'

sup() Displays as superscript. "x2".sup() // '<sup>x2</sup>'

You might also like