Javascript String Methods Flashcards

1
Q

Convert a number, Boolean, or an object to a string

A

.toString()

var myNumber = 24; // 24

var myString = myNumber.toString(); // “24”

String()

var myNumber = 24; // 24

var myString = String(myNumber); // “24”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Split a String Into Multiple Substrings

A

.split()

var myString = “coming,apart,at,the,commas”;

var substringArray = myString.split(“,”); // [“coming”, “apart”, “at”, “the”, “commas”]

var arrayLimited = myString.split(“,”, 3); // [“coming”, “apart”, “at”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Split a String Into Multiple Substrings while limiting the amount of splits you take

A

.split(“,”, 3)

var myString = “coming,apart,at,the,commas”;

var arrayLimited = myString.split(“,”, 3); // [“coming”, “apart”, “at”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Get the Length of a String

A

.length

var myString = “You’re quite a character.”;

var stringLength = myString.length; // 25

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Locate the first Substring within a String

A

.indexOf()

The indexOf() method starts searching for the substring (the first argument passed in) from the beginning of the string, and returns the position of the start of the first occurrence of the substring.

var stringOne = “Johnny Waldo Harrison Waldo”;

var wheresWaldo = stringOne.indexOf(“Waldo”); // 7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Locate the last Substring within a String

A

.lastIndexOf()

The lastIndexOf() method is exactly the same except it returns the starting position of the last occurrence of the passed in substring.

var stringOne = “Johnny Waldo Harrison Waldo”;

var wheresWaldo = stringOne.lastIndexOf(“Waldo”); // 22

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Locate the first Substrig withing a String from a certain index

A

indexOf(“word”, 5)

starts searching at character 5, ignoring characters 0-4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Locate the last Substring within a String from a certain index

A

.lastIndexOf(“word”, 5)

starts searching at character 5 and goes in reverse, ignoring characters 6 and above.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Replace a Substring

A

.replace()

To replace part or all of a string with a new string, you can use replace(). The first argument is the substring you want to replace, and the second argument is the new substring. This will only replace the first instance of the matched substring.

var slugger = “Josh Hamilton”;

var betterSlugger = slugger.replace(“h Hamilton”, “e Bautista”);

console.log(betterSlugger); // “Jose Bautista”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Replace all instances of the matched substring

A

Use .replace() with a regular expression with the global flag

var myString = “She sells automotive shells on the automotive shore”;

var newString = myString.replace(/automotive/g, “sea”);

console.log(newString); // “She sells sea shells on the sea shore”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Find the Character at a Given Position

A

.charAt()

var myString = “Birds of a Feather”; var whatsAtSeven = myString.charAt(7); // “f”

.charCodeAt()

  • gives you the character code, instead of the character itself.*
  • var myString = “Birds of a Feather”;*
  • var whatsAtSeven = myString.charCodeAt(7); // “102”*
  • var whatsAtEleven = myString.charCodeAt(11); // “70”*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Concatenating Multiple Strings

A

.concat()

For the most part, when you concatenate strings, you’ll use the addition operator (+). But you do also have the option to use the concat() method:

var stringOne = “Knibb High football “;

var stringTwo = stringOne.concat(“rules.”); // “Knibb High football rules”

You can also pass multiple strings into it, and all will be appended (in the order they appear) to the original string:

var stringOne = “Knibb “; var stringTwo = “High “;

var stringThree = “football “; var stringFour = “rules.”;

var finalString = stringOne.concat(stringTwo, stringThree, stringFour);

console.log(finalString); // “Knibb High football rules.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Slice a String (Extract a Substring)

A

.slice()

var stringOne = “abcdefghijklmnopqrstuvwxyz”;

var stringTwo = stringOne.slice(5, 10); // “fghij”

.substring()

var stringOne = “abcdefghijklmnopqrstuvwxyz”;

var stringTwo = stringOne.substring(5, 10); // “fghij”

For both slice() and substring(), the first argument is the character at which to begin the substring (again using zero-based numbering) and the second argument (which is optional) is the character in the string after you want the substring to end. So in the examples above, the arguments “5, 10” mean characters 5 through 9 are “sliced” to create the new string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Slice a String and take a set number of characters

A

.substr()

For substr(), once again the first argument represents the character that begins the new string and the second argument is optional. But this time, the second argument represents the total number of characters that should be included, starting with the character in the “5” position.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Convert a String to Uppercase

A

.toLocaleUpperCase() or .toUpperCase()

var stringOne = “Speak up, I can’t hear you.”;

var stringTwo = stringOne.toLocaleUpperCase(); // “SPEAK UP, I CAN’T HEAR YOU”

var stringThree = stringOne.toUpperCase(); // “SPEAK UP, I CAN’T HEAR YOU”

Generally, the results between the “locale” method and the non-locale method are the same, but according to MDN’s reference “for some locales, such as Turkish, whose case mappings do not follow the default case mappings in Unicode, there may be a different result.” Zakas’ book says “if you do not know the language in which the code will be running, it is safer to use the locale-specific methods.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Convert a String to Lowercase

A

.toLocaleLowerCase() or .toLowerCase()

var stringOne = “YOU DON’T HAVE TO YELL”;

var stringTwo = stringOne.toLocaleLowerCase(); // “you don’t have to yell”

var stringThree = stringOne.toLowerCase(); // “you don’t have to yell”

Generally, the results between the “locale” method and the non-locale method are the same, but according to MDN’s reference “for some locales, such as Turkish, whose case mappings do not follow the default case mappings in Unicode, there may be a different result.” Zakas’ book says “if you do not know the language in which the code will be running, it is safer to use the locale-specific methods.”

17
Q

Pattern Matching (2 options)

A

For both methods, only the first matched occurrence is returned. If no match is found it will return null.

.match() method is called on a string and is passed a regular expression

var myString = “How much wood could a wood chuck chuck”;

var myPattern = /.ood/;

var myResult = myString.match(myPattern); // [“wood”]

var patternLocation = myResult.index; // 9

var originalString = myResult.input // “How much wood could a wood chuck chuck”

.exec() method is called on a RegExp object and is passed the string

var myString = “How much wood could a wood chuck chuck”;

var myPattern = /.huck/;

var myResult = myPattern.exec(myString); // [“chuck”]

var patternLocation = myResult.index; // 27

var originalString = myResult.input // “How much wood could a wood chuck chuck”