- The sequence of one or more characters enclosed within quotation marks is called a string.
- The quotation can be single quotes
''or double quotes
" "or backtick ``.
- And, the sequence of characters can be alphabets, numbers, symbols, etc.
Note, in the above example the whitespace, comma, and exclamation mark are also part of the string.
charAt()returns the character at a specified index in a string.
- The very first character of the string has an index of 0, the second character has index 1, and so on...
- This method extracts the part of the string between
endand returns the substring.
substr()method returns the specified number of characters from the specified index (
startparameter) from a given string.
startdefines the starting index from where the substring is to be extracted from the original string.
lengthdefines the number of characters to be extracted from the specified start index.
Note: If the
lengthparameter isn't given, then all the characters from start till the end of the string are extracted.
concat()method joins two or more strings.
concat()method doesn't modify the original strings, but it returns a new string.
toUpperCase()method converts the strings to upper case letters.
toLowerCase()method converts the strings to lower case letters.
slice()method extracts and returns a part of the string from start to (excluding) the end character.
- If there's no second argument specified, then the slice method extracts till the end of the string.
slice()also works on negative indices. If a negative index is specified, the string is extracted from the right end.
- The negative index starts from -1 and it indicates the last character of the string, -2 is the second last character, and so on...
replace()method is used to replace a part of a given string with a new substring.
- One important thing to note here is that the
replace()method does not change the original string on which it is called upon. It simply returns a new string.
includes()method does a case-sensitive search on the original string to see if the specified substring is present in the string or not.
- If the specified string is present, the method returns
trim()method removes leading and trailing whitespaces from the given string.
- Working through the examples is the best idea to understand these methods. Play with the examples.