Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Check a String Starts/Ends with a Specific String in jQuery?
JavaScript provides several built-in methods to check if a string starts or ends with specific characters. While jQuery is excellent for DOM manipulation, these string operations are handled by native JavaScript methods that work efficiently without any additional library.
In this article, we'll explore various approaches to verify if a string begins or ends with another string using practical examples.
Using startsWith() Method
The startsWith() method is the most direct way to check if a string begins with specific characters. It's case-sensitive and returns a boolean value.
Syntax
str.startsWith(searchString, position)
Parameters
searchString: The string to search for at the beginning
position: Optional. The position to start searching from (default is 0)
Example
let str = 'Hi, how are you?';
console.log(str.startsWith('Hi')); // true
console.log(str.startsWith('how')); // false
console.log(str.startsWith('how', 4)); // true (starts from index 4)
console.log(str.startsWith('hi')); // false (case-sensitive)
true false true false
Using endsWith() Method
The endsWith() method checks if a string ends with specified characters.
Syntax
str.endsWith(searchString, length)
Example
let str = 'Hi, how are you?';
console.log(str.endsWith('?')); // true
console.log(str.endsWith('you?')); // true
console.log(str.endsWith('you')); // false
console.log(str.endsWith('how', 7)); // true (check within first 7 chars)
true true false true
Using indexOf() Method
The indexOf() method can be used to check string position. For start checking, index should be 0.
Example
let str = 'Hi, how are you?';
// Check if starts with 'Hi'
let startsWithHi = str.indexOf('Hi') === 0;
console.log('Starts with "Hi":', startsWithHi);
// Check if ends with '?'
let endsWithQuestion = str.indexOf('?') === str.length - 1;
console.log('Ends with "?":', endsWithQuestion);
// Find position of 'how'
console.log('Position of "how":', str.indexOf('how'));
Starts with "Hi": true Ends with "?": true Position of "how": 4
Using substring() Method
The substring() method extracts characters between two indices and can be used for start/end checking.
Example
let str = 'Hi, how are you?';
// Check if starts with 'Hi'
let firstTwo = str.substring(0, 2);
console.log('First 2 characters:', firstTwo);
console.log('Starts with "Hi":', firstTwo === 'Hi');
// Check if ends with 'you?'
let lastFour = str.substring(str.length - 4);
console.log('Last 4 characters:', lastFour);
console.log('Ends with "you?":', lastFour === 'you?');
First 2 characters: Hi Starts with "Hi": true Last 4 characters: you? Ends with "you?": true
Using slice() Method
The slice() method works similarly to substring() but supports negative indices.
Example
let str = 'Hi, how are you?';
// Check start with slice
console.log('First 2 chars:', str.slice(0, 2));
console.log('Starts with "Hi":', str.slice(0, 2) === 'Hi');
// Check end with negative slice
console.log('Last 4 chars:', str.slice(-4));
console.log('Ends with "you?":', str.slice(-4) === 'you?');
First 2 chars: Hi Starts with "Hi": true Last 4 chars: you? Ends with "you?": true
Complete Example with jQuery
<!DOCTYPE html>
<html>
<head>
<title>String Start/End Check</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div>
<h2>String: "Hello, World!"</h2>
<button id="checkStart">Check if starts with "Hello"</button>
<button id="checkEnd">Check if ends with "World!"</button>
<div id="result"></div>
</div>
<script>
$(document).ready(function() {
let text = "Hello, World!";
$('#checkStart').click(function() {
let result = text.startsWith('Hello');
$('#result').html('Starts with "Hello": ' + result);
});
$('#checkEnd').click(function() {
let result = text.endsWith('World!');
$('#result').html('Ends with "World!": ' + result);
});
});
</script>
</body>
</html>
Comparison of Methods
| Method | Purpose | Performance | Recommended For |
|---|---|---|---|
startsWith()/endsWith()
|
Direct start/end checking | Best | Primary choice |
indexOf() |
Find position + compare | Good | When you need position info |
substring()/slice()
|
Extract + compare | Moderate | When you need the extracted part |
Conclusion
For checking if strings start or end with specific characters, use startsWith() and endsWith() methods as they are specifically designed for this purpose and offer the best performance. Other methods like indexOf(), substring(), and slice() can be useful when you need additional functionality beyond simple start/end checking.
