In JS, String is one type of variable. String stores a series of characters (A-Z, a-z, 0-1, space, special characters etc.) You can use double quote/ single quote to store a string variable. It can be declared with let, const and also using var.
String indexing starts from 0 and so on.
String Methods:
charAt():
This function returns the character from the string.of the specified position which is given as a parameter.
concat():
This joins more than one function together. The resulting string doesn’t change any of the existing string after concatenation.
But there is a more easy method than using which you can easily handles necessary spaces.
includes():
This returns true if the string to be checked contains the specific string which is sent as a parameter. Otherwise, false. It is case sensitive. So, the output of this function is boolean. The position from where the start operation will start is optional, default 0.
endsWith():
This function returns true if a string ends with the specified string which is sent as a parameter. Otherwise, false. It also gives output a boolean. Also case sensitive.
indexOf():
This method is also case sensitive. It checks if the specific value is in the string or not. It it is, then returns the position of first occurrence of it, otherwise returns -1. It is optional to give the information as a function parameter from where the value checking will start. By default, it starts from 0 position.
replace():
This replaces the specific part of the string string with any specific value. It doesn’t affect the original string.
slice():
This method extracts a part of the string and returns that part as output. It seeks 2 parameters, start & end. end is optional.
split():
This method splits the target string and returns the substrings according to the condition of splitting.
startsWith():
This gives output true if the specified string starts with a specific string. Otherwise false. It also takes an optional parameter which defines the starting position.
substr():
It also returns a part of the string like slice().
But they have a couple of differences between them. With a negative argument, slice() starts counting from the end of the string, whereas substr() considers negative value as zero.
toLowercase():
This converts the whole string to a lowercase string.
toUppercase():
Converts a string to uppercase letters.
trim():
It removes unnecessary white spaces from both sides of the string.
trimStart() + trimEnd():
trimStart() removes whitespaces from the starting of the string. rimEnd() removes whitespaces from the end of the string.
Top comments (0)