A sequence of letters, special characters, numbers, etc. or a combination of all of them is known as a string. Strings are created by enclosing the string characters within a single quote (‘) or within double quotes (“).
Syntax:
var myString = 'Good Morning123!'; // Single quoted string var myString = "Good Morning123!"; // Double quoted string
In Javascript, many string methods are either built-in or user-defined. Built-in string methods are the methods that are present in any programming language library.
Built-in string methods of JavaScript:
- search(): It is used to search a string for a specific value or an expression. It returns the position of the match.
- split(): It is used to split a string into an array of substrings.
- startsWith(): It is used to check whether a string begins with the specified characters.
- slice(): It is used to extract a part of a string and return a new string.
- concat(): It is used to combine texts of two strings and return a new string.
- charAt(): It is used to return the character at the specified index.
- indexOf() It is used to return the index, within the string object, which occurs first, of the specified value. It returns -1 if the object is not found.
- lastIndexOf(): It is used to return the index, within the string object, which occurs last, of the specified value. It returns -1 if the object is not found.
- match(): It is used to match a regular expression against a string.
- replace(): It is used to find a match between a regular expression and a string. The matched substring is replaced with a new substring.
- substr(): It is used to return the characters in a string, beginning at the specified location, through the specified number of characters.
- substring(): It is used to return the characters in a string between the two specified indexes.
- toLowerCase(): It is used to convert the string value which is called, to lower case.
- toUpperCase(): It is used to convert the string value which is called, to upper case.
- valueOf(): It is used to return the primitive value of the specified object.
- User-defined string methods in JavaScript:
- logIt(): It is used to log a parameter back to the console, when the code is executed.
- return(): It is used to return a specific value, explicitly.
User-defined string methods: The methods that are defined by the user to perform a particular task.
Example: Search() method.
<p id="demo"></p> function myFunction() { var str = "Welcome to GeeksforGeeks!"; var a= str.search("GeeksforGeeks"); document.getElementById("demo").innerHTML = a; } |
Output:
11
Example: split() method.
<p id="demo"></p> function myFunction() { var str = "How are you feeling today?"; var res = str.split(" "); document.getElementById("demo").innerHTML = res; } |
How, are, you, feeling, today?
Example: startsWith() method.
<p id="demo"></p> function myFunction() { var str = "Hello world, welcome to the universe."; var n = str.startsWith("Hello"); document.getElementById("demo").innerHTML = n; } |
Output:
true
Recommended Posts:
- builtin command in Linux with examples
- JavaScript | Replace multiple strings with multiple other strings
- JavaScript | Strings
- Compare the Case Insensitive strings in JavaScript
- How to create multi-line strings in JavaScript?
- Optimum way to compare strings in JavaScript
- How to sort strings in JavaScript?
- Difference between single-quoted and double-quoted strings in JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Objects in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


