Skip to main content

Command Palette

Search for a command to run...

JavaScript Strings.

Published
2 min read
JavaScript Strings.
O

Passionate about crafting elegant code and building innovative solutions that make a positive impact. 🚀 Join me on a journey through the realms of technology as I share my experiences, insights, and latest discoveries. 💡 Let's dive into the world of software engineering, development trends, and emerging technologies together. Follow along for a dose of tech inspiration and knowledge.

The String objects lets you work with a series of characters; it wraps JavaScript's string primitive data type with a number of helper methods.

As JavaScript automatically converts between string primitives and string objects, you can call any of the helper methods of the string object on a string primitive.

Use the following syntax to create a string object:

var val = new String(string);

The string parameter is a series of characters that has been properly encoded.

String Properties

Following is a list of the properties of String object and its description:

charAt(position):

Returns the character at the specified position (in Number).

charCodeAt(position):

Returns a number indicating the Unicode value of the character at the given position (in Number).

concat([string,,]):

Joins specified string literal values (specify multiple strings separated by comma) and returns a new string.

indexOf(SearchString, Position):

Returns the index of first occurrence of specified String starting from specified number index. Returns -1 if not found.

lastIndexOf(SearchString, Position):

Returns the last occurrence index of specified SearchString, starting from specified position. Returns -1 if not found.

localeCompare(string,position):

Compares two strings in the current locale.

match(RegExp):

Search a string for a match using specified regular expression. Returns a matching array.

replace(searchValue, replaceValue):

Search specified string value and replace with specified replace Value string and return new string.

Regular expression can also be used as searchValue.

search(RegExp):

Search for a match based on specified regular expression.

slice(startNumber, endNumber):

Extracts a section of a string based on specified starting and ending index and returns a new string.

split(separatorString, limitNumber):

Splits a String into an array of strings by separating the string into substrings based on specified separator. Regular expression can also be used as separator.

substr(start, length):

Returns the characters in a string from specified starting position through the specified number of characters (length).

substring(start, end):

Returns the characters in a string between start and end indexes.

toLocaleLowerCase():

Converts a string to lower case according to current locale.

toLocaleUpperCase():

Converts a sting to upper case according to current locale.

toLowerCase():

Returns lower case string value.

toString():

Returns the value of String object.

toUpperCase():

Returns upper case string value.

valueOf():

Returns the primitive value of the specified string object.

More from this blog

Bode's blog

44 posts

A developer with a passion for new tech, designing experiences, and user interfaces. Creating harmony between people and technology.