Javascript Module 6
Javascript Module 6
String Operations:
String operations involve manipulating or working with strings. This includes tasks such as
concatenation, splitting, extracting substrings, replacing characters, converting case, and more.
Code :
let str1 = "Hello";
let str2 = "World";
let combinedStr = str1 + " " + str2; // String concatenation
console.log(combinedStr); // Output: Hello World
String Methods:
JavaScript provides numerous built-in methods for performing operations on strings. These methods
offer functionalities like searching within strings, extracting parts of strings, converting cases, trimming
whitespace, and more.
Code :
let sentence = "JavaScript is awesome";
console.log(sentence.length); // Length of string
console.log(sentence.toUpperCase()); // Convert to uppercase
console.log(sentence.includes("awesome")); // Check substring presence
console.log(sentence.split(" ")); // Split string into array by space
Code:
let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName; // String concatenation
console.log(fullName); // Output: John Doe
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
1. Reverse a String
Question: Write a function to reverse a given string.
Code :
function reverseString(str) {
return str.split("").reverse().join("");
}
2. Check Palindrome
Question: Write a function to determine if a string is a palindrome (reads the same forwards
and backwards).
Code :
function isPalindrome(str) {
const reversed = str.split("").reverse().join("");
return str === reversed;
}
3. Count Vowels
Question: Write a function to count the number of vowels in a string.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
function countVowels(str) {
const vowels = 'aeiou';
let count = 0;
for (let char of str.toLowerCase()) {
if (vowels.includes(char)) {
count++;
}
}
return count;
}
4. Capitalize Words
Question: Write a function to capitalize the first letter of each word in a sentence.
Code :
function capitalizeWords(sentence) {
return sentence.split(" ").map(word => word.charAt(0).toUpperCase()
+ word.slice(1)).join(" ");
}
5. Check Anagrams
Question: Write a function to check if two strings are anagrams of each other.
Code :
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
function longestWord(sentence) {
const words = sentence.split(" ");
let longest = words[0];
for (let word of words) {
if (word.length > longest.length) {
longest = word;
}
}
return longest;
}
7. Remove Duplicates
Question: Write a function to remove duplicate characters from a string.
Code :
function removeDuplicates(str) {
let result = "";
for (let char of str) {
if (result.indexOf(char) === -1) {
result += char;
}
}
return result;
}
8. Count Characters
Question: Write a function to count the occurrences of each character in a string.
Code:
function countCharacters(str) {
const charCount = {};
for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
}
return charCount;
}
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
9. Replace Spaces
Question: Write a function to replace all spaces in a string with '%20'.
Code :
function replaceSpaces(str) {
return str.replace(/\s/g, '%20');
}
Code :
function titleCase(sentence) {
return sentence.toLowerCase().replace(/(^|\s)\w/g, (match) =>
match.toUpperCase());
}
Code :
function hasSubstring(str, substr) {
return str.includes(substr);
}
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
function removeWhitespace(str) {
return str.replace(/\s/g, "");
}
Code :
function isSubsequence(sub, str) {
let subIndex = 0;
for (let char of str) {
if (char === sub[subIndex]) {
subIndex++;
}
if (subIndex === sub.length) {
return true;
}
}
return false;
}
Code :
function firstNonRepeatingChar(str) {
const charCount = {};
for (let char of str) {
charCount[char] = (charCount[char] || 0) + 1;
}
for (let char of str) {
if (charCount[char] === 1) {
return char;
}
}
return null;
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
Code :
function isRotation(str1, str2) {
if (str1.length !== str2.length) {
return false;
}
const combinedStr = str1 + str1;
return combinedStr.includes(str2);
}
Code :
function countWords(sentence) {
return sentence.split(" ").filter(word => word !== "").length;
}
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]
Software Training and Placement Center
These examples demonstrate how strings are used and manipulated in JavaScript, along with their real-
time applications in various scenarios. Strings are a fundamental part of programming and play a crucial
role in web development and application building.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : [email protected]