willwm's notebooks

  • String.localeCompare Example - /willwm/string-localecompare-example
    Last edited 5 months ago
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare function areEqualIgnoreCase(str1, str2) { if (!str1 || !str2) { return false; } const result = str1.localeCompare(str2, undefined, { sensitivity: 'base' }); return (result === 0); } function runTestCase(str1, str2) { const areEqual = areEqualIgnoreCase(str1, str2); console.log(`Are ${str1} and ${str2} equal (ignoring case)? ${areEqual}`); } runTestCase('foo', 'bar'); runTestCase('foo', 'foo'); runTestCase('foo', 'FoO'); runTestCase('bar', 'BAR');