This is the 23rd day of my participation in the August More Text Challenge

First, find judgment

1.1 charAt

The charAt() method returns the specified character from a string.

str.charAt(index)

  • @params: an integer between 0 and the length of the string minus 1. (0 to length-1), the default value is 0.
  • @returnCharacter:
  • Whether to change the original string: don’t change
const str = 'hello';
console.log(str.charAt(2));  // 'e'
console.log(str.charAt(5));  / /"
Copy the code

1.2 charCodeAt

The charCodeAt() method returns an integer between 0 and 65535, representing the UTF-16 code unit at the given index.

str.charCodeAt(index)

  • @params: an integer greater than or equal to 0 and less than the length of the string. If it is not a numeric value, it defaults to 0.
  • @return: A number that specifies the UTF-16 code unit value of the character at index; If index is out of range, charCodeAt() returns NaN.
  • Whether to change the original string: don’t change
const str = 'hello';
console.log(str.charCodeAt(2));  / / 108
console.log(str.charCodeAt(5));  // NaN
Copy the code

1.3 indexOf

The indexOf() method returns the indexOf the specified value that first occurs in the String calling it, searching from fromIndex. If the value is not found, -1 is returned.

str.indexOf(searchValue [, fromIndex])

  • @params:
    • SearchValue: is the string value to be searched;
    • FromIndex: indicates the position to start the search. Can be any integer. The default value is 0.
  • @return: The index of the first occurrence of the searched string searchValue, or -1 if none is found.
  • Whether to change the original string: don’t change
console.log('Blue Whale'.indexOf('Blue'))   / / returns 0
console.log('Blue Whale'.indexOf('Blute'))   / / return 1
console.log('Blue Whale'.indexOf('Whale'.0))   / / return 5
console.log('Blue Whale'.indexOf('Whale'.5))   / / return 5
Copy the code

1.4 lastIndexOf

The lastIndexOf() method returns the index of the last time the specified value of a String was called, searching backwards in a String at the specified fromIndex position. Returns -1 if this particular value is not found.

str.lastIndexOf(searchValue[, fromIndex])

  • @params:
    • SearchValue: is a string that represents the value being searched. If searchValue is an empty string, fromIndex is returned. ;
    • FromIndex: Optional. The beginning character of the string searchValue to be matched looks back to the left from the fromIndex bit of STR.
  • @return: returns the index of the last occurrence of the specified value (which is still counted from left to right starting with 0), or -1 if not found.
  • Whether to change the original string: don’t change
const str = 'Brave new world';
console.log(str.lastIndexOf('w'));
Copy the code

1.5 the startsWith

The startsWith() method is used to determine whether the current string begins with another given substring and returns true or false depending on the result.

str.startsWith(searchString[, position])

  • @params:
    • SearchString: indicates the substring to search for;
    • Position: optional, indicates the starting position of the searchString in STR. The default value is 0.
  • @return: Returns true if the given character is found at the beginning of the string; Otherwise return false.
  • Whether to change the original string: don’t change
const str = 'Saturday night plans';
console.log(str.startsWith('Sat'));  // true
console.log(str.startsWith('Sat'.3));  // false
Copy the code

1.6 the endsWith

The endsWith() method checks whether the current string is “terminated” by another given substring, returning true or false depending on the result.

str.endsWith(searchString[, length])

  • @params:
    • SearchString: indicates the substring to search for;
    • Length: optional, is the length of STR. The default value is str.length.
  • @return: Returns true if the substring passed is at the end of the search string, false otherwise.
  • Whether to change the original string: don’t change
const str = 'To be, or not to be, that is the question.';

console.log(str.endsWith('question.'));  // true
console.log(str.endsWith('to be'));      // false
console.log(str.endsWith('to be'.19));  // true
Copy the code

1.7 includes

The includes() method is used to determine whether one string is included in another string, returning true or false depending on the case.

str.includes(searchString[, position])

  • @params:
    • SearchString: Represents the string to search in this string
    • Position: Optional, indicates the index position of the current string from which to search for substrings. The default value is 0.
  • @return: Returns true if the current string contains the searched string; Otherwise return false.
  • Whether to change the original string: don’t change
const str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be'.1));    // false
console.log(str.includes('TO BE'));       // false
Copy the code

1.8 localeCompare

The localeCompare() method returns a number indicating whether a reference string is before or after the sort order or the same as the given string.

referenceStr.localeCompare(compareString[, locales[, options]])

  • @params:
    • CompareString: indicates the string used for comparison.
    • Locales: Optional, a BCP 47 compliant string or an array of strings representing one or more languages or regions.
    • Options: Optional.
  • @return: negative if the reference character precedes the comparison character; Positive if the reference character exists after the comparison character; It returns 0 if it’s equal.
  • Whether to change the original string: don’t change
console.log('a'.localeCompare('c'));  // -1 
console.log('check'.localeCompare('against'));  / / 1
console.log('a'.localeCompare('a'));  / / 0
Copy the code