Skip to content

Instantly share code, notes, and snippets.

@melicarls
Last active July 15, 2016 17:04
Show Gist options
  • Select an option

  • Save melicarls/db89429763baeab55e9f8ac48ee46c39 to your computer and use it in GitHub Desktop.

Select an option

Save melicarls/db89429763baeab55e9f8ac48ee46c39 to your computer and use it in GitHub Desktop.
Collection of String Methods researched by WDI-30

String Methods - Thursday, July 14th

.length

Syntax

str.length;

Return Value

Returns the number of characters in the string

Example

"programmer".length;
==> 10

.toUpperCase()

Syntax

str.toUpperCase();

Return Value

Returns a copy of the string with all characters converted to uppercase

Example

"wdi-30 is great".toUpperCase();
==> "WDI-30 IS GREAT"

.toLowerCase()

Syntax

str.toLowerCase();

Return Value

Returns a copy of the string with all characters converted to lowercase

Example

"SHHH, I'M WHISPERING".toLowerCase();
==> "shhh, i'm whispering"

.indexOf()

Syntax

str.indexOf(searchString[, fromIndex]);

Return Value

Returns the index of the first occurrence of the specified value. Returns -1 if the value isn't found.

Example

"Programming is fun".indexOf("m");
==> 6
"Programming is fun".indexOf("z");
==> -1
"Programming is my favorite thing".indexOf("m", 10);
==> 15 
  //(starts searching left-to-right from index 10)

.lastIndexOf()

Syntax

str.lastIndexOf(searchString[, fromIndex]);

Return Value

Returns the index of the last occurrence of the specified value. Returns -1 if the value isn't found.

Example

"racecar".lastIndexOf("a");
==> 5
"racecar".lastIndexOf("a", 5);
==> 1 
  //(starts at index 5 and searches right-to-left)

.startsWith()

Syntax

str.startsWith(searchString[, fromIndex]);

Return Value

Returns a boolean indicating whether the search string was found

Example

"Programming is the best".startsWith("Pro");
==> true
"Programming is the best".startsWith("pro");
==> false
"Programming is the best".startsWith("Pro", 0);
==> true
"Programming is the best".startsWith("Pro", 1);
==> false 
  //(starts checking from the specified index)

.endsWith()

Syntax

str.endsWith(searchString[, fromIndex]);

Return Value

Returns a boolean indicating whether the search string was found

Example

"Programming is the best".endsWith("best");
==> true

.includes()

Syntax

str.includes(searchString[, fromIndex]);

Return Value

Returns a boolean indicating whether the search string was found

Example

"She sells sea shells".includes("sea");
==> true
"She sells sea shells".includes("sea", 13);
==> false 
// (starts searching left-to-right from index 13)

.slice()

Syntax

str.slice(startIndex[, endIndex]);

Return Value

Returns a new string with the characters from the start index to the end index-1 (not inclusive)

Example

"supercalifragilisticexpialidocious".slice(20);
==> "expialidocious"
"supercalifragilisticexpialidocious".slice(20, 22);
==> "ex"

.substr()

Syntax

str.substr(startIndex[, length]);

Return Value

Returns a new string from the start index of the specified length (if given)

Example

"burrito supreme".substr(5);
==> "to supreme"
"burrito supreme".substr(5, 6);
==> "to sup"

.substring()

Syntax

str.substring(startIndex[, endIndex]);

Return Value

Returns the characters from start index up to but not including the end index (if specified)

Example

"steak tacos".substring(5);
==> " tacos"
"steak tacos".substring(5, 6);
==> " "

.trim(), trimLeft() and .trimRight()

Syntax

str.trim();
str.trimLeft();
str.trimRight();

Return Value

Returns a new string with leading and trailing whitespace removed (just leading whitespace for .trimLeft(), just trailing whitespace for .trimRight())

Example

"     so much space      ".trim();
==> "so much space"
"     so much space      ".trimLeft();
==> "so much space      "
"     so much space      ".trimRight();
==> "     so much space"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment