C# Corner
Tech
News
Videos
Forums
Trainings
Books
Events
More
Interviews
Jobs
Live
Learn
Career
Members
Blogs
Challenges
Certifications
Bounties
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Trim in JavaScript
WhatsApp
Abhay Shanker
5y
8.5
k
0
1
25
Blog
In JavaScript, there is no trim method. But below i have created a simple trim method which removes the leading and the trailing spaces from the string.
function
trim(str) {
str = str.replace(/^s+/,
''
)
for
(
var
i = str.length; i--;)
if
(/S/.test(str.charAt(i)))
return
str.substring(0, ++i)
return
str
}
function
testTrim() {
var
myString =
" Test Trim "
;
alert(trim(myString));
}
People also reading
Membership not found