How to get substring in TypeScript?


The string contains various characters, and the substring is the part of the string. We can get the substring from the string two ways. The first is using the starting and ending position of the substring, and the second is using the starting position and length of the substring.

We will learn both ways to get the substring from the string in TypeScript in this tutorial.

Get The Substring Using The Start Position and End Position

In TypeScript, indexing of string starts from zero. So, if we have a zero-based starting and ending position of the substring, we can get the substring from the particular string.

Syntax

Users can follow the syntax below to get the substring from the particular string using the start and end positions.

get_sub_string(start, end): string {
   let resultantString = "";
   for (let i = 0; i < this.string.length; i++) {
      if (i >= start && i <= end) {
         resultantString += this.string[i];
      }
   }
   return resultantString;
}

Algorithm

  • Step 1 − Define the get_sub_string() method, which takes the starting and ending index of the substring and returns the string.

  • Step 2 − Create the resultantString variable of type string to store the substring.

  • Step 3 − Use the for loop to iterate through the string, which we need to get the substring.

  • Step 4 − Inside the for loop, check if the index is between the including start and end, then append that position's character to the resultantString variable.

  • Step 5 − Once the iteration of for loop completes, return the resultantString, which is the required substring.

Example

In the example below, we have created the class stringClass, which contains the string member. Also, we have implemented the get_sub_string() method according to the above syntax and algorithm.

After that, we have created the object of the stringClass and invoked the get_sub_sting() method by passing the various start and end positions as a parameter.

// Creating a class named stringClass
class stringClass {
   // string memeber
   string: string;
   
   // constructor to initialize the string member
   constructor(str: string) {
      this.string = str;
   }
   // method to get substring, which takes the start and end property
   get_sub_string(start: number, end: number): string {
      // defining the resultantString variable to store substring
      let resultantString = "";
      for (let i = 0; i < this.string.length; i++) {
         // if index is between start, and end, include the character of that position to resultantString
         if (i >= start && i <= end) {
            resultantString += this.string[i];
         }
      }
      // return the substring
      return resultantString;
   }
}
let str1: stringClass = new stringClass("TutorialsPoint");
console.log(
   "The substring of TutorialsPoint starting from 2 to 5 is " +
str1.get_sub_string(2, 5)
);
console.log(
   "The substring of TutorialsPoint starting from 0 to 7 is " +
str1.get_sub_string(0, 7)
);

On compiling, it will generate the following JavaScript code −

// Creating a class named stringClass
var stringClass = /** @class */ (function () {
   // constructor to initialize the string member
   function stringClass(str) {
      this.string = str;
   }
   // method to get substring, which takes the start and end property
   stringClass.prototype.get_sub_string = function (start, end) {
      // defining the resultantString variable to store substring
      var resultantString = "";
      for (var i = 0; i < this.string.length; i++) {
         // if index is between start, and end, include the character of that position to resultantString
         if (i >= start && i <= end) {
            resultantString += this.string[i];
         }
      }
      // return the substring
      return resultantString;
   };
   return stringClass;
}());
var str1 = new stringClass("TutorialsPoint");
console.log("The substring of TutorialsPoint starting from 2 to 5 is " +
   str1.get_sub_string(2, 5));
console.log("The substring of TutorialsPoint starting from 0 to 7 is " +
   str1.get_sub_string(0, 7));

Output

The above code will produce the following output −

The substring of TutorialsPoint starting from 2 to 5 is tori
The substring of TutorialsPoint starting from 0 to 7 is Tutorial

Using the substring() Method to get substring

Rather than writing a function from scratch to get a substring using the start and end positions, we can use the substring() method of the string class in TypeScript. It also works as in the above example and returns the substring.

Syntax

Users can follow the syntax below to use the substring() method in TypeScript.

let str: string = "Hello";
let subString: string = str.substring(start,[end]);

Parameters

  • start − It is a required parameter representing the starting position from where we need to get the substring.

  • end − It is an optional parameter until we get the substring. It excludes the character from the end position.

Return Value

In this example, we have defined the sampleString named string variable. Also, we used the substring() method by taking the samleString as a reference. Furthermore, we have passed the different start and end parameter values for the substring() method, and users can observe the output.

Example

let sampleString: string = "Welcome";
let subString: string = sampleString.substring(0, 4);
console.log("The substring from 0 to 4 is " + subString);
// using substring() method without passing end parameter
subString = sampleString.substring(2);
console.log("The substring from 2 to end is " + subString);
subString = sampleString.substring(-2, 5);
console.log("The substring from -2 to 5 is " + subString);
subString = sampleString.substring(-2, -5);
console.log("The substring from -2 to -5 is " + subString);
subString = sampleString.substring(-5, -2);
console.log("The substring from -5 to -2 is " + subString);

On compiling, it will generate the following JavaScript code −

var sampleString = "Welcome";
var subString = sampleString.substring(0, 4);
console.log("The substring from 0 to 4 is " + subString);
// using substring() method without passing end parameter
subString = sampleString.substring(2);
console.log("The substring from 2 to end is " + subString);
subString = sampleString.substring(-2, 5);
console.log("The substring from -2 to 5 is " + subString);
subString = sampleString.substring(-2, -5);
console.log("The substring from -2 to -5 is " + subString);
subString = sampleString.substring(-5, -2);
console.log("The substring from -5 to -2 is " + subString);

Output

The above code will produce the following output −

The substring from 0 to 4 is Welc
The substring from 2 to end is lcome
The substring from -2 to 5 is Welco
The substring from -2 to -5 is
The substring from -5 to -2 is

In the above output, users can observe that if we pass both negative values as a parameter, the substring() method returns an empty string. If we pass only the start negative value, it starts substring from the 0th index. Also, if we don’t pass the end parameter, it returns the substring from the start to the length of the string.

Using The substr() Method to get substring

In TypeScript, the substr() method also returns the substring from the particular string and is almost the same as the substring() method of the string class. The basic difference between the substr() and substring() method is the value they take as a parameter. The substring() takes the end position as a second parameter, and the substr() takes the length of the substring as a second parameter.

Syntax

In the syntax below, we have used the substr() method.

let str2: string = "Hi there!";
let substring: stirng = str2.substr(start, [len])

Parameters

  • start − It is a zero-based starting position of the substring.

  • len − It is an optional parameter referring to the length of the substring.

Return Value

The substr() method returns the substring starting from the start and length of len. If we don’t pass the len parameter, it returns the substring from start to end.

Example

We have used the substr() method with different parameter values in this example. Users can observe the output and how it returns the substring with different start and len parameter values.

let demoStr: string = "Shubham";
let substring: string = demoStr.substr(1,3);
console.log("The substring of length 3 starting from 1st index is " + substring);
// substr() method without an optional parameter
substring = demoStr.substr(3);
console.log("The substring starting from 3rd index is " + substring);

On compiling, it will generate the following JavaScript code −

var demoStr = "Shubham";
var substring = demoStr.substr(1, 3);
console.log("The substring of length 3 starting from 1st index is " + substring);
// substr() method without an optional parameter
substring = demoStr.substr(3);
console.log("The substring starting from 3rd index is " + substring);

Output

The above code will produce the following output −

The substring of length 3 starting from 1st index is hub
The substring starting from 3rd index is bham

Updated on: 03-Jan-2023

373 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements