-2

I intend to check if a string is a substring of another string. However, case insensitive match is not possible since toLowerCase() and toUpperCase() methods are not supported in Rhino 1.7.13.

var stored_string="{{SSHA1}9BC34549D565D9505B287DE0CD20AC77BE1D3F2C"
var str = "9bc34549d565d9505b287de0cd20ac77be1d3f2c"

I am using indexOf mathod to check for the substring.

if (stored_string.toString().indexOf(str)===0) {
   //do something
}

Is there any good way this comparison is possible case insensitive?

Karan Nayyar
  • 666
  • 2
  • 7
  • 14
  • Rhino 1.7.13 has `toLowerCase` and `toUpperCase` methods on strings. You can try it [here](https://www.jdoodle.com/execute-rhino-online/). That's the code: `var stored_string="{{SSHA1}9BC34549D565D9505B287DE0CD20AC77BE1D3F2C"; print(stored_string.toLowerCase());` Why are you converting the string to string with `stored_string.toString()`? – jabaa Jan 27 '22 at 16:28
  • toLowerCase() is working.But indexOf() is not able to validate if it's a substring. – Karan Nayyar Jan 28 '22 at 05:30
  • How is the given and accepted answer the correct answer? It only adds a polyfill for the existing `toLowerCase`. – jabaa Jan 28 '22 at 08:51

1 Answers1

0

Here's a toLowerCase polyfill that should work. I'm unsure of all the restrictions of rhino, but this should work for standard ASCII characters a-z and A-Z

function toLowerCase(str) {
  var output = "";
  for (let i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
      output += String.fromCharCode(str.charCodeAt(i) + 32);
    } else {
      output += str[i];
    }
  }
  return output;
}

console.log(toLowerCase(prompt("Enter a string")));

so we can use that here:

function toLowerCase(str) {
  var output = "";
  for (let i = 0; i < str.length; i++) {
    if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
      output += String.fromCharCode(str.charCodeAt(i) + 32);
    } else {
      output += str[i];
    }
  }
  return output;
}

var stored_string="{{SSHA1}9BC34549D565D9505B287DE0CD20AC77BE1D3F2C"
var str = "9bc34549d565d9505b287de0cd20ac77be1d3f2c"

console.log(toLowerCase(stored_string).indexOf(toLowerCase(str)) !== -1 || toLowerCase(str).indexOf(toLowerCase(stored_string)) !== -1);
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34