Convert a string to a new string
A simple one!
There are many ways to convert a string to a new string, depending on what you want to do with the original string. Here are a few examples:
- Uppercase: Convert a string to uppercase using the
toUpperCase()
method:const originalString = "hello"; const newString = originalString.toUpperCase(); // "HELLO"
- Lowercase: Convert a string to lowercase using the
toLowerCase()
method:const originalString = "HELLO"; const newString = originalString.toLowerCase(); // "hello"
- Reverse: Reverse the order of characters in a string using the
split()
andjoin()
methods:const originalString = "hello"; const newString = originalString.split("").reverse().join(""); // "olleh"
- Trim: Remove whitespace from the beginning and end of a string using the
trim()
method:const originalString = " hello "; const newString = originalString.trim(); // "hello"
- Replace: Replace a substring with another string using the
replace()
method:const originalString = "hello world"; const newString = originalString.replace("world", "universe"); // "hello universe"
- Substring: Extract a portion of a string using the
substring()
method:const originalString = "hello world"; const newString = originalString.substring(6); // "world"
- Concatenate: Combine two strings using the
+
operator:const originalString1 = "hello"; const originalString2 = " world"; const newString = originalString1 + originalString2; // "hello world"
These are just a few examples of how you can convert a string to a new string. There are many more methods and techniques available in JavaScript!