Function to decode a string in JavaScript


In the given problem statement we are asked to implement a function to decode a string with the help of javascript functionalities. To solve these kinds of problems we can use built−in functions of Javascript. These functions usually take encoded data as input and give output as decoded strings.

What are built−in methods to decode strings in Javascript?

Let's understand techniques to decode strings in javascript.

In javascript, if we need to decode a string we can use different built−in methods. Some methods are: decodeURIComponent(), unescape() and atob(). We will see the working of all the methods in detail below.

Logic for The Above Problem

The problem statement is saying that we have to decode a given string with the help of Javascript. So first we should know what is encoding and decoding of strings.

Encoding and decoding are the mechanism to transform a data or string from one form to another form. This mechanism is basically used to transfer data or to store data from one device to another device.

Encoding is the process of converting data from its original form into a format which can not be readable and can be transmitted or stored in a specific way. While decoding is the opposite process of encoding, where the encoded information is transformed into its original form.

In Javascript, there are many built−in functions available to encode and decode any given strings. For example encodeURIComponent() and decodeURIComponent() methods are used for URL encoding and decoding. With the usage of these kinds of functions we can ensure that our data is properly encoded and decoded according to the appropriate standards.

Algorithm with decodeURIComponent() method

The decodeURIComponent method is a built−in method of Javascript which takes a URL encoded string as its parameter and returns a decoded string. So let’s see the working of this function.

Step 1 − Define a function decodeStr() and pass an argument which is an encoded string.

Step 2 − Inside the function we will use javascript’s built−in method called decodeURIComponent and pass the encoded string in it.

Step 3 − Now define a string variable and assign a value as an encoded string in it and call the above function to get the output as a decoded string.

Example 

//function to get decoded string
function decodeStr(encodedStr) {
  return decodeURIComponent(encodedStr);
}
//define encoded string
const encodedStr = "Hello%20World%21";
const decodedStr = decodeStr(encodedStr);
console.log("Decoded string:");
console.log(decodedStr);

Output

Decoded string:
Hello World!

Algorithm with unescape() method

The unescape() method is a built−in function of Javascript which takes an encoded string as its parameter and returns a decoded string. So let’s see the working of this function.

Step 1 − Define a function decodeStr() and pass an argument which is an encoded string.

Step 2 − Inside the function we will use javascript’s built−in method called unescape and pass the encoded string in it.

Step 3 − Now define a string variable and assign a value as an encoded string in it and call the above function to get the output as a decoded string.

Example

//function to get decoded string
function decodeStr(encodedStr) {
  return unescape(encodedStr);
}

//define encoded string here
const encodedStr = "Hello%20Tutorialspoint%21";
const decodedStr = decodeStr(encodedStr);
console.log("Decode string is as follows:");
console.log(decodedStr);

Output

Decode string is as follows:
Hello Tutorialspoint!

Algorithm with replace() and regular expression

In this algorithm we will use replace() method and regular expression to decode the encoded message in javascript. So let’s see the working of this function.

Step 1 − Define a function decodeStr() and pass an argument which is an encoded string.

Step 2 − Inside the function a regular expression is defined and it matches any percent−encoded sequence in the string and the callback function passed to replace() method which converts each sequence to its corresponding character using String.fromCharCode() and parseInt().

Step 3 − Now define a string variable and assign a value as an encoded string in it and call the above function to get the output as a decoded string.

Example 

//function to get decoded string
function decodeStr(encodedStr) {
  const decodedStr = encodedStr.replace(/%([0-9A-Fa-f]{2})/g, (match, p1) => String.fromCharCode(parseInt(p1, 16)));
  return decodedStr;
}

//define encoded string here
const encodedStr = "Hello%20Javascript%21";
const decodedStr = decodeStr(encodedStr);
console.log("Decoded string is as follows:")
console.log(decodedStr);

Output

Decoded string is as follows:
Hello Javascript!

Complexity

The time complexity and space complexities of the different kinds of methods for decoding a given string can vary depending upon the size of the given string. For decodeURIComponent() and unescape() functions have similar time and space complexities, O(n). In this n is the size of the input string. Because the defined function iterates over each character in the string.

Conclusion

In this code we have implemented a function using decodeURIComponent and unescape javascript’s built−in functions.With the help of these functions we can ensure that our data or string is properly encoded and decoded. And lastly we have seen time and space complexities O(n) for both the methods.

Updated on: 23-Aug-2023

608 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements