[Go to site: main page, start]

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

JavaScript trimEnd Method Explained

The document provides an overview of strings in JavaScript, detailing their definition, indexing methods, properties, and various built-in methods for manipulation. It covers string creation, accessing characters, and methods such as toUpperCase, toLowerCase, concat, replace, and many others, along with examples for each. Additionally, it explains how to use these methods effectively to manage and manipulate string data.

Uploaded by

shaheenroblox
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 views11 pages

JavaScript trimEnd Method Explained

The document provides an overview of strings in JavaScript, detailing their definition, indexing methods, properties, and various built-in methods for manipulation. It covers string creation, accessing characters, and methods such as toUpperCase, toLowerCase, concat, replace, and many others, along with examples for each. Additionally, it explains how to use these methods effectively to manage and manipulate string data.

Uploaded by

shaheenroblox
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

Strings in JavaScript

What is a String?
– A String is a data type used for storing text.
– A String consists of characters: alphabets, numbers, symbols, and spaces.
– Strings are enclosed in single (' ') or double (" ") quotes.
– In JavaScript, a String is also an object with its own properties and methods.
– Example:
• var str1 = "Hello";
• var str2 = 'World';

Indexing in JavaScript Strings


Each character in a string is assigned an index number.
There are two types of indexes:
• Positive Index
• Negative Index
Positive Index (Left to Right)
• Indexing starts from 0.
• Moves from left to right.
• Used to access characters from the beginning of the string.
Negative Index (Right to Left)
• Starts from -1 (last character).
• Moves from right to left.
Example:
var str=”HELLO WORLD”

To access the characters:


– [Link](str[0]); // Output: H
– [Link](str[10]); // Output: D
– [Link](str[-3]); // Output: R

String Property
length
– It returns the number of characters in a string (including spaces).
– The length property of an empty string is 0.
Syntax:
[Link]

Page 1 of 11
Example 1:

var message = "HELLO WORLD";


document. write ([Link]); // Output: 11

Example 2:
var str = " ";
document. write(str. length) // it returns 1.

Example 3: H
<script> E
L
var text = "HELLO WORLD"; L
O
for (var i = 0; i < [Link]; i++)
{ W
O
[Link](text[i] + "<br>"); R
} L
D
</script>

String Methods
toUpperCase( )
– The toUpperCase() method converts a string to uppercase letters.
– It does not change the original string.
Syntax:
[Link]()
Example:
var text = "Hello World!";
var result = [Link]();
[Link](result) // HELLO WORLD!

[Link]("cbse".toUpperCase()) // CBSE

toLowerCase( )
– The toLowerCase( ) method converts a string to lowercase letters.
– The toLowerCase( ) method does not change the original string.
Syntax:
[Link]( )
Example:
text = "Hello World!";
result = [Link]( );
[Link](result) // hello world!
[Link](“CBSE”. toLowerCase( )) // cbse

Page 2 of 11
concat( )
– It joins two or more strings.
– It does not change the existing strings.
– It returns a new string.
– The concat() method can be used instead of the plus operator.
Syntax:
[Link](string1, string2, ..., stringX)
Example:
var text1 = "Hello"
var text2 = "World"
[Link]([Link]( text2)) // HelloWorld
[Link]([Link]( " ", text2)) // Hello World
[Link](" CBSE ".concat( "EXAM ", "2026")) // CBSE EXAM 2026

replace( )
– Replaces first occurrences of the specified character/word with the new character/word.
Syntax:
[Link](searchValue, newValue)
Example1:
var str="Please visit My website !";
var n = str. replace("website", "JavaScript");
[Link](n) // Please visit My JavaScript !

Example2:
var text = "Mr Blue has a Blue house and a Blue car";
var result = [Link]("Blue", "Red");
[Link](result) // Mr Red has a Blue house and a Blue car

replaceAll( )
– The replaceAll() method returns a new string with all values replaced.
– The replaceAll() method does not change the original string.
Syntax:
[Link](searchValue, newValue)
Example:
var text = "I love Cats. Cats are very easy to love. Cats are very friendly."
var result = [Link]("Cats","Dogs");
[Link](result) // I love Dogs. Dogs are very easy to love. Dogs are very friendly.

charAt( )
– The charAt() method returns the character at a specified index (position) in a string.
– Negative values return an empty string.
Page 3 of 11
– Out of range: Returns an empty string ("").

Syntax:
[Link](index)
Example:
var text = "HELLO WORLD";
[Link]([Link](0)); // H

at( )
– The at() method the character at the specified index.
– It supports negative indexing: -1 refers to the last character.
– Out of range: Returns undefined.
– Index is optional. Default is 0.
Syntax:
[Link](index)
Example:
var text = "Indian Schools";
var character = [Link](7);
[Link](character) // S

trim( )
– The trim() method removes whitespace from both sides of a string.
– The trim() method does not change the original string.
Syntax:
[Link]()
Example:
text = " Hello World! ";
[Link]([Link]) // 27
result1 = [Link]();
[Link](result1) // Hello World!
[Link]([Link]) // 12

trimStart( )
– The trimStart() method removes whitespace from the beginning of a string.
– The trimStart() method does not change the original string.
– The trimStart() method works like trim(), but removes whitespace only from the start of a string.
Syntax:
[Link]()
Example:
text = " Hello World! ";
result2 = [Link]();
[Link](result2) // Hello World!
[Link]([Link]) // 20

Page 4 of 11
trimEnd( )
– The trimEnd() method removes whitespace from the end of a string.
– The trimEnd() method does not change the original string.
– The trimEnd() method works like trim(), but removes whitespace only from the end of a string.
Syntax:
[Link]()
Example:
text = " Hello World! ";
result3 = [Link]();
[Link](result3) // Hello World!
[Link]([Link]) // 19

match( )
– It is used to search a string for matches based on a regular expression, and returns the matches as
an array.
– The match() method returns an array with the matches.
– The match() method returns null if no match is found.
– //The search() method returns the position of the first match.
Syntax
[Link](regexp)

Example:
//it will only return the first match of "ain".
var text = "The rain in SPAIN stays mainly in the plain";
result=[Link]("ain");
[Link](result) // ain

//A global search for "ain":


var text = "The rain in SPAIN stays mainly in the plain";
[Link]([Link](/ain/g)) // ain,ain,ain

var matches = [Link](/ain/g);


[Link]("Count: " + [Link]); // Count: 4

//A global, case-insensitive search:


vartext = "The rain in SPAIN stays mainly in the plain";
[Link]([Link](/ain/gi)); // ain,AIN,ain,ain

slice( )
– The slice() method extracts a part of a string.
– The slice() method does not change the original string.
– The start and end parameters specify the part of the string to extract.
Page 5 of 11
– handles negative values by counting from the end of the string.
– If the start index is greater than the end index, it returns an empty string.

Syntax:
[Link](start)
[Link](start, end)
[Link](-start, -end)

Example:
var text = "Hello world!";
var result = [Link](0, 5);
[Link](result) // Hello

var text = "Hello world!";


var result = [Link](6);
[Link](result) // world!

substring( )
– The substring() method extracts characters from start to end (exclusive) and returns the substring.
– end - End position (up to, but not including).
– The substring() method does not change the original string.
– If start is greater than end, arguments are swapped: (4, 1) = (1, 4).
– Start or end values less than 0, are treated as 0.
– If "start" is less than 0, it will start from index 0
– It does not handle negative values; negative values are treated as 0.
– If the start index is greater than the end index, substring() automatically swaps the two values.

Syntax:
[Link](start)
[Link](start, end)
Example1:
var text = "Hello world!";
var result = [Link](1, 4);
[Link](result) // ell
Example2:
var str ="This is my first script in JavaScript"
sub=[Link](7, 16)
[Link](sub) // returns "my first" as substring

substr()
– The substr() method extracts a part of a string.
– The substr() method begins at a specified position, and returns a specified number of characters.
– The substr() method does not change the original string.
– length - The number of characters to extract. If omitted, it extracts the rest of the string
Page 6 of 11
– To extract characters from the end of the string, use a negative start position.
Syntax:
[Link](start)
[Link](start, length)
Example:
var text = "Hello world!";
var result = [Link](2);
[Link](result) // llo world!

includes( )
– The includes() method returns true if a string contains a specified string.
– Otherwise it returns false.
– The includes() method is case sensitive.
Syntax:
[Link](searchvalue)
[Link](searchvalue, start)
Example:
var text = "Hello world, welcome to the IT world.";
var result = [Link]("world");
[Link](result) //true

var text = "Hello world, welcome to the IT world.";


var result = [Link]("world", 15);
[Link](result) //true

indexOf( )
– The indexOf() method returns the position of the first occurrence of a value in a string.
– It returns -1 if the value is not found.
– This method is case sensitive.
Syntax:
[Link](searchvalue)
[Link](searchvalue, start)
Example:
[Link]("welcome".indexOf('e')) // 1
[Link]("welcome".indexOf('e',2)) // 6
Str = "Hello world, welcome to the world.";
result = [Link]("World");
[Link](result) // -1

search( )
– It returns the starting location of passing string otherwise returns -1.
– It returns the index (position) of the first match.
– It returns -1 if no match is found.
– The search() method is case sensitive.

Page 7 of 11
– The search() method cannot take a start position argument.
– The indexOf() method cannot search against a regular expression.
Syntax:
[Link](searchValue)
Example:
var str = "This is my first webpage"
[Link](str. search("first")) // 11

lastIndexOf( )
– The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a
string.
– It searches the string from the end to the beginning.
– It returns the index from the beginning (position 0).
– It returns -1 if the value is not found.
– It is case sensitive.
Syntax:
[Link](searchvalue)
[Link](searchvalue, start)
Example:
text = "Hello planet earth, you are a great planet.";
result = [Link]("planet");
[Link](result) // 36
[Link]([Link]("Planet")); // -1
[Link]([Link]("planet", 10)); // 6

repeat( )
– The repeat() method returns a string with a number of copies of a string.
– The repeat() method returns a new string.
– The repeat() method does not change the original string.
Syntax:
[Link](count)
Example:
var text = " Hello ISD ";
var result = [Link](3);
[Link](result) // Hello ISD Hello ISD Hello ISD

split( )
– The split() method splits a string into an array of substrings.
– The split() method returns the new array.
– The split() method does not change the original string.
– If (" ") is used as separator, the string is split between words.
– limit are integer that limits the number of splits.

Page 8 of 11
– If the separator parameter is omitted, an array with the original string is returned
Syntax:
[Link](separator)
[Link](separator, limit)
Example:
text = "How are you doing today?";
myArray = [Link](" ");
[Link](myArray) // How,are,you,doing,today?

charCodeAt( )
– The charCodeAt() method returns the code of the character at a specified index in a string:
Syntax:
[Link](index)
Example:
text = "Apple";
code=[Link](4)
[Link](code); // 101

codePointAt( )
– The codePointAt() method returns the Unicode value at an index (position) in a string.
– The index of the first position is 0, the second is 1, ....
– Index Optional, Default value = 0.
– charCodeAt() is UTF-16, codePointAt() is Unicode.
– Both methods return an integer representing the UTF-16 code of a character, but
only codePointAt() can return the full value of a Unicode value greather 0xFFFF (65535).
Syntax:
[Link](index)
Example:
text = "HELLO WORLD";
code = [Link](0);
[Link](code) // 72

endsWith( )
– The endsWith() method returns true if a string ends with a specified string.
– Otherwise, it returns false.
– The endsWith() method is case sensitive.
– Length is optional, the length of the string to search.
– Default value is the length of the string
Syntax:
[Link](searchvalue)
[Link](searchvalue, length)

Page 9 of 11
Example:
var text = "Hello world";
var result = [Link]("World");
[Link](result) // false

startsWith( )
– The startsWith() method returns true if a string starts with a specified string.
– Otherwise it returns false.
– The startsWith() method is case sensitive.
Syntax:
[Link](searchValue)
[Link](searchValue, start)
Example:
var text = "Hello world, welcome to the universe.";
result=[Link]("Hello");
[Link](result) //true

padEnd( )
– The padEnd() method pads a string at the end.
– The padEnd() method pads a string with another string (multiple times) until it reaches a given length.
– The padEnd() method is a string method.
– To pad a number, convert the number to a string first.
Syntax:
[Link](length, string)
Example:
text = "5";
padded = [Link](4,"x");
[Link](padded) // 5xxx

padStart( )
– The padStart() method pads a string from the start.
– The padStart() method pads a string with another string (multiple times) until it reaches a given
length.
– Default is space.
Syntax:
[Link](length, string)
Example:
text = "8";
padded = [Link](3,"#");
[Link](padded) // ##8

Page 10 of 11
Name Description
charAt() Returns the character at a specified index (position)
charCodeAt() Returns the Unicode of the character at a specified index
concat() Returns two or more joined strings
constructor Returns the string's constructor function
endsWith() Returns if a string ends with a specified value
fromCharCode() Returns Unicode values as characters
includes() Returns if a string contains a specified value
indexOf() Returns the index (position) of the first occurrence of a value in a string
lastIndexOf() Returns the index (position) of the last occurrence of a value in a string
length Returns the length of a string
localeCompare() Compares two strings in the current locale
match() Searches a string for a value, or a regular expression, and returns the matches
prototype Allows you to add properties and methods to an object
repeat() Returns a new string with a number of copies of a string
replace() Searches a string for a pattern, and returns a string where the first match is replaced
replaceAll() Searches a string for a pattern and returns a new string where all matches are replaced
search() Searches a string for a value, or regular expression, and returns the index (position) of the
slice() Extracts a part of a string and returns a new string
split() Splits a string into an array of substrings
startsWith() Checks whether a string begins with specified characters
substr() Extracts a number of characters from a string, from a start index (position)
substring() Extracts characters from a string, between two specified indices (positions)
toLocaleLowerCase() Returns a string converted to lowercase letters, using the host's locale
toLocaleUpperCase() Returns a string converted to uppercase letters, using the host's locale
toLowerCase() Returns a string converted to lowercase letters
toString() Returns a string or a string object as a string
toUpperCase() Returns a string converted to uppercase letters
trim() Returns a string with removed whitespaces
trimEnd() Returns a string with removed whitespaces from the end
trimStart() Returns a string with removed whitespaces from the start
valueOf() Returns the primitive value of a string or a string object

Page 11 of 11

You might also like