Basic String Methods

·

3 min read

As soon as you add a dot , there is a wrapper that gets created around the premitive values that gives them access to the methods of specific data types . It includes :-

1.charAt

Accepts one parameter on which you want the one charAt . By default index is 0.

syntax: charAt(index)

index

An integer between 0 and str.length - 1. If the index cannot be converted to the integer or no index is provided, the default is 0, so the first character of str is returned.

Examples

var string = 'I Love Coding';
console.log("The character at index 0   is '" + string.charAt()   + "'");
The character at index 0   is 'B'

2.trim

It will remove all the white spaces in the start and in the end. To return a new string with whitespace trimmed from just one end, use trimStart() or trimEnd().

syntax: trim()

Examples

var string = '         I Love Coding       ' ;
console.log( string.trim() );
 I Love Coding

3.concat

Used for concatinating multiple string values and returns a new string.

syntax: concat(str1)
        concat(str1, str2)
        concat(str1, str2, ... , strN)

Examples

let str= 'Hello, '
console.log(str.concat('I LOVE CODING'))
// Hello, I LOVE CODING

4.startsWith

returns true or false based on whether that string starts with character/value or not.

syntax: startsWith(searchString)
        startsWith(searchString, position)

Parameters

searchString

The characters to be searched for at the start of this string.

position (Optional)

The position in this string at which to begin searching for searchString. Defaults to 0.

Examples

let str = 'I love coding';

console.log(str.startsWith('c' , 7))          // true

5.includes

It does exactly what it means. Returns true or false based on whether that character or value is present inside that string or not .

syntax: includes(searchString)
        includes(searchString, position)

Examples

 let str = 'I love coding';

console.log(str.includes('c'))          // true

6.indexOf

Returns index of specific character that you are looking for.

Syntax:
indexOf(searchString)
indexOf(searchString, position)

Return value

The index of the first occurrence of searchString found, or -1 if not found.

Examples

'hello world'.indexOf('', 8) // returns 8
'hello world'.indexOf('', 22) // returns 11
'Blue Whale'.indexOf('Whale', 5)  // returns  5

7.lastIndexOf

Returns last index of specific character that you are looking for.

Syntax:
indexOf(searchString)
indexOf(searchString, position)

Return value

The index of the last occurrence of searchString found, or -1 if not found.

Examples

'naman'.lastIndexOf('a');     // returns 3
'naman'.lastIndexOf('a', 2);  // returns 1

8.repeat

Used for repeating the same string multiple times.

Syntax: repeat(count)

count

An integer between 0 and +Infinity, indicating the number of times to repeat the string.

Return value

A new string containing the specified number of copies of the given string.

Examples

'xyz'.repeat(1)     // 'xyz'
'xyz'.repeat(2)     // 'xyzxyz'
'xyz'.repeat(3.5)   // 'xyzxyzxyz' (count will be converted to integer)
'xyz'.repeat(1/0)   // RangeError

8.replaceAll

Used for replacing all the occurances of any specific character/value .The original string is left unchanged.

Examples

'aabbbbbbbbcc'.replaceAll('b', '.');      // 'aa........cc'

9.slice

Accepts two parameter the startindex and endindex . Second parameter is a optional one. Used for removing the character/value .

Syntax:
slice(beginIndex)
slice(beginIndex, endIndex)

Parameters

beginIndex

The zero-based index at which to begin extraction.

endIndex ( Optional )

The zero-based index before which to end extraction. The character at this index will not be included.

Examples

let str1 = 'The morning is upon us.', // the length of str1 is 23.
    str2 = str1.slice(1, 8),
    str3 = str1.slice(4, -2),
    str4 = str1.slice(12),
    str5 = str1.slice(30);
console.log(str2)  // OUTPUT: he morn
console.log(str3)  // OUTPUT: morning is upon u
console.log(str4)  // OUTPUT: is upon us.
console.log(str5)  // OUTPUT: ""