String Methods in TypeScript

Kyaw Thu
2 min readOct 9, 2023

--

Photo by Ales Nesetril on Unsplash

TypeScript provides a variety of string methods that allow you to manipulate and work with strings in different ways. Here is a list of commonly used string methods in TypeScript along with explanations for each:

charAt(index: number): string

  • Returns the character at the specified index in the string.
  • For example, "Hello".charAt(1) returns "e".

charCodeAt(index: number): string

  • Returns the Unicode value (integer) of the character at the specified index.
  • For example, "Hello".charCodeAt(1) returns 101 (the Unicode value for "e").

concat(...strings: string[]): string

  • Combines two or more strings and returns a new string.
  • For example, "Hello".concat(", World!") returns "Hello, World!".

indexOf(searchString: string, fromIndex?: number): number

  • Returns the index of the first occurrence of a specified substring. Returns -1 if not found.
  • For example, "Hello, world!".indexOf("world") returns 7.

lastIndexOf(searchString: string, fromIndex?: number): number

  • Returns the index of the last occurrence of a specified substring. Returns -1 if not found.
  • For example, "Hello, world, world!".lastIndexOf("world") returns 13.

substring(start: number, end?: number): string

  • Returns a substring from the specified start index to the end index (optional).
  • For example, "Hello, world".substring(7) returns "world".

slice(strat: number, end?: number): string

  • Returns a substring from the specified start index to the end index (optional).
  • Similar to substring, but also works with negative indices.
  • For example, "Hello, world".slice(-5) returns "world".

startsWith(searchString: string, position?: number): boolean

  • Checks if a string starts with the specified substring. Returns true or false.
  • For example, "Hello, world".startsWith("Hello") returns true.

endsWith(searchString: string, position?: number): boolean

  • Checks if a string ends with the specified substring. Returns true or false.
  • For example, "Hello, world".endsWith("world") returns true.

toLowerCase(): string

  • Converts the string to lowercase.
  • For example, "HeLlO, wOrLd".toLowerCase() returns "hello, world".

toUpperCase(): string

  • Converts the string to uppercase.
  • For example, "hello, world".toUpperCase() returns "HELLO, WORLD".

trim(): string

  • Removes whitespace from the beginning and end of the string.
  • For example, " Hello, world ".trim() returns "Hello, world".

replace(searchValue: string | RegExp, replaceValue: string): string

  • Replaces occurrences of searchValue with replaceValue.
  • For example, "Hello, world".replace("world", "there") returns "Hello, there".

split(separator: string | RegExp, limit?: number): string[]

  • Splits the string into an array of substrings using a specified separator.
  • For example, "apple,banana,cherry".split(",") returns ["apple", "banana", "cherry"].

If you want to learn more about TypeScript, please refer to the official documentation.

If you want to read a blog about regular expressions versus string methods, you can read it here. 👇

Regular Expressions versus String Methods | Medium

Thank you so much for reading this post, and I’d love to hear your thoughts! Feel free to share your experiences in the comments below.

If you like the post and want to support me, you can buy me a coffee.

--

--

Kyaw Thu

I am a Frontend Developer. I write blogs and tutorials about web development, especially about animating React components using Framer Motion and Tailwind CSS.