[Go to site: main page, start]

0% found this document useful (0 votes)
11 views4 pages

Java Arrays and ArrayList Methods

JavaScript strings are immutable sequences of characters used to represent text, created using string literals or the String() constructor. Essential methods for string manipulation include length, charAt, includes, and replace, among others. These methods allow for various operations such as searching, modifying, and formatting strings.

Uploaded by

raj2007demon
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)
11 views4 pages

Java Arrays and ArrayList Methods

JavaScript strings are immutable sequences of characters used to represent text, created using string literals or the String() constructor. Essential methods for string manipulation include length, charAt, includes, and replace, among others. These methods allow for various operations such as searching, modifying, and formatting strings.

Uploaded by

raj2007demon
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

JavaScript Strings: An Overview

In JavaScript, a String is a sequence of zero or more characters used to represent text.


JavaScript strings are immutable, meaning they cannot be changed after they are
created. Any operation that appears to modify a string actually creates a new string.

How to Create a String


Strings are typically created using string literals, which are enclosed in single quotes
('...'), double quotes ("..."), or backticks (`...`) for template literals.

Creation Type Code Example Description

Single Quotes const str1 = 'Hello Standard way to create a string literal.
World';

Double Quotes const str2 = "Hello Standard way to create a string literal.
World";

Template Literal const str3 = `Hello Uses backticks. Allows for embedded expressions
${name}`; (${expression}) and multi-line strings.

String() Constructor const str4 = new Creates a String object (less common and often
String('Hello'); discouraged as it can lead to confusion with
primitive strings).

Essential String Methods


The following table details the most common and essential methods available for the
JavaScript String object.

Method Code Example Description

length const len = Property, not a method. Returns the length


"text".length; (number of characters) of the string.
Method Code Example Description

charAt(index) const ch = Returns the character at the specified index.


"Java".charAt(1); (Result: 'a')

charCodeAt(index) const code = Returns the Unicode value (an integer between 0
"a".charCodeAt(0); and 65535) of the character at the specified
index. (Result: 97)

at(index) const ch = Returns the character at the specified index.


"Java".at(-1); Supports negative indexing from the end of the
string. (Result: 'a')

concat(str2, ...) const result = Combines the text of two or more strings and
"Hi".concat(" there", returns a new string. (Result: "Hi there!")
"!");

includes(searchString, const b = Checks whether a string contains the specified


position) "book".includes("oo"); string. Returns true or false. (Result: true)

startsWith(searchString, const b = Checks if a string begins with the characters of a


position) "Hello".startsWith("He") specified string. Returns true or false. (Result:
; true)

endsWith(searchString, const b = Checks if a string ends with the characters of a


length) "Hello".endsWith("lo"); specified string. Returns true or false. (Result:
true)

indexOf(searchValue, const index = Returns the index of the first occurrence of the
fromIndex) "banana".indexOf("an"); specified value. Returns -1 if not found. (Result:
1)

lastIndexOf(searchValue, const index = Returns the index of the last occurrence of the
fromIndex) "banana".lastIndexOf("an specified value. Returns -1 if not found. (Result:
"); 3)

substring(indexStart, const sub = Returns a part of the string, starting from


indexEnd) "Hello".substring(1, 4); indexStart up to (but not including) indexEnd.
(Result: "ell")
Method Code Example Description

slice(indexStart, const sub = Extracts a section of a string and returns it as a


indexEnd) "Hello".slice(-2); new string. Supports negative indices. (Result:
"lo")

substr(start, length) const sub = Returns a part of the string, starting from start
"Hello".substr(1, 3); for a specified length. (Deprecated) (Result:
"ell")

replace(regexp|substr, const rep = Searches a string for a value or a regular


newSubstr|function) "java".replace("a", expression, and returns a new string with the
"o"); specified replacement. Only replaces the first
match unless a global regex (/g) is used. (Result:
"jova")

replaceAll(regexp|substr const rep = Replaces all occurrences of a substring or regular


, newSubstr|function) "a-b-c".replaceAll("-", expression match with a replacement. (Result:
"/"); "a/b/c")

toLowerCase() const lower = Converts all characters in the string to lowercase.


"JAVA".toLowerCase(); (Result: "java")

toUpperCase() const upper = Converts all characters in the string to uppercase.


"java".toUpperCase(); (Result: "JAVA")

trim() const trimmed = " text Removes whitespace from both ends of a string.
".trim(); (Result: "text")

trimStart() (or trimLeft()) const s = " text Removes whitespace from the beginning of a
".trimStart(); string. (Result: "text ")

trimEnd() (or trimRight()) const s = " text Removes whitespace from the end of a string.
".trimEnd(); (Result: " text")

split(separator, limit) const arr = Splits a string into an array of substrings based
"a,b,c".split(","); on a specified separator. (Result: ["a", "b",
"c"])

repeat(count) const s = "a".repeat(3); Returns a new string with the string repeated a
specified number of times. (Result: "aaa")
Method Code Example Description

match(regexp) const matches = "1 apple Retrieves the result of matching a string against a
and 2 regular expression. Returns an array or null.
bananas".match(/\\d/g); (Result: ["1", "2"])

search(regexp) const index = Searches for a match between a regular


"test".search(/e/); expression and this string. Returns the index of
the first match, or -1. (Result: 1)

valueOf() const s = new Returns the primitive value of a String object.


String("hi").valueOf(); (Result: "hi")

localeCompare(otherStrin const diff = Compares two strings in the current locale.


g, locales, options) "a".localeCompare("b"); Returns a number indicating if the reference
string comes before, after, or is equivalent to the
compared string.

toString() const s = Returns a string representation of the object.


123..toString();

normalize(form) const s = Returns the Unicode Normalization Form of a


'a\́
'.normalize('NFC'); string.

padStart(targetLength, const s = Pads the current string with another string until
padString) "5".padStart(2, "0"); the resulting string reaches the given length from
the start. (Result: "05")

padEnd(targetLength, const s = "5".padEnd(2, Pads the current string with another string until
padString) "0"); the resulting string reaches the given length from
the end. (Result: "50")

You might also like