Skip to content

Latest commit

 

History

History
162 lines (128 loc) · 4.47 KB

strings.md

File metadata and controls

162 lines (128 loc) · 4.47 KB

Strings

A string is a sequence of characters used to store text data.

let name = "Rohan";
let city = "Bangalore";

Strings can be enclosed in "", '' or ``

👶 Creating strings

For creating a string, new String("Hello") is rarely used, primitive and simple way is used to define and declare strings.

let message = "Hi";

🔒 String Immutability

Strings are immutable, meaning their characters cannot be changed directly after creation.

let str = "Hello";
str[0] = "Y";

console.log(str); // "Hello"

String methods return new strings instead of modifying the original string.

📞 Accessing Characters

Characters can be accessed using square bracket notation or the at() method. Indexing starts from 0.

let str = "Hello";

console.log(str[0]);   // "H"
console.log(str.at(0));  // "H"
console.log(str.at(-1)); // "o" (last character)

Note

Negative indices work with at(), but not with square bracket notation.

String properties

length property

let str = "javascript is amazing";
console.log(str.length); // returns the length of the string including blank spaces.

⚙️ String methods

  1. ToUppercase() - to convert all characters into uppercase alphabets
console.log("hi".toUpperCase()); // "HI"
  1. toLowerCase() - to convert all characters into lowercase alphabets
console.log("HI".toLowerCase()); // "hi"
  1. trim methods
console.log(" hi ".trim()); // "hi"
console.log(" hi ".trimStart()); // "hi "
console.log(" hi ".trimEnd()); // " hi"
  1. includes() - returns true if the input string is a substring of the string
console.log("Javascript".includes("Java")); // true
console.log("Javascript".includes("Python")); // fasle
  1. startsWitn and endsWith methods
console.log("Javascript".startsWith("Java")); // true
console.log("Javascript".startsWith("Python")); // fasle

console.log("Javascript".endsWith("script")); // true
console.log("Javascript".endsWith("lang")); // fasle
  1. indexOf() and lastIndexOf() - return the first or last index of the substringin the string, or else reutrn -1 if not present
console.log("java".indexOf("a")); // 1
console.log("java".lastIndexof("a")); // 3
  1. slice() - used to extract part of the string
console.log("Javascript".slice(4)); // from the 4th till last index --> "script"
console.log("Javascript".slice(0,4)); // from 0th to 4 -1 = 3rd index --> "Java"
console.log("Javascript".slice(-6)); // last 6 characters --> "script"
  1. substring() - extracts a part of a string using start and end indices. The end index is excluded.
console.log("Javascript".substring(0, 4)); // "Java"
console.log("Javascript".substring(4));    // "script"

Note

Unlike slice(), substring() treats negative indices as 0 and swaps the arguments if the start index is greater than the end index.

// 9. replace() and replaceAll() - used to substitute parts of the string with replacement

console.log("ha ha ha".replace("ha", "hi"));    // "hi ha ha"
console.log("ha ha ha".replaceAll("ha", "hi")); // "hi hi hi"
  1. split(delimiter) - used to convert string into and array
console.log("apples,mangoes,bananas".split(",")); // ["apples","mangoes","bananas"]

// 11. concat() - to attach a string in the end

console.log("Merry".concat(" and ", "James")); // "Merry and James"
  1. repeat() - used to repeat a string for a number of times
console.log("*".repeat(3)); // "***"
  1. charAt() - returns the character at the index
console.log("Javascript".charAt(2)); // "v"

// 14. charCodeAt() - returns the ASCII value of the characte at the index

console.log("A".charCodeAt(0)); // 65
  1. padStart() and padEnd() - pad the string to the spefic length by contcatinating the given character to the start or the end
console.log("5".padStart(3,"0")); // "005"
console.log("5".padEnd(3,"0")); // "500"
  1. match() - to find substrings using regular expression
console.log("abc123".match(/\d+/)); // "123"

// 17. search() - finds the index of the substring

console.log("hello123".search(/\d/)); // 5

Note

String("Hello") returns a primitive string, whereas new String("Hello") creates a String object. Prefer primitive strings.

console.log(typeof String("Hello"));     // "string"
console.log(typeof new String("Hello")); // "object"

previous

next