Day 2. Data Types_String Methods

Going through the exercises, I felt the need to post strings so that I can easily look up those methods when I need it. Let’s start!


String Methods

1. length

It returns the number of characters in a string, including the empty space.


2. accessing characters in a string

I can access to characters in a string by using their indexes. Indexes start with 0. I can indicate the last index using string.length - 1.


3. toUpperCase()

toUpperCase()
  • Return Value : A new string representing the calling string converted to upper case.


4. toLowerCase()

toLowerCase()
  • Return Value : A new string representing the calling string converted to lower case.


5. substr()

no longer recommended


6. substring()

substring(indexStart)
substring(indexStart, indexEnd)
  • Parameters : indexStart, indexEnd(optional) It takes two arguments: The starting index, and the stopping index. The stopping index is NOT included in the returned value.

  • Return Value : A new string containing the specified part of the given string.


7. split()

It splits a string at a specified place. In the separator parameter, indicate the criteria of where to split.

split()
split(separator)
split(separator, limit)
  • Parameters : separator(optional), limit(optional)

  • Return Value : An array of strings, split at each point where the separator occurs in the given string.


8. trim()

It removes the trailing space in the beginning and at the end of a string.

trim()
  • Return Value : A new string representing str stripped of whitespace from both its beginning and end.
    To return a new string with whitespace trimmed from just one end, use trimStart() or trimEnd().


9. includes()

It takes a substring argument and checks if the argument exists in the string. It returns a boolean value.

includes(searchString)
includes(searchString, position)
  • Parameters : searchString, position(optional)

  • Return Value : true if the search string is found anywhere within the given string; otherwise, false if not.


10. replace()

It takes an old substring, and the new substring as parameters.

replace(regexp, newSubstr)
replace(regexp, replacerFunction)

replace(substr, newSubstr)
replace(substr, replacerFunction)
  • Parameters : regexp(pattern), substr, newSubstr(replacement), replacerFunction(replacement)

  • Return Value : A new string, with some or all matches of a pattern replaced by a replacement.


11. charAt()

It takes an index and returns he value at that index.

charAt(index)
  • Parameters : index 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.

  • Return Value : A string representing the character at the specified index. If index is out of range, charAt() returns an empty string.


12. charCodeAt()

It takes an index and returns he value at that index.

charCodeAt(index)
  • Parameters : index If index is not a number, it defaults to 0.

  • Return Value : A number representing the UTF-16 code unit value of the character at the given index. If index is out of range, charCodeAt() returns NaN.


13. indexOf()

It takes a substring and returns the first position of the substring if it exists. If it doesn’t exist, it returns -1.

indexOf(searchString)
indexOf(searchString, position)
  • Parameters : searchString, position(optional) If the method is called with no arguments, searchString is coerced to "undefined".

  • Return Value : The index of the first occurrence of searchString found, or -1 if not found.


14. lastIndexOf()

It takes a substring and returns the last position of the substring if it exists. If it doesn’t exist, it returns -1.

lastIndexOf(searchString)
lastIndexOf(searchString, position)
  • Parameters : searchString, position(optional) If the method is called with no arguments, searchString is coerced to "undefined".

  • Return Value : The index of the last occurrence of searchString found, or -1 if not found.


15. concat()

It takes many substrings as an argument and joins them.

concat(str1)
concat(str1, str2)
concat(str1, str2, ... , strN)
  • Parameters : strN

  • Return Value : A new string containing the combined text of the strings provided.


16. startsWith()

It takes a substring as an argument and checks if the string starts with that argument. It returns a boolean value.

startsWith(searchString)
startsWith(searchString, position)
  • Parameters : searchString, position(optional)

  • Return Value : true if the given characters are found at the beginning of the string; otherwise, false.


17. endsWith()

It takes a substring as an argument and checks if the string ends with that argument. It returns a boolean value.

endsWith(searchString)
endsWith(searchString, length)
  • Parameters : searchString, length(optional)

  • Return Value : true if the given characters are found at the beginning of the string; otherwise, false.


It takes a substring and returns the index of the first match. The searching value can be string or regular expression pattern.

search(regexp)
  • Parameters : regexp If a non-RegExp object regexp is passed, it is implicitly converted to a RegExp.

  • Return Value : The index of the first match between the regular expression and the given string, or -1 if no match was found.


19. match()

It takes a substring or a regular expression pattern and returns an array if it exists, or null if not.

match(regexp)
  • Parameters : regexp If a non-RegExp object regexp is passed, it is implicitly converted to a RegExp.
    If there’s no parameter, it will return an array with an empty string: [””].

  • Return Value : An array whose contents depend on the presence or absence of the global(g) flag, or null if no matches are found.


20. repeat()

It takes a number as an argument and returns a repeated version of the string.

repeat(count)
  • Parameters : count

  • Return Value : A new string containing the specified number of copies of the given string.


Leave a comment