DEV Community

Cover image for Javascript String Methods You Must Know to Become an Algorithms Wizard

Javascript String Methods You Must Know to Become an Algorithms Wizard

Mauro Bono on July 06, 2019

In this article I want to talk about few basic string methods which are most commonly used in Javascript and very useful when it comes to solve pro...
Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

It's important to remember that under the hood a string is just (LIKE) an array of chars. What a good use of spread!

Collapse
 
mattmcmahon profile image
Matt McMahon • Edited

Great introduction, Mauro!

Sometimes string manipulation can go wrong in strange ways. So it's good to know that there are alternative versions for some of these methods.

For example, the toLocalUpperCase and toLocalLowerCase methods property capitalize languages (other than, e.g., English) which can have some unexpected casing rules.

Also, some Unicode glyphs need multiple character codes. If you're working with languages (and not hashing or encoding), you may want to reach for the codePointAt method, which handles this type of glyph correctly. If splitting or reversing a string isn't working as you'd expect, keep this method in mind.

Depending on your audience and local, you may never need to reach for these methods. As your audience grows, though, using them can solve a number of strange and hard to understand bugs in string manipulation.

Cheers!

Collapse
 
uptheirons78 profile image
Mauro Bono

Thanks for the useful reply. This post is meant to be an introduction of the methods I used more on solving coding problems, but I am surely going to integrate it even with your suggestions. Have a nice day!

Collapse
 
pjijin profile image
Jijin P πŸ‘¨β€πŸ’»πŸ¦„

In 5th, one of the output is wrong.

const str = 'The Force will be with you. Always.';

const newStr = str.toLowerCase();

console.log(newStr.includes('force')); //false

This will return true.

Collapse
 
uptheirons78 profile image
Mauro Bono • Edited

My mistake. I forgot to change the return value after copying the line from before. Thanks, I am going to correct ;)

Collapse
 
uptheirons78 profile image
Mauro Bono

As I told before, thanks for this suggestion. Now a question. The problem with split() is only when it comes to split the string on any character ? or even when you need to split at white spaces to create an array of words inside a sentence with split(' ')? just to understand better this "problem"

 
adam_cyclones profile image
Adam Crockett πŸŒ€

I don't think of JavaScript as a language anymore, it's more of a vendors interpretation of an engine. I appreciate your point of view but honestly, if something behaves like something else who cares if factually not the same.

Thread Thread
 
sleeplessbyte profile image
Derk-Jan Karrenbeld

JavaScript has, as you might be aware, a specification called EcmaScript. The implementation (say v8) is indeed an interpretation of that specification, but this specification does dictate how all this is supposed to work, quite well. In this case, it even states that the specification doesn't specify how strings are supposed to be stored. It also specifically states that the string "elements" are the UTF-16 integers that @dominela10 speaks about.

ref: String Type

However, it also describes how the string iterator is different than the array iterator. To clarify: it specifically states that the string iterator iterates over codepoints, which is the closest thing to a character JavaScript has, which is not the same as a code element. To interpret a string as an array is therefore something that is indeed pretty careless.

ref: String Iterator

Iterators are not Arrays, Arrays are, depending on the language, iterable, or yield an iterator; strings are, depending on the language iterable, or yield an iterator; iterators are iterable.

In that aspect, @adam_cyclones you are correct that the string iterator is the same as an array iterator.

 
uptheirons78 profile image
Mauro Bono

Thanks for the answer!

 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

And what are most JavaScript engines written in?

Further to this, I am fully aware of what types exist and thier prototyping.

Collapse
 
uptheirons78 profile image
Mauro Bono • Edited

Thanks. I didn't know about this strange behaviour of split(). I am going to add your suggestions on the post.I learned something new and it is great. Thanks again and have a nice day!

 
sleeplessbyte profile image
Derk-Jan Karrenbeld

Yes, that is what I was saying as well :)

The ref links also link to the ecma spec :D

Thread Thread
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited

Such string much char. Only kidding I will have a read at some point.

EcmaScript .. what's that? 🀣