JavaScript Tutorial
  • 🍼Introduction
  • 🥛Hello Gamers
  • 🍯Variables
    • Action Game Example
  • 🌰Strings
    • RPG Example
  • 🍪Character Acces
    • Simulation Example
  • 🍩Plus (+) operator
    • Strategy Game Example
  • 🥜Functions
    • Adventure Game Example
  • 🍿Template strings
    • Strategy Game Example
  • 🍫Numbers
    • Sports Game Example
  • 🍬Converting Numbers to Strings
    • Fighting Game Example
  • 🍭Operations
    • FPS Game Example
  • 🍮Conditions
    • Platformer Game Example
  • 🎂Arrays
    • Puzzle Game Example
  • 🍰Objects
    • Racing Game Example
  • 🧁Arrow Functions
    • Rhythm Game Example
  • 🔌Shameless Plug
  • 🥧Implicit Return
    • Platform Game Example
  • 🍦String Methods
    • GRID Game Example
  • 🍨Object Literals
    • Board Game Example
  • 🍧DynamicProperty
    • Political Game Example
  • 🍡Reading Dynamic Property
    • Hide & Seek Game
  • 🥮Objects Continued
    • Medieval Game Example
  • 🥠Object Shorthands
    • Trivia Game example
  • 🔌Shameless Plug (again)
  • 🍥Destructuring & Concatenation
    • Dig a hole game example
  • 🍏Optional Chaining
    • Action Game Example
  • 🍐Nullish coalescing operator
    • Adventure Game Example
  • 🍊Refactoring If Conditions
    • Arcade Game Example
  • 🍋Implicit Conversion & Falcy Values
    • Strategy Game Example
  • 🍌Arrays of objects
    • Sports Game Example
  • 🍉Transforming Array of Objects
    • Fighters Game Example
  • 🍇Some important array methods
    • FPS Example
  • 🫐Reducing arrays of objects
    • Third Person Shooter Example
  • 🍓Try....catch
    • Platformer Example
  • 🍈Immutability
    • Puzzle game example
  • 🍒Creating new arrays or objects
    • Racing Game Example
  • 🔌Shameless Plug (again)
  • 🍑Immutable object operations
    • Basics
    • Rhythm Game Example
  • 🥭Classes
    • Basics
    • Platformer Game Example
  • 🍍Instance Method
    • Basics
    • Role-playing game example
  • 🥥Object Oriented Programming
    • Basics
    • Stealth Game Example
  • 🥝Getters & Setters
    • Basics
    • Survival Game Example
  • 🍅Static Methods & Chaining
    • Basics
    • Tactical Role Playing Game
  • 🍆Class Inheritance
    • Tower Defense Game Example
  • 🥑Use of Super
    • Vehicular Combat Game example
  • 🫒Public Class Field
    • MMORPG Example
  • 🥦Private class fields
    • Battle Royale Games Example
  • 🥬Aysnc/Await
    • Collectible Card Game example
    • Video Board game example
  • 🫑SetTimeout
    • Dice game example
  • 🥒Execution Order
    • Digital Collectible Card Game example
  • 🌶️The callback pattern
    • Life Simulation Game example
  • 🌽Promises
    • Music Game Example
  • 🥕Writing a function that returns a promise
    • Party game example
  • 🧄Introduction to JSON
    • Tile based game example
  • 🧅JSON.parse(string) / JSON.stringify(object)
    • Rhythm action game example
  • 🍠JSON Example for a game
    • Fetching JSON game data
  • 🥐Handling fetch errors
    • Fetch and HTTP Requests
  • 🥯ES Modules
    • Tower Defence Game Example
  • 🍞Default Export
    • Strategy Adventure Game Example
  • 🥖Fetch Wrapper
    • Strategy Role-playing game example
  • 🔌Final Shameless Plug
  • 🫓Lexical Scope and Arrow Functions
    • Vehicular Combat Game Example
  • 🥨Passing Functions
    • Vehicular simulation game example
  • 🥚Dynamic Imports
    • Video card game example
  • 🍠What is a closure
    • Video casino game example
  • 🎆Congratulations
Powered by GitBook
On this page

String Methods

In JavaScript, strings are objects that have built-in methods that can be used to manipulate and analyze the string. Here are some of the most commonly used string methods:

length: returns the number of characters in a string.

let myString = "Hello World";
console.log(myString.length); // Output: 11

charAt(index): returns the character at a specified index in a string. The first character has an index of 0.

let myString = "Hello World";
console.log(myString.charAt(0)); // Output: "H"
console.log(myString.charAt(5)); // Output: " "

indexOf(searchValue, [start]): returns the index of the first occurrence of a specified value in a string. If the value is not found, it returns -1. The optional start parameter specifies the position to start the search from.

let myString = "Hello World";
console.log(myString.indexOf("o")); // Output: 4
console.log(myString.indexOf("o", 5)); // Output: 7

lastIndexOf(searchValue, [start]): returns the index of the last occurrence of a specified value in a string. If the value is not found, it returns -1. The optional start parameter specifies the position to start the search from, searching backwards.

let myString = "Hello World";
console.log(myString.lastIndexOf("o")); // Output: 7
console.log(myString.lastIndexOf("o", 6)); // Output: 4

slice(start, [end]): returns a portion of a string as a new string. The start parameter specifies the index of the first character to include, and the optional end parameter specifies the index of the last character to include.

let myString = "Hello World";
console.log(myString.slice(0, 5)); // Output: "Hello"
console.log(myString.slice(6)); // Output: "World"

substring(start, [end]): returns a portion of a string as a new string. It's similar to slice but it can't accept negative values as input.

let myString = "Hello World";
console.log(myString.substring(0, 5)); // Output: "Hello"
console.log(myString.substring(6)); // Output: "World"

substr(start, [length]): returns a portion of a string as a new string. The start parameter specifies the index of the first character to include, and the optional length parameter specifies the number of characters to include.

let myString = "Hello World";
console.log(myString.substr(0, 5)); // Output: "Hello"
console.log(myString.substr(6, 4)); // Output: "Worl"

replace(searchValue, replaceValue): replaces the first occurrence of a specified value with another value.

let myString = "Hello World";
console.log(myString.replace("World", "Friend")); // Output: "Hello Friend"
PreviousPlatform Game ExampleNextGRID Game Example

Last updated 2 years ago

🍦