JavaScript methods are built-in functions that you can call and use to manipulate strings for different JavaScript projects. Additionally, these methods can help you change the results of certain string values; they save you the time you would have spent writing codes to change or alter these strings.
In this article, we’ll look at a few of these javascript string methods and some of their applications. Dive in!
1. String length()
You can use the string length function to sum the number of characters in an initialized string, then display the int value:
let schName1 = "Tel-aviv University";
let length1 = schName.length;
2. String slice()
Slice() is a string function that allows you to take a portion of a string and return it as a new string. Also, the method requires two parameters: where it starts the extraction and where it stops.
//We will be using slice to cut and display text from the variable.
let rym = "sugar can be sweeter than honey when you add Milk and Choco";
// Here, splice will copy words after the first two words, then copy down to 19. White spaces are counted too.
let t1 = rym.slice(2, 19);
console.log(t1)
3. String charAt()
You can locate the character in a string at a particular index value using JavaScript’s charAt function. Additionally, in JavaScript, the function charAt returns a string with a single character, and the index value can either be a single number or a string of numbers. Also, if an index value is not specified, the charAt function in JavaScript returns the first character.
// initialize the variable.
let strExample = "Missionary";
// using charAt to pick out the value for the parameter entered.
let firstIndexValue= strExample.charAt(1);\
console.log(firstIndexValue); // output is 1
4. String split()
You can divide a string into an array of substrings using the split() method. Additionally, Split() does not change the original string; instead, it creates a new array as its output. However, the split function creates a list of strings by splitting a string based on the separator the user supplies.
// declare the variable and assign values.
let tst = "what is happening over there?";
// the code below will use " " to separate the variable in an array.
const myAry = tst.split(" ");
console.log(myAry);
// the code below will use " " to separate the variable in an array.
let vowels1 = "a,e,i,o,u";
const myAry1 = vowels1.split(",");
console.log(myAry1);
5. String trim()
You can use the trim() method if you don’t want to deal with the stress of manually eliminating white spaces from your code. Also, you can eliminate whitespace characters from the beginning and end of a string by using the string method trim().
// The trim cuts off the white space seen in this variable below.
let ex1 = " Jump up to see the sea! ";
let ex2 = ex1.trim();
console.log(ex2)
6. String replace()
A value or regular expression is looked up in a string using the replace() method. It replaces the value(s) and the strings with new strings.
let dTxt = "Please visit Us ........wait while the website address loads \n";
// original text display before the replace() was called.
console.log(dTxt)
let newText = dTxt.replace("wait while the website address loads", "WWW.NitrolexTechWriter.com");
// original text display after the replace() was called.
console.log(newText)
7. String replaceAll()
One way to replace every instance of a string or regex is to use Javascript’s All() function. Furthermore, two parameters are accepted by the replaceAll() method: pattern and replacement; the pattern can be either a string or a regular expression.
// initialization
const my_strng1 = "I love donkeys more when they are in motion!";
let my_pattern = "donkeys";
let my_replacement = "horses";
// use replace to replace donkey with horses.
let new_data = my_strng1.replaceAll(my_pattern,my_replacement);
console.log(new_data)
8. String toUpperCase()
Any letter that is identified as being in lowercase will be converted to uppercase by the toUpperCase() function. Additionally, this method completes the operation without altering any characters because JavaScript strings are immutable.
let text1 = "Hello World!";
let text2 = text1.toUpperCase();
console.log(text2);
The specified string is transformed into a new one that only contains uppercase letters. Hence, there will now be two strings: the initial one and the most recent capitalization of it.
9. String toLowerCase()
When you use this function, any letter that the toLowerCase() function determines to be in uppercase is converted to lowercase. Additionally, this method operates without changing any characters, just like when changing lowercase to uppercase, because JavaScript strings are immutable.
let myG = 'Hey there!';
console.log(myG.toLowerCase());
Again, only one capital letter in the string “myG” remains after it is changed to lowercase.
Hence, the toLowerCase() method only affects uppercase letters; lowercase letters are unaffected. These letters preserve their original form.
10. String concat()
You can combine strings by using the JavaScript string method concat(). Furthermore, the concat() method achieves this by concatenating one or more string values with the calling string and returning the result as a new string.
var fir_strng = 'Bio';
console.log(fir_strng.concat('chemicals','is not ','harmful'));
Conclusion
You discovered in this article what JavaScript string methods are—one of the crucial elements you’ll need to create intricate programs.