With clear code examples and descriptions, you'll learn how to use each method to perform common string operations like finding substrings, converting case, and removing whitespace 😎
So, Let's get started 🤖
str.length : returns the length of a string.
str.charAt(index) : returns the character at the specified index in a string.
str.indexOf(searchValue[, fromIndex]) : returns the index of the first occurrence of the specified value in a string.
str.lastIndexOf(searchValue[, fromIndex]) : returns the index of the last occurrence of the specified value in a string.
str.substring(start[, end]) : returns a new string that is a substring of the original string.
str.substr(start[, length]) : returns a new string that is a part of the original string.
str.toLowerCase() : returns a new string with all characters in lower case.
str.toUpperCase() : returns a new string with all characters in upper case.
str.trim() : removes whitespace from both ends of a string
str.replace(searchValue, replaceValue) : replaces all occurrences of the specified value in a string with another value.
Hope this is helpful ✨
Do Like ❤️ & Save 🔖
Do 𝗙𝗼𝗹𝗹𝗼𝘄 me on Linkedin for more:
Tips💡+ Guides📜 + Resources ⚡ related to Programming and Web Development 👨💻
Do Follow me here on dev.to ✅
Top comments (21)
Is Elon Musk really tweeted that?
No. It's just clickbait from the author. All their articles follow the same (deceptive) pattern to get attention.
It was sarcasm! But thanx for the reply! You do what you gatta do to, don't you?
Sorry. I'm really dense today. 😓
Don't we use str.replace(/l/g, "L") to replace all l with L present in string.
It uses Regx which is great but can get complicated for beginners who aren't familiar with regular expressions, otherwise, it's ❤️
yeah but then shouldn't it say "replaces FIRST occurrence of the specified value..." !
there's a .replaceAll, but it's relatively new, i guess, so using regex is your best bet.
Support is good
Amazing stuff, truly liked it.
One thing I would like to mention is I think instead of
str.charAt(index), It is prefer to use
Thanks a lot for sharing that, It will help a lot of people, The only reason why I think charAt() is more preferred over the str is that str has poor support with some very old browsers so that's why it is recommended to use charAt() as a safe bet otherwise str will work just fine 99.99℅ of the time
Again, Thank you so much for sharing this 🙌
Is this a problem in 2023?
Btw, there is a difference between
Thank You 🙌
Thanks a lot for the advice,
No problem 🙌
substr is more or less deprecated
That's why such posts are useless, it's always better to check the actual documentation.
Totally agree. Reading up on strings at mdn makes much more sense. Just trying to correct the obvious issues:)
Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍