Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this blog, we will learn about String Functions in javascript. We will see different methods that are present in the Strings. We will see how Strings are created in javascript. We will look at how these methods and functions are used in the code.
Strings are sequences of characters that are used for holding the data in the text form. In general, strings don’t have methods and functions, but in javascript, strings are treated as objects, so strings have methods and functions in javascript. We can manipulate the Strings according to their needs.
Create Strings in JavaScript
There are two ways in which strings are created in javascript.
1. Using string literal
2. Using string objects i.e using a new keyword
1. ByUsing String Literal
In string literal, we try to use double inverted commas to create a new string.
Syntax:
var msg=”I am string literal”;
Example:
<script>
var msg="This is how we use string literal";
document.write(str);
</script>
Output
This is how we use string literal
2. By Using String Object (New Keyword)
To create a new string object, we use new keyword.
Syntax:
var stringName=new String("New String is created");
Let’s see this with the example.
Example:
<script>
var stringName=new String("New String is created");
document.write(stringName);
</script>
<script>
let msg = ”String Functions in javascript”;
let length= msg.length;
document.write(length);
</script>
Output
30
2. JavaScript String ChartAt Method
This method returns the character present at that index. Indexing in strings also starts with 0.
Example:
<script>
var str=”String Functions in javascript”;
document.write(str.charAt(2));
</script>
Output
r
3. JavaScript String IndexOf() Method
This returns the index position of the given input from the string.
Example:
<script>
var msg="Learn String Functions in javascript";
var n=msg.indexOf("String");
document.write(n);
</script>
Output
7
4. JavaScript String Concat() Method
It combines two or more strings and returns the new string.
Example:
<script>
var x="Coding ";
var y="Ninjas";
document.writeln(x.concat(y));
</script>
Output
Coding Ninjas
5. JavaScript String CharCodeAt() Method
It returns the Unicode value of the character present at the mentioned index.
Example:
<script>
var x="String Functions in javascript";
document.writeln(x.charCodeAt(3));
</script>
Output
105
6. JavaScript String Includes() Method
It returns true if the searched string is included in the given string.
Example:
let sentence = 'Coding Ninjas.';
let word = 'Ninjas';
console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`);
Output
True
7. JavaScript String LastIndex() Method
It searches for the last occurrence of the word in the string. It returns the index of the last occurrence of the word. It returns -1 in case the string is not found.
Example:
<script>
let msg=”Hello, hi, Hello”;
let search= msg.lastIndexOf(“Hello”);
document.write(search);
</script>
Output
12
8. JavaScript String Replace() Method
It searches for the given text to be replaced in the given string. It takes two parameters. The string to be replaced, the second is a new string to be added in the replacement of the first one. In return, it returns the new string with the string that is replaced.
Example:
<script>
let msg=”Hello, hi, Hello”;
let newString= msg.replace(‘Hi’,’Hey’);
document.write(newString);
</script
Output
Hello, Hey, Hello
9. JavaScript String Match() Method
It takes one argument as a parameter, returns the string if found, and in case the string does not match, it returns null. If the given value is in the string, it converts it to a regular expression.
When the match method has a string as an argument
Example:
<script>
var x="Coding Ninjas ";
var y= x.match(“Ninjas”);
document.write(y);
</script>
Output
Ninjas
Using regular expression
Example:
<script>
var x="Coding Ninjas ";
var y= x.match(/Ninjas/);
document.write(y);
</script>
Output
Ninjas
10. JavaScript String EndsWith() Method
It matches the given string ends with the specified sequence of characters given in the parameter. It is a case-sensitive method. It returns true or false as output.
Example:
<script>
var x=’Coding Ninjas’;
var y= x.endsWith(‘njas’);
document.write(y);
</script>
Output
True
11. JavaScript String Slice() Method
This method extracts a part of the string and returns the extracted part to a new string. In the case of an array, it returns a new array. It has different methods depending upon the parameters.
slice()
slice(start)
slice(start,end)
slice()
If no value is given in the parameters, it starts from 0.
Example:
If no value is given in the parameters, it starts from 0.
<script>
let message=’String functions in javascript.’;
let result=message.slice();
document.write(result);
</script>
Output
String functions in javascript.
slice(start)
It starts from the start parameter till the end, and returns the new string.
Example:
<script>
let message='I am studying String functions in javascript.’;
let result=message.slice(14);
document.write(result);
</script>
Output
String functions in javascript
slice(start,end)
This method of slice takes two parameters the start and the end. It returns the string or new array without including the end character.
It can have two parameters. The start and the length. The length parameter is optional. It returns the new string from the start to the length. The method does not make any changes in the original string.
Example:
<script>
let message=’I am studying String functions in javascript.’;
let result=message.substr(13);
document.write(result);
</script>
Output
String functions in javascript.
13. JavaScript String Substring() Method
It returns the substring of the original string. It does not make any changes in the original string. In case the start is greater than the end, it automatically swaps both values.
Example:
<script>
let message=’I study from Coding Ninjas.’;
let result=message.substring(14,28);
document.write(result);
</script>
Output
Coding Ninjas
14. JavaScript String toLowerCase() Method
It convert the string to lowercase characters. This method does not make any changes in the original string. Returns the string in the lowercase.
Example:
<script>
let message=’CODING NINJAS’;
let result=message.toLowerCase();
document.write(result);
</script>
Output
coding ninjas
15. JavaScript String toUpperCase() Method
It convert the string to uppercase characters. This method does not make any changes in the original string. Returns the string in the uppercase.
Example:
<script>
let message=’coding ninjas’
let result=message.toUpperCase();
document.write(result);
</script>
Output
CODING NINJAS
16. JavaScript String trim() Method
It removes the whitespace from both the ends of the string i.e beginning and the end.
Example:
<script>
let string=” Hi, This is Rudra! ”;
let result = string.trim();
document.write(result);
</script>
Output
Hi, This is Rudra!
17. JavaScript trim End() Method
It trims the white space from the end of the string. It does not make any changes in the original string.
Example:
<script>
let string= ‘ Hi, This is Rudra! ‘;
let result = string.trimStart();
document.write(result);
</script>
Output
‘Hi, This is Rudra! ‘
18. JavaScript pad Start() Method
It pads the present string from starting with the current string and returns the new string of the specified length
padStart(totalLength,padString)
Example:
<script>
let string=‘ Hi, This is Rudra! ‘;
let result = string.trimEnd();
document.write(result);
</script>
Output
‘ Hi, This is Rudra!’
19. JavaScript pad End() Method
It pads the present string from the end with the current string and returns the new string of the specified length.
padEnd(totalLength,padString)
Example:
<script>
var number=99;
var result= number.padStart(5, 0);
document.write(result);
</script>
String function in javascript is a function which takes string as input and gives modified string as output. For example toUpperCase(), toLowerCase() and trim().
Q. What does string () do?
String() is a constructor function in JavaScript that is used to generate a new string object from a specified value, such as a number or another data type, by converting it to a string.
Q. How do you write a string function in JavaScript?
A string function in JavaScript is created by declaring a function, receiving string parameters, performing specified actions within the function, and then returning a string value.
Q. How do you declare a string function?
Declare a string function in JavaScript by defining a named function, providing parameters, and returning the appropriate string result based on the input parameters and logic within the function.
Conclusion
In this blog, we have discussed string Functions in JavaScript. We have looked at different methods and properties of string functions in javascript. Learn more about strings in javascript, by learning from the blogs below: