localeCompare helps to compare strings in a sane manner (#tilPost)

Rodney Rehm recently tweeted about this function I have never heard of – String.prototype.localCompare. And wow – this can be so useful.

Did you want to know if a string includes a given character sequence no matter if upper or low…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Rodney Rehm recently tweeted about this function I have never heard of – String.prototype.localCompare. And wow - this can be so useful.

  • Did you want to know if a string includes a given character sequence no matter if upper or lower case?
  • Did you ever come across the problem that when you want to compare strings that include numeric values that this is usually not so easy?
'aBcD' === 'abcd' // false
'ábcd' === 'abcd' // false

'Price 2€'  > 'Price 1€'  // true
'Price 20€' > 'Price 3€' // false

Using localCompare you can define several options that can help out here. According to MDN it returns the following value:

A negative number if the reference string occurs before the compare string; positive if the reference string occurs after the compare string; 0 if they are equivalent.

So let's have a look

'aBcD'.localeCompare('abcd', undefined, { sensitivity: 'base' }) // 0 -> equal
'ábcd'.localeCompare('abcd', undefined, { sensitivity: 'base' }) // 0 -> equal

'Price 20€'.localeCompare('Price 3€', undefined, { numeric: true })  // 1
'Price 20€'.localeCompare('Price 3€', undefined, { numeric: false }) // -1

This can help to find out if strings have an equal base without fiddling around with numeric code point values and you have to admit that the numeric option is really cool!!!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-06T17:56:40+00:00) » localeCompare helps to compare strings in a sane manner (#tilPost). Retrieved from https://www.scien.cx/2017/08/07/localecompare-helps-to-compare-strings-in-a-sane-manner-tilpost/.
MLA
" » localeCompare helps to compare strings in a sane manner (#tilPost)." Stefan Judis | Sciencx - Monday August 7, 2017, https://www.scien.cx/2017/08/07/localecompare-helps-to-compare-strings-in-a-sane-manner-tilpost/
HARVARD
Stefan Judis | Sciencx Monday August 7, 2017 » localeCompare helps to compare strings in a sane manner (#tilPost)., viewed 2022-12-06T17:56:40+00:00,<https://www.scien.cx/2017/08/07/localecompare-helps-to-compare-strings-in-a-sane-manner-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » localeCompare helps to compare strings in a sane manner (#tilPost). [Internet]. [Accessed 2022-12-06T17:56:40+00:00]. Available from: https://www.scien.cx/2017/08/07/localecompare-helps-to-compare-strings-in-a-sane-manner-tilpost/
CHICAGO
" » localeCompare helps to compare strings in a sane manner (#tilPost)." Stefan Judis | Sciencx - Accessed 2022-12-06T17:56:40+00:00. https://www.scien.cx/2017/08/07/localecompare-helps-to-compare-strings-in-a-sane-manner-tilpost/
IEEE
" » localeCompare helps to compare strings in a sane manner (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2017/08/07/localecompare-helps-to-compare-strings-in-a-sane-manner-tilpost/. [Accessed: 2022-12-06T17:56:40+00:00]
rf:citation
» localeCompare helps to compare strings in a sane manner (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2017/08/07/localecompare-helps-to-compare-strings-in-a-sane-manner-tilpost/ | 2022-12-06T17:56:40+00:00
https://github.com/addpipe/simple-recorderjs-demo