How To Compare Strings In Which Appears Similar Characters But Different Char Codes?
I have the problem with comparing strings with different char codes but similar characters like the following: console.log('³' === '3') // false; False value from the code above
Solution 1:
Look into ASCII folding, which is primarily used to convert accented characters to unaccented ones. There's a JS library for it here.
For your provided example, it will work - for other examples, it might not. It depends on how the equivalence is defined (nobody but you knows what you mean by "similar" - different characters are different characters).
If you know all of the characters that you want to map already, the easiest way will simply be to define a mapping yourself:
var eqls = function(first, second) {
var mappings = { '³': '3', '3': '3' };
if (mappings[first]) {
return mappings[first] == mappings[second];
}
returnfalse;
}
if (eqls('³', '3')) { ... }
Solution 2:
There is no "universal solution"
If you've only to deal with digits you may build up your "equivalence table" where for each supported character you define a "canonical" character.
For example
var eqTable = []; // the table is just an array
eqTable[179] = 51; // ³ --> 3/* ... */
Then build a simple algorythm to turn a string into its canonical form
var original, // the source string - let's assume original=="³3"var canonical = ""; // the canonical resulting stringvar i,
n,
c;
n = original.length;
for( i = 0; i < n; i++ )
{
c = eqTable[ original.charCodeAt( i ) ];
if( typeof( c ) != 'undefined' )
{
canonical += String.fromCharCode( c );
}
else
{
canonical += original[ i ]; // you *may* leave the original character if no match is found
}
}
// RESULT: canonical == "33"
Post a Comment for "How To Compare Strings In Which Appears Similar Characters But Different Char Codes?"