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 - 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 - 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
JavaScript String Methods JavaScript strings are the sequence of characters. They are treated as Primitive data types. In JavaScript, strings are automatically converted to string objects when using string methods on them. This process is called auto-boxing. The following are methods that we can call on strings.slice() extra
11 min read
How to create half of the string in uppercase and the other half in lowercase? The first thing that we have to keep in my mind while solving this kind of problem is that the strings are immutable i.e, If I am taking the following string var string1 = "geeksforgeeks";string1[0] = "G";console.log(string1);As strings are immutable, so we cannot change the character of the string,
9 min read
Suffix Automation In computer science, a suffix automaton is an efficient data structure for representing the substring index of a given string which allows the storage, processing, and retrieval of compressed information about all its substrings. In this article, we will delve into the concept of suffix automation,
13 min read
JSTL fn:substringAfter() Function In the main string, if we want to return the subset of the string followed by a specific substring then we can use the JSTL fn:substringAfter() Function. JSTL substringAfter() FunctionThis function mainly returns the portion of the string that comes after the given string value. In this article, we
2 min read
JSTL fn:substring() Function In JSTL, the fn:substring() function is used to extract or retrieve the part of a given input or specified string, by starting from the start index to the end index [optional]. This function is mainly used for string manipulation tasks by many developers. In this article we will explore the Syntax a
2 min read
JSTL fn:startsWith() Function In JSTL the fn:startsWith() function is used to check if a given string starts with a particular prefix. This function returns the boolean value as True or False according to the check result. If the input string begins with the prefix then true is been returned else false is been returned. In this
2 min read