JavaScript Program to Add Prefix and Suffix to a String
Last Updated :
18 Jul, 2024
In this article, we are going to implement a JavaScript program through which we can add a prefix and suffix to a given string. A prefix is added to the beginning of a word to modify its meaning. A suffix is added to the end of a word. Together, they form a prefix-suffix string, enabling the creation of new words or altering the existing word's meaning or grammatical category.
Approach 1: Using String Concatenation
In this approach, we are using string concatenation to combine multiple strings. We took three strings as input: prefix, string, and suffix, and then we returned the concatenated form of these strings.
Syntax:
const stringFormation = (a, b, c) => {
return a + b + c;
};
Example: This example shows the use of the above-explained approach.
JavaScript
const stringFormation = (
inputPrefix,
inputString,
inputSuffix
) => {
return (
inputPrefix +
inputString +
inputSuffix
);
};
let testCase1 = "Geeks";
let testCase2 = " for ";
let testCase3 = "Geeks";
console.log(
stringFormation(
testCase1,
testCase2,
testCase3
)
);
Approach 2: Using Array Spread Operator
In this approach we are using Array Spread Operator. We took three strings as input: prefix, string, and suffix, and then we are forming a string by joining them through join() method.
Syntax
const stringFormation = (a, b, c) => {
return [a, b, c].join("");
};
Example: This example shows the use of the above-explained approach.
JavaScript
const stringFormation = (
inputPrefix,
inputString,
inputSuffix
) => {
return [
inputPrefix,
inputString,
inputSuffix,
].join("");
};
let testCase1 = "Geeks";
let testCase2 = " for ";
let testCase3 = "Geeks";
console.log(
stringFormation(
testCase1,
testCase2,
testCase3
)
);
Approach 4: Using Array Join
In this Approach we use the Array.join() method to concatenate the prefix, string, and suffix into a single string. By passing an array containing these elements to join(''), they are joined together without any separator, forming the desired result.
Example: In this example we defines a function stringFormation that concatenates three input strings. It then tests the function with three test cases and prints the result.
JavaScript
const stringFormation = (
inputPrefix,
inputString,
inputSuffix
) => {
return [inputPrefix, inputString, inputSuffix].join('');
};
let testCase1 = "Geeks";
let testCase2 = " for ";
let testCase3 = "Geeks";
console.log(
stringFormation(
testCase1,
testCase2,
testCase3
)
);
Approach 4: Using Template Literals (Template Strings)
In this approach, we use template literals (template strings) to concatenate the prefix, string, and suffix into a single string. Template literals allow for easy interpolation of variables and expressions within strings.
Syntax:
const stringFormation = (prefix, string, suffix) => {
return `${prefix}${string}${suffix}`;
};
Example:
JavaScript
const stringFormation = (prefix, string, suffix) => {
return `${prefix}${string}${suffix}`;
};
const prefix = "Geeks";
const suffix = "Geeks";
const string = " for ";
console.log(stringFormation(prefix, string, suffix));
Approach 5: Using the concat Method
In this approach, we use the concat method of the String object to concatenate the prefix, string, and suffix into a single string. The concat method provides a straightforward way to combine multiple strings.
Example:
JavaScript
const stringFormation = (prefix, string, suffix) => {
return prefix.concat(string, suffix);
};
console.log(stringFormation("pre", "fix", "suffix"));
console.log(stringFormation("Hello, ", "World", "!"));
console.log(stringFormation("Start-", "Middle", "-End"));
Outputprefixsuffix
Hello, World!
Start-Middle-End
Similar Reads
JavaScript Program to find Lexicographically next String
In this article, we are going to learn how can we find the Lexicographically next string. Lexicographically next string refers to finding the string that follows a given string in a dictionary or alphabetical order.Examples: Input : testOutput : tesuExplanation : The last character 't' is changed to
3 min read
JavaScript - Add a Character to the End of a String
These are the following ways to insert a character at the end of the given string:1. Using ConcatenationWe can use the + operator or template literals to append the character.JavaScriptlet str = "Hello GFG"; let ch = "!"; let res = str + ch; console.log(res); OutputHello GFG! 2. Using Template Liter
2 min read
Javascript Program To Reverse Words In A Given String
Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i"Examples:Â Input: s = "geeks quiz practice code"Â Output: s = "code practice quiz geeks"Input: s = "getting good at coding needs a lot of practice"Â Output: s = "pra
4 min read
JavaScript - Add Characters to a String
Here are the various approaches to add characters to a string in JavaScriptUsing the + Operator - Most PopularThe + operator is the simplest way to add characters to a string. It concatenates one or more strings or characters without modifying the original string.JavaScriptconst s1 = "Hello"; const
2 min read
Javascript Program to Modify a string by performing given shift operations
Given a string S containing lowercase English alphabets, and a matrix shift[][] consisting of pairs of the form{direction, amount}, where the direction can be 0 (for left shift) or 1 (for right shift) and the amount is the number of indices by which the string S is required to be shifted. The task i
3 min read
Javascript Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples:Input : {âgeeksforgeeksâ, âgeeksâ, âgeekâ, âgeezerâ}Output : "gee"Input : {"apple", "ape", "april"}Output : "ap"We start with an example. Suppose there are two strings- âgeeksforgeeksâ and âgeeksâ. What is the longest common prefix in
3 min read
JavaScript - Insert a String at a Specific Index
These are the following methods to insert a string at a specific index in JavaScript:1. Using the slice() MethodInserting a string using the slice() method involves splitting the original string into two parts: one before the insertion point and one after. The new string is then placed between these
3 min read
JavaScript - Add a Character to the Beginning of a String
These are the following ways to insert a character at the beginning of the given string:1. Using String ConcatenationDirectly prepend the character using the + operator or template literals.JavaScriptlet str = "Hello, World!"; let ch = "*"; let res = ch + str; console.log(res); Output*Hello, World!
2 min read
How to add a Character to String in PHP ?
Given two strings, the task is to add a Character to a String in PHP. The character can be added to a string in many ways in this article we will be using eight methods including the Concatenate Method, the String Interpolation Method, the str_pad() Method, Concatenation Assignment (.=) Operator, St
5 min read
Find the Longest Non-Prefix-Suffix Substring in the Given String
Given a string s of length n. The task is to determine the longest substring t such that t is neither the prefix nor the suffix of string s, and that substring must appear as both prefix and suffix of the string s. If no such string exists, print -1. Example: Input: s = "fixprefixsuffix"Output: fix
7 min read