Skip to main content

TypeScript Strings

String is combinations of alphabets symbols and characters they are make a word. Character is basic unit of string. String is primitive data type.

JavaScript string literals

Strings Methods

Method Overview
charAt() Returns a character from string by given index.
concat() Returns a combination of the two or more specific string.
indexOf() This method returns the first index of the text that is present in the given string. If the text is not present then it return a value of -1.
replace() Replaces the matched substring with a new substring
split() Splits the string into substrings and returns an array
toUpperCase() Converts all the characters of the string into upper case
toLowerCase() Converts all the characters of the string into lower case
charCodeAt() Returns UTF-16 unicode number of given index element value
codePointAt() Returns a nonnegative integer Number that is the code point value of the UTF-16 encoded code point starting at the specified index
includes() Checks whether a string includes another string
endsWith() Checks whether a string ends with another string
LastIndexOf() Returns the index of last occurrence of value in the string
localeCompare() Checks whether a string comes before, after or is the same as the given string
match() Matches a regular expression against the given string
normalize() Returns the Unicode Normalization Form of the given string.
padEnd() Pads the end of the current string with the given string
padStart() Pads the beginning of the current string with given string
repeat() Returns a string consisting of the elements of the object repeated in the given times.
search() Searches for a match between a regular expression and a string
slice() Returns a section of a string
startsWith() Checks whether a string starts with another string
substr() Returns a string beginning at the specified location and of the given characters
substring() Returns a string between the two given indexes
toLocaleLowerCase() Returns a lower case string while respecting current locale
toLocaleUpperCase() Returns an upper case string while respecting current locale
trim() Trims the white space from beginning and end of string
trimLeft() Trims the white space from left side of the string
trimRight() Trims the white space from right side of the string

charAt()

This method are returning a string text character by given index value.

var str = "Hello Programmer";
//Get a character value
console.log(str.charAt(0));
console.log(str.charAt(3));
H
l

concat()

This method are combine two string text values and return a new string.

var str1= "ABC";
var str2="XYZ";

//Combine two text string
var str3=str1.concat(str2);

console.log(str1);
console.log(str2);
console.log(str3);
ABC
XYZ
ABCXYZ

indexOf()

console.log("Text One".indexOf("e"));//return first match index of e
console.log("ABCXYZ".indexOf("YZ"));
console.log("abicxyz".indexOf("i"));
console.log("abcxyz".indexOf("d")); // when char are not exist
1
4
2
-1

replace()

let str = "1010 1010";

// Replace first occurrence
console.log(str.replace("1010","Run"));
Run 1010

split()

let str = "TypeScript Code Example";
let splitText = str.split(" ");
console.log(str); 
console.log(splitText);
TypeScript Code Example
[ 'TypeScript', 'Code', 'Example' ]

toUpperCase()

console.log("Hello Programmer".toUpperCase()); 
HELLO PROGRAMMER

toLowerCase()

console.log("ABCdEf".toLowerCase()); 
abcdef

charCodeAt()

This method are return UTF-16 unicode of given index character value.

codePointAt()

let str = "ABCDEFG";
// return UTF-16 unicode number by index
console.log(str.charCodeAt(0));
console.log(str.charCodeAt(3));
65
68

includes()

console.log("Text One".includes("One"));
console.log("ABCXYZ".includes("D"));
console.log("ABCXYZ".includes("X"));
console.log("abcxyz".includes("X"));
true
false
true
false

endsWith()

console.log("<= || =>".endsWith("=>"));
console.log("<= || =>".endsWith("==>"));
//conside space
console.log("Run this Code ".endsWith("Code"));
console.log("Run this Code".endsWith("Code"));
true
false
false
true

LastIndexOf()

console.log("Text One".lastIndexOf("e")); 
console.log("ZABCXYZ".lastIndexOf("Z"));
console.log("abcxxyz".lastIndexOf("x"));
console.log("abcxyz".lastIndexOf("d")); //when char are not exist
7
6
4
-1

localeCompare()

console.log("Equal".localeCompare("Equal")); //0
console.log("Not Equal".localeCompare("Equal")); //1
console.log("1010".localeCompare("Ten")); //-1
console.log("AB".localeCompare("AB")); //0
0
1
-1
0

match()

let word : string = '2021 is number 1';
// regular expression
var regex = /[0-9]/g;
// Find numbers
var result = word.match(regex);
// Display all numbers
console.log(result);
[ '2', '0', '2', '1', '1' ]

normalize()

let word = " Unicode : \u25CD  \u2A00";
// normalize Unicode
const result = word.normalize('NFC');
console.log(result);
 Unicode : ◍  ⨀

padEnd()

let str = '9';

console.log(str.padEnd(6, '0'));
900000

padStart()

var str = '10';

console.log(str.padStart(6, '0'));
000010

repeat()

var str = "Running ";

console.log(str.repeat(3));
Running Running Running

search()

let str = "This is simple text";

console.log(str.search("simple"));
console.log(str.search("text"));
console.log(str.search("iss"));
8
15
-1

slice()

console.log("Text One".slice(0, 4));
console.log("Text Two".slice(5,8));
Text
Two

startsWith()

let str = 'Text Code';

console.log(str.startsWith("Text"));

console.log(str.startsWith("Code"));

console.log(str.startsWith("text"));
true
false
false

substr()

let str = 'Runing Code';

console.log(str.substr(0,3));
Run

substring()

let str = 'Runing Code';

console.log(str.substring(0,3));
Run

toLocaleLowerCase()

console.log("toLocaleLowerCase".toLocaleLowerCase())
tolocalelowercase

toLocaleUpperCase()

let simple ="AbcdEfg";
let upper_case =simple.toLocaleUpperCase();
console.log(simple);
console.log(upper_case);
AbcdEfg
ABCDEFG




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment