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...
For further actions, you may consider blocking this person and/or reporting abuse
It's important to remember that under the hood a string is just (LIKE) an array of chars. What a good use of spread!
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
andtoLocalLowerCase
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!
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!
In 5th, one of the output is wrong.
This will return true.
My mistake. I forgot to change the return value after copying the line from before. Thanks, I am going to correct ;)
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"
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.
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
string
s 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.
Thanks for the answer!
And what are most JavaScript engines written in?
Further to this, I am fully aware of what types exist and thier prototyping.
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!
Yes, that is what I was saying as well :)
The ref links also link to the ecma spec :D
Such string much char. Only kidding I will have a read at some point.
EcmaScript .. what's that? π€£