Damerau-Levenshtein
Similar to Levenshtein, Damerau-Levenshtein distance with transposition (also sometimes calls unrestricted Damerau-Levenshtein distance) is the minimum number of operations needed to transform one string into the other, where an operation is defined as an insertion, deletion, or substitution of a single character, or a transposition of two adjacent characters.
It does respect triangle inequality, and is thus a metric distance.
This is not to be confused with the optimal string alignment distance, which is an extension where no substring can be edited more than once.
using System; using F23.StringSimilarity; public class Program { public static void Main(string[] args) { var d = new Damerau(); // 1 substitution Console.WriteLine(d.Distance("ABCDEF", "ABDCEF")); // 2 substitutions Console.WriteLine(d.Distance("ABCDEF", "BACDFE")); // 1 deletion Console.WriteLine(d.Distance("ABCDEF", "ABCDE")); Console.WriteLine(d.Distance("ABCDEF", "BCDEF")); Console.WriteLine(d.Distance("ABCDEF", "ABCGDEF")); // All different Console.WriteLine(d.Distance("ABCDEF", "POIU")); } }
Will produce:
Jaro-Winkler
Jaro-Winkler is a string edit distance that was developed in the area of record linkage (duplicate detection) (Winkler, 1990). The Jaro–Winkler distance metric is designed and best suited for short strings such as person names, and to detect typos.
Jaro-Winkler computes the similarity between 2 strings, and the returned value lies in the interval .
It is (roughly) a variation of Damerau-Levenshtein, where the substitution of 2 close characters is considered less important then the substitution of 2 characters that a far from each other.
The distance is computed as 1 — Jaro-Winkler similarity.
using System; using F23.StringSimilarity; public class Program { public static void Main(string[] args) { var jw = new JaroWinkler(); // substitution of s and t Console.WriteLine(jw.Similarity("My string", "My tsring")); // substitution of s and n Console.WriteLine(jw.Similarity("My string", "My ntrisg")); } }
will produce:
JavaScript
JS Array
concat()
constructor
copyWithin()
entries()
every()
fill()
filter()
find()
findIndex()
forEach()
from()
includes()
indexOf()
isArray()
join()
keys()
length
lastIndexOf()
map()
pop()
prototype
push()
reduce()
reduceRight()
reverse()
shift()
slice()
some()
sort()
splice()
toString()
unshift()
valueOf()
JS Boolean
constructor
prototype
toString()
valueOf()
JS Classes
constructor()
extends
static
super
JS Date
constructor
getDate()
getDay()
getFullYear()
getHours()
getMilliseconds()
getMinutes()
getMonth()
getSeconds()
getTime()
getTimezoneOffset()
getUTCDate()
getUTCDay()
getUTCFullYear()
getUTCHours()
getUTCMilliseconds()
getUTCMinutes()
getUTCMonth()
getUTCSeconds()
now()
parse()
prototype
setDate()
setFullYear()
setHours()
setMilliseconds()
setMinutes()
setMonth()
setSeconds()
setTime()
setUTCDate()
setUTCFullYear()
setUTCHours()
setUTCMilliseconds()
setUTCMinutes()
setUTCMonth()
setUTCSeconds()
toDateString()
toISOString()
toJSON()
toLocaleDateString()
toLocaleTimeString()
toLocaleString()
toString()
toTimeString()
toUTCString()
UTC()
valueOf()
JS Error
name
message
JS Global
decodeURI()
decodeURIComponent()
encodeURI()
encodeURIComponent()
escape()
eval()
Infinity
isFinite()
isNaN()
NaN
Number()
parseFloat()
parseInt()
String()
undefined
unescape()
JS JSON
parse()
stringify()
JS Math
abs()
acos()
acosh()
asin()
asinh()
atan()
atan2()
atanh()
cbrt()
ceil()
cos()
cosh()
E
exp()
floor()
LN2
LN10
log()
LOG2E
LOG10E
max()
min()
PI
pow()
random()
round()
sin()
sqrt()
SQRT1_2
SQRT2
tan()
tanh()
trunc()
JS Number
constructor
isFinite()
isInteger()
isNaN()
isSafeInteger()
MAX_VALUE
MIN_VALUE
NEGATIVE_INFINITY
NaN
POSITIVE_INFINITY
prototype
toExponential()
toFixed()
toLocaleString()
toPrecision()
toString()
valueOf()
JS OperatorsJS RegExp
constructor
compile()
exec()
g
global
i
ignoreCase
lastIndex
m
multiline
n+
n*
n?
n{X}
n{X,Y}
n{X,}
n$
^n
?=n
?!n
source
test()
toString()
(x|y)
.
\w
\W
\d
\D
\s
\S
\b
\B
\0
\n
\f
\r
\t
\v
\xxx
\xdd
\uxxxx
JS Statements
break
class
continue
debugger
do…while
for
for…in
for…of
function
if…else
return
switch
throw
try…catch
var
while
JS String
charAt()
charCodeAt()
concat()
constructor
endsWith()
fromCharCode()
includes()
indexOf()
lastIndexOf()
length
localeCompare()
match()
prototype
repeat()
replace()
search()
slice()
split()
startsWith()
substr()
substring()
toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toString()
toUpperCase()
trim()
valueOf()
JavaScript Type Conversion Table
This table shows the result of converting different JavaScript values to Number, String, and Boolean:
OriginalValue | Convertedto Number | Convertedto String | Convertedto Boolean | Try it |
---|---|---|---|---|
false | «false» | false | Try it » | |
true | 1 | «true» | true | Try it » |
«0» | false | Try it » | ||
1 | 1 | «1» | true | Try it » |
«0» | «0» | true | Try it » | |
«000» | «000» | true | Try it » | |
«1» | 1 | «1» | true | Try it » |
NaN | NaN | «NaN» | false | Try it » |
Infinity | Infinity | «Infinity» | true | Try it » |
-Infinity | -Infinity | «-Infinity» | true | Try it » |
«» | «» | false | Try it » | |
«20» | 20 | «20» | true | Try it » |
«twenty» | NaN | «twenty» | true | Try it » |
«» | true | Try it » | ||
20 | «20» | true | Try it » | |
NaN | «10,20» | true | Try it » | |
NaN | «twenty» | true | Try it » | |
NaN | «ten,twenty» | true | Try it » | |
function(){} | NaN | «function(){}» | true | Try it » |
{ } | NaN | «» | true | Try it » |
null | «null» | false | Try it » | |
undefined | NaN | «undefined» | false | Try it » |
Values in quotes indicate string values.
Red values indicate values (some) programmers might not expect.
Adding Numbers and Strings
WARNING !!
JavaScript uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
If you add two numbers, the result will be a number:
Example
var x = 10;
var y = 20;
var z = x + y; // z will be 30 (a number)
If you add two strings, the result will be a string concatenation:
Example
var x = «10»;
var y = «20»;
var z = x + y; // z will be 1020 (a string)
If you add a number and a string, the result will be a string concatenation:
Example
var x = 10;
var y = «20»;
var z = x + y; // z will be 1020 (a string)
If you add a string and a number, the result will be a string concatenation:
Example
var x = «10»;
var y = 20;
var z = x + y; // z will be 1020 (a string)
A common mistake is to expect this result to be 30:
Example
var x = 10;
var y = 20;
var z = «The result is: » + x + y;
A common mistake is to expect this result to be 102030:
Example
var x = 10;
var y = 20;
var z = «30»;
var result = x + y + z;
The JavaScript interpreter works from left to right.
First 10 + 20 is added because x and y are both numbers.
Then 30 + «30» is concatenated because z is a string.
Shingle (n-gram) based algorithms
A few algorithms work by converting strings into sets of n-grams (sequences of n characters, also sometimes called k-shingles). The similarity or distance between the strings is then the similarity or distance between the sets.
The cost for computing these similarities and distances is mainly domnitated by k-shingling (converting the strings into sequences of k characters). Therefore there are typically two use cases for these algorithms:
Directly compute the distance between strings:
using System; using F23.StringSimilarity; public class Program { public static void Main(string[] args) { var dig = new QGram(2); // AB BC CD CE // 1 1 1 0 // 1 1 0 1 // Total: 2 Console.WriteLine(dig.Distance("ABCD", "ABCE")); } }
Or, for large datasets, pre-compute the profile or set representation of all strings. The similarity can then be computed between profiles or sets:
using System; using F23.StringSimilarity; public class Program { public static void Main(string[] args) { string s1 = "My first string"; string s2 = "My other string..."; // Let's work with sequences of 2 characters... var cosine = new Cosine(2); // For cosine similarity I need the profile of strings StringProfile profile1 = cosine.GetProfile(s1); StringProfile profile2 = cosine.GetProfile(s2); // Prints 0.516185 Console.WriteLine(profile1.CosineSimilarity(profile2)); } }
Pay attention, this only works if the same Cosine object is used to parse all input strings!
Q-Gram
The distance between two strings is defined as the L1 norm of the difference of their profiles (the number of occurences of each n-gram): SUM( |V1_i — V2_i| ). Q-gram distance is a lower bound on Levenshtein distance, but can be computed in O(m + n), where Levenshtein requires O(m.n)
Cosine similarity
The similarity between the two strings is the cosine of the angle between these two vectors representation, and is computed as V1 . V2 / (|V1| * |V2|)
Distance is computed as 1 — cosine similarity.
Jaccard index
Like Q-Gram distance, the input strings are first converted into sets of n-grams (sequences of n characters, also called k-shingles), but this time the cardinality of each n-gram is not taken into account. Each input string is simply a set of n-grams. The Jaccard index is then computed as |V1 inter V2| / |V1 union V2|.
Distance is computed as 1 — cosine similarity.
Jaccard index is a metric distance.
Sorensen-Dice coefficient
Similar to Jaccard index, but this time the similarity is computed as 2 * |V1 inter V2| / (|V1| + |V2|).
Distance is computed as 1 — cosine similarity.
Replacing String Content
The method replaces a specified value with another
value in a string:
Example
str = «Please visit Microsoft!»;
var n = str.replace(«Microsoft», «W3Schools»);
The method does not change the string it is called on. It returns a new string.
By default, the method replaces only the first match:
Example
str = «Please visit Microsoft and Microsoft!»;
var n = str.replace(«Microsoft», «W3Schools»);
By default, the method is case sensitive. Writing MICROSOFT (with
upper-case) will not work:
Example
str = «Please visit Microsoft!»;
var n = str.replace(«MICROSOFT», «W3Schools»);
To replace case insensitive, use a regular expression with an flag (insensitive):
Example
str = «Please visit Microsoft!»;
var n = str.replace(/MICROSOFT/i, «W3Schools»);
Note that regular expressions are written without quotes.
To replace all matches, use a regular expression with a flag (global match):
Example
str = «Please visit Microsoft and Microsoft!»;
var n = str.replace(/Microsoft/g, «W3Schools»);
You will learn a lot more about regular expressions in the chapter JavaScript Regular
Expressions.
Примечания для тех, кто наследует этот метод
При реализации собственных типов следует переопределить метод для возврата значений, которые являются значимыми для этих типов.When you implement your own types, you should override the method to return values that are meaningful for those types. Производные классы, которым требуется больший контроль над форматированием, чем предоставляет возможность реализовать интерфейс IFormattable.Derived classes that require more control over formatting than provides can implement the IFormattable interface. Его метод позволяет определять строки формата, которые управляют форматированием, и использовать объект IFormatProvider, который может обеспечить форматирование для определенного языка и региональных параметров.Its method enables you to define format strings that control formatting and to use an IFormatProvider object that can provide for culture-specific formatting.
Переопределения метода должны соответствовать следующим рекомендациям:Overrides of the method should follow these guidelines:
— Возвращаемая строка должна быть понятной и удобочитаемой для людей.- The returned string should be friendly and readable by humans.
— Возвращаемая строка должна уникальным образом идентифицировать значение экземпляра объекта.- The returned string should uniquely identify the value of the object instance.
-Возвращаемая строка должна быть максимально короткой, чтобы ее можно было отображать с помощью отладчика.- The returned string should be as short as possible so that it is suitable for display by a debugger.
-Переопределение не должно возвращать Empty или строку со значением NULL.- Your override should not return Empty or a null string.
— Переопределение не должно вызывать исключение.- Your override should not throw an exception.
— Если строковое представление экземпляра зависит от языка и региональных параметров или может быть отформатировано несколькими способами, реализуйте интерфейс IFormattable.- If the string representation of an instance is culture-sensitive or can be formatted in multiple ways, implement the IFormattable interface.
— Если возвращаемая строка содержит конфиденциальную информацию, необходимо сначала запросить соответствующее разрешение.- If the returned string includes sensitive information, you should first demand an appropriate permission. Если запрос проходит удачно, вы можете вернуть конфиденциальную информацию. в противном случае следует вернуть строку, которая исключается из конфиденциальной информации.If the demand succeeds, you can return the sensitive information; otherwise, you should return a string that excludes the sensitive information.
-Переопределение не должно иметь наблюдаемых побочных эффектов, чтобы избежать сложностей при отладке.- Your override should have no observable side effects to avoid complications in debugging. Например, вызов метода не должен изменять значение полей экземпляра.For example, a call to the method should not change the value of instance fields.
— Если тип реализует метод анализа (или или метод, конструктор или какой-либо другой статический метод, который создает экземпляр типа из строки), следует убедиться, что строка, возвращаемая методом , может быть преобразована в экземпляр объекта.- If your type implements a parsing method (or or method, a constructor, or some other static method that instantiates an instance of the type from a string), you should ensure that the string returned by the method can be converted to an object instance.
Longest Common Subsequence
The longest common subsequence (LCS) problem consists in finding the longest subsequence common to two (or more) sequences. It differs from problems of finding common substrings: unlike substrings, subsequences are not required to occupy consecutive positions within the original sequences.
It is used by the diff utility, by Git for reconciling multiple changes, etc.
The LCS distance between strings X (of length n) and Y (of length m) is n + m — 2 |LCS(X, Y)|
min = 0
max = n + m
LCS distance is equivalent to Levenshtein distance when only insertion and deletion is allowed (no substitution), or when the cost of the substitution is the double of the cost of an insertion or deletion.
This class implements the dynamic programming approach, which has a space requirement O(m.n), and computation cost O(m.n).
In «Length of Maximal Common Subsequences», K.S. Larsen proposed an algorithm that computes the length of LCS in time O(log(m).log(n)). But the algorithm has a memory requirement O(m.n²) and was thus not implemented here.
using System; using F23.StringSimilarity; public class Program { public static void Main(string[] args) { var lcs = new LongestCommonSubsequence(); // Will produce 4.0 Console.WriteLine(lcs.Distance("AGCAT", "GAC")); // Will produce 1.0 Console.WriteLine(lcs.Distance("AGCAT", "AGCT")); } }
Hexadecimal
JavaScript interprets numeric constants as hexadecimal if they are preceded by
0x.
Example
var x = 0xFF; // x will be 255
Never write a number with a leading zero (like 07).Some JavaScript versions interpret
numbers as octal if they are written with a leading zero.
By default, JavaScript displays numbers as base 10 decimals.
But you can use the method to output numbers from base 2
to base 36.
Hexadecimal is base 16. Decimal is base 10.
Octal is base 8. Binary is base 2.
Example
var myNumber = 32;myNumber.toString(10); // returns 32myNumber.toString(32); // returns
10
myNumber.toString(16); // returns 20
myNumber.toString(8); // returns 40
myNumber.toString(2); // returns 100000
JS Tutorial
JS HOMEJS IntroductionJS Where ToJS OutputJS StatementsJS SyntaxJS CommentsJS VariablesJS OperatorsJS ArithmeticJS AssignmentJS Data TypesJS FunctionsJS ObjectsJS EventsJS StringsJS String MethodsJS NumbersJS Number MethodsJS ArraysJS Array MethodsJS Array SortJS Array IterationJS DatesJS Date FormatsJS Date Get MethodsJS Date Set MethodsJS MathJS RandomJS BooleansJS ComparisonsJS ConditionsJS SwitchJS Loop ForJS Loop WhileJS BreakJS Type ConversionJS BitwiseJS RegExpJS ErrorsJS ScopeJS HoistingJS Strict ModeJS this KeywordJS LetJS ConstJS Arrow FunctionJS ClassesJS DebuggingJS Style GuideJS Best PracticesJS MistakesJS PerformanceJS Reserved WordsJS VersionsJS Version ES5JS Version ES6JS JSON
JavaScript
JS Array
concat()
constructor
copyWithin()
entries()
every()
fill()
filter()
find()
findIndex()
forEach()
from()
includes()
indexOf()
isArray()
join()
keys()
length
lastIndexOf()
map()
pop()
prototype
push()
reduce()
reduceRight()
reverse()
shift()
slice()
some()
sort()
splice()
toString()
unshift()
valueOf()
JS Boolean
constructor
prototype
toString()
valueOf()
JS Classes
constructor()
extends
static
super
JS Date
constructor
getDate()
getDay()
getFullYear()
getHours()
getMilliseconds()
getMinutes()
getMonth()
getSeconds()
getTime()
getTimezoneOffset()
getUTCDate()
getUTCDay()
getUTCFullYear()
getUTCHours()
getUTCMilliseconds()
getUTCMinutes()
getUTCMonth()
getUTCSeconds()
now()
parse()
prototype
setDate()
setFullYear()
setHours()
setMilliseconds()
setMinutes()
setMonth()
setSeconds()
setTime()
setUTCDate()
setUTCFullYear()
setUTCHours()
setUTCMilliseconds()
setUTCMinutes()
setUTCMonth()
setUTCSeconds()
toDateString()
toISOString()
toJSON()
toLocaleDateString()
toLocaleTimeString()
toLocaleString()
toString()
toTimeString()
toUTCString()
UTC()
valueOf()
JS Error
name
message
JS Global
decodeURI()
decodeURIComponent()
encodeURI()
encodeURIComponent()
escape()
eval()
Infinity
isFinite()
isNaN()
NaN
Number()
parseFloat()
parseInt()
String()
undefined
unescape()
JS JSON
parse()
stringify()
JS Math
abs()
acos()
acosh()
asin()
asinh()
atan()
atan2()
atanh()
cbrt()
ceil()
cos()
cosh()
E
exp()
floor()
LN2
LN10
log()
LOG2E
LOG10E
max()
min()
PI
pow()
random()
round()
sin()
sqrt()
SQRT1_2
SQRT2
tan()
tanh()
trunc()
JS Number
constructor
isFinite()
isInteger()
isNaN()
isSafeInteger()
MAX_VALUE
MIN_VALUE
NEGATIVE_INFINITY
NaN
POSITIVE_INFINITY
prototype
toExponential()
toFixed()
toLocaleString()
toPrecision()
toString()
valueOf()
JS OperatorsJS RegExp
constructor
compile()
exec()
g
global
i
ignoreCase
lastIndex
m
multiline
n+
n*
n?
n{X}
n{X,Y}
n{X,}
n$
^n
?=n
?!n
source
test()
toString()
(x|y)
.
\w
\W
\d
\D
\s
\S
\b
\B
\0
\n
\f
\r
\t
\v
\xxx
\xdd
\uxxxx
JS Statements
break
class
continue
debugger
do…while
for
for…in
for…of
function
if…else
return
switch
throw
try…catch
var
while
JS String
charAt()
charCodeAt()
concat()
constructor
endsWith()
fromCharCode()
includes()
indexOf()
lastIndexOf()
length
localeCompare()
match()
prototype
repeat()
replace()
search()
slice()
split()
startsWith()
substr()
substring()
toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toString()
toUpperCase()
trim()
valueOf()
JavaScript
JS Array
concat()
constructor
copyWithin()
entries()
every()
fill()
filter()
find()
findIndex()
forEach()
from()
includes()
indexOf()
isArray()
join()
keys()
length
lastIndexOf()
map()
pop()
prototype
push()
reduce()
reduceRight()
reverse()
shift()
slice()
some()
sort()
splice()
toString()
unshift()
valueOf()
JS Boolean
constructor
prototype
toString()
valueOf()
JS Classes
constructor()
extends
static
super
JS Date
constructor
getDate()
getDay()
getFullYear()
getHours()
getMilliseconds()
getMinutes()
getMonth()
getSeconds()
getTime()
getTimezoneOffset()
getUTCDate()
getUTCDay()
getUTCFullYear()
getUTCHours()
getUTCMilliseconds()
getUTCMinutes()
getUTCMonth()
getUTCSeconds()
now()
parse()
prototype
setDate()
setFullYear()
setHours()
setMilliseconds()
setMinutes()
setMonth()
setSeconds()
setTime()
setUTCDate()
setUTCFullYear()
setUTCHours()
setUTCMilliseconds()
setUTCMinutes()
setUTCMonth()
setUTCSeconds()
toDateString()
toISOString()
toJSON()
toLocaleDateString()
toLocaleTimeString()
toLocaleString()
toString()
toTimeString()
toUTCString()
UTC()
valueOf()
JS Error
name
message
JS Global
decodeURI()
decodeURIComponent()
encodeURI()
encodeURIComponent()
escape()
eval()
Infinity
isFinite()
isNaN()
NaN
Number()
parseFloat()
parseInt()
String()
undefined
unescape()
JS JSON
parse()
stringify()
JS Math
abs()
acos()
acosh()
asin()
asinh()
atan()
atan2()
atanh()
cbrt()
ceil()
cos()
cosh()
E
exp()
floor()
LN2
LN10
log()
LOG2E
LOG10E
max()
min()
PI
pow()
random()
round()
sin()
sqrt()
SQRT1_2
SQRT2
tan()
tanh()
trunc()
JS Number
constructor
isFinite()
isInteger()
isNaN()
isSafeInteger()
MAX_VALUE
MIN_VALUE
NEGATIVE_INFINITY
NaN
POSITIVE_INFINITY
prototype
toExponential()
toFixed()
toLocaleString()
toPrecision()
toString()
valueOf()
JS OperatorsJS RegExp
constructor
compile()
exec()
g
global
i
ignoreCase
lastIndex
m
multiline
n+
n*
n?
n{X}
n{X,Y}
n{X,}
n$
^n
?=n
?!n
source
test()
toString()
(x|y)
.
\w
\W
\d
\D
\s
\S
\b
\B
\0
\n
\f
\r
\t
\v
\xxx
\xdd
\uxxxx
JS Statements
break
class
continue
debugger
do…while
for
for…in
for…of
function
if…else
return
switch
throw
try…catch
var
while
JS String
charAt()
charCodeAt()
concat()
constructor
endsWith()
fromCharCode()
includes()
indexOf()
lastIndexOf()
length
localeCompare()
match()
prototype
repeat()
replace()
search()
slice()
split()
startsWith()
substr()
substring()
toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toString()
toUpperCase()
trim()
valueOf()
The constructor Property
The property returns the constructor
function for all JavaScript variables.
«John».constructor
// Returns function String() {}
(3.14).constructor
// Returns function Number() {}
false.constructor // Returns
function Boolean() {}
.constructor
// Returns function Array() {}
{name:’John’,age:34}.constructor
// Returns function Object() {}
new Date().constructor
// Returns function Date() {}
function () {}.constructor // Returns
function Function(){}
You can check the constructor property to find out if an object is an
(contains the word «Array»):
function isArray(myArray) {
return myArray.constructor.toString().indexOf(«Array») > -1;
}
Or even simpler, you can check if the object is an Array function:
function isArray(myArray) {
return myArray.constructor
=== Array;
}
You can check the constructor property to find out if an object is a
(contains the word «Date»):
function isDate(myDate) {
return myDate.constructor.toString().indexOf(«Date») > -1;
}
Or even simpler, you can check if the object is a Date function:
JavaScript
JS Array
concat()
constructor
copyWithin()
entries()
every()
fill()
filter()
find()
findIndex()
forEach()
from()
includes()
indexOf()
isArray()
join()
keys()
length
lastIndexOf()
map()
pop()
prototype
push()
reduce()
reduceRight()
reverse()
shift()
slice()
some()
sort()
splice()
toString()
unshift()
valueOf()
JS Boolean
constructor
prototype
toString()
valueOf()
JS Classes
constructor()
extends
static
super
JS Date
constructor
getDate()
getDay()
getFullYear()
getHours()
getMilliseconds()
getMinutes()
getMonth()
getSeconds()
getTime()
getTimezoneOffset()
getUTCDate()
getUTCDay()
getUTCFullYear()
getUTCHours()
getUTCMilliseconds()
getUTCMinutes()
getUTCMonth()
getUTCSeconds()
now()
parse()
prototype
setDate()
setFullYear()
setHours()
setMilliseconds()
setMinutes()
setMonth()
setSeconds()
setTime()
setUTCDate()
setUTCFullYear()
setUTCHours()
setUTCMilliseconds()
setUTCMinutes()
setUTCMonth()
setUTCSeconds()
toDateString()
toISOString()
toJSON()
toLocaleDateString()
toLocaleTimeString()
toLocaleString()
toString()
toTimeString()
toUTCString()
UTC()
valueOf()
JS Error
name
message
JS Global
decodeURI()
decodeURIComponent()
encodeURI()
encodeURIComponent()
escape()
eval()
Infinity
isFinite()
isNaN()
NaN
Number()
parseFloat()
parseInt()
String()
undefined
unescape()
JS JSON
parse()
stringify()
JS Math
abs()
acos()
acosh()
asin()
asinh()
atan()
atan2()
atanh()
cbrt()
ceil()
cos()
cosh()
E
exp()
floor()
LN2
LN10
log()
LOG2E
LOG10E
max()
min()
PI
pow()
random()
round()
sin()
sqrt()
SQRT1_2
SQRT2
tan()
tanh()
trunc()
JS Number
constructor
isFinite()
isInteger()
isNaN()
isSafeInteger()
MAX_VALUE
MIN_VALUE
NEGATIVE_INFINITY
NaN
POSITIVE_INFINITY
prototype
toExponential()
toFixed()
toLocaleString()
toPrecision()
toString()
valueOf()
JS OperatorsJS RegExp
constructor
compile()
exec()
g
global
i
ignoreCase
lastIndex
m
multiline
n+
n*
n?
n{X}
n{X,Y}
n{X,}
n$
^n
?=n
?!n
source
test()
toString()
(x|y)
.
\w
\W
\d
\D
\s
\S
\b
\B
\0
\n
\f
\r
\t
\v
\xxx
\xdd
\uxxxx
JS Statements
break
class
continue
debugger
do…while
for
for…in
for…of
function
if…else
return
switch
throw
try…catch
var
while
JS String
charAt()
charCodeAt()
concat()
constructor
endsWith()
fromCharCode()
includes()
indexOf()
lastIndexOf()
length
localeCompare()
match()
prototype
repeat()
replace()
search()
slice()
split()
startsWith()
substr()
substring()
toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toString()
toUpperCase()
trim()
valueOf()
Численное преобразование
Для численного преобразования объекта используется метод , а если его нет – то :
Метод обязан возвращать примитивное значение, иначе его результат будет проигнорирован. При этом – не обязательно числовое.
У большинства объектов нет
У большинства встроенных объектов такого нет, поэтому численное и строковое преобразования для них работают одинаково.
Исключением является объект , который поддерживает оба типа преобразований:
Детали спецификации
Если посмотреть в стандарт, то в пункте говорится о том, что есть у любых объектов. Но он ничего не делает, просто возвращает сам объект (непримитивное значение!), а потому игнорируется.
Overview
The main characteristics of each implemented algorithm are presented below. The «cost» column gives an estimation of the computational cost to compute the similarity between two strings of length m and n respectively.
Normalized? | Metric? | Type | Cost | ||
---|---|---|---|---|---|
distance | No | Yes | O(m*n) 1 | ||
distancesimilarity | Yes | No | O(m*n) 1 | ||
distance | No | No | O(m*n) 1 | ||
3 | distance | No | Yes | O(m*n) 1 | |
Optimal String Alignment 3 | not implemented yet | No | No | O(m*n) 1 | |
similaritydistance | Yes | No | O(m*n) | ||
distance | No | No | O(m*n) 1,2 | ||
distance | Yes | Yes | O(m*n) 1,2 | ||
distance | Yes | No | O(m*n) | ||
distance | No | No | Profile | O(m+n) | |
similaritydistance | Yes | No | Profile | O(m+n) | |
similaritydistance | Yes | Yes | Set | O(m+n) | |
similaritydistance | Yes | No | Set | O(m+n) |
In this library, Levenshtein edit distance, LCS distance and their sibblings are computed using the dynamic programming method, which has a cost O(m.n). For Levenshtein distance, the algorithm is sometimes called Wagner-Fischer algorithm («The string-to-string correction problem», 1974). The original algorithm uses a matrix of size m x n to store the Levenshtein distance between string prefixes.
If the alphabet is finite, it is possible to use the method of four russians (Arlazarov et al. «On economic construction of the transitive closure of a directed graph», 1970) to speedup computation. This was published by Masek in 1980 («A Faster Algorithm Computing String Edit Distances»). This method splits the matrix in blocks of size t x t. Each possible block is precomputed to produce a lookup table. This lookup table can then be used to compute the string similarity (or distance) in O(nm/t). Usually, t is choosen as log(m) if m > n. The resulting computation cost is thus O(mn/log(m)). This method has not been implemented (yet).
In «Length of Maximal Common Subsequences», K.S. Larsen proposed an algorithm that computes the length of LCS in time O(log(m).log(n)). But the algorithm has a memory requirement O(m.n²) and was thus not implemented here.
There are two variants of Damerau-Levenshtein string distance: Damerau-Levenshtein with adjacent transpositions (also sometimes called unrestricted Damerau–Levenshtein distance) and Optimal String Alignment (also sometimes called restricted edit distance). For Optimal String Alignment, no substring can be edited more than once.
Weighted Levenshtein
An implementation of Levenshtein that allows to define different weights for different character substitutions.
This algorithm is usually used for optical character recognition (OCR) applications. For OCR, the cost of substituting P and R is lower then the cost of substituting P and M for example because because from and OCR point of view P is similar to R.
It can also be used for keyboard typing auto-correction. Here the cost of substituting E and R is lower for example because these are located next to each other on an AZERTY or QWERTY keyboard. Hence the probability that the user mistyped the characters is higher.
Converting Dates to Strings
The global method can convert dates to
strings.
String(Date()) // returns «Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)»
The Date method does the same.
Example
Date().toString() // returns «Thu Jul 17 2014 15:38:19 GMT+0200 (W. Europe Daylight Time)»
In the chapter Date Methods, you
will find more methods that can be used to convert dates to
strings:
Method | Description |
---|---|
getDate() | Get the day as a number (1-31) |
getDay() | Get the weekday a number (0-6) |
getFullYear() | Get the four digit year (yyyy) |
getHours() | Get the hour (0-23) |
getMilliseconds() | Get the milliseconds (0-999) |
getMinutes() | Get the minutes (0-59) |
getMonth() | Get the month (0-11) |
getSeconds() | Get the seconds (0-59) |
getTime() | Get the time (milliseconds since January 1, 1970) |
JavaScript
JS Array
concat()
constructor
copyWithin()
entries()
every()
fill()
filter()
find()
findIndex()
forEach()
from()
includes()
indexOf()
isArray()
join()
keys()
length
lastIndexOf()
map()
pop()
prototype
push()
reduce()
reduceRight()
reverse()
shift()
slice()
some()
sort()
splice()
toString()
unshift()
valueOf()
JS Boolean
constructor
prototype
toString()
valueOf()
JS Classes
constructor()
extends
static
super
JS Date
constructor
getDate()
getDay()
getFullYear()
getHours()
getMilliseconds()
getMinutes()
getMonth()
getSeconds()
getTime()
getTimezoneOffset()
getUTCDate()
getUTCDay()
getUTCFullYear()
getUTCHours()
getUTCMilliseconds()
getUTCMinutes()
getUTCMonth()
getUTCSeconds()
now()
parse()
prototype
setDate()
setFullYear()
setHours()
setMilliseconds()
setMinutes()
setMonth()
setSeconds()
setTime()
setUTCDate()
setUTCFullYear()
setUTCHours()
setUTCMilliseconds()
setUTCMinutes()
setUTCMonth()
setUTCSeconds()
toDateString()
toISOString()
toJSON()
toLocaleDateString()
toLocaleTimeString()
toLocaleString()
toString()
toTimeString()
toUTCString()
UTC()
valueOf()
JS Error
name
message
JS Global
decodeURI()
decodeURIComponent()
encodeURI()
encodeURIComponent()
escape()
eval()
Infinity
isFinite()
isNaN()
NaN
Number()
parseFloat()
parseInt()
String()
undefined
unescape()
JS JSON
parse()
stringify()
JS Math
abs()
acos()
acosh()
asin()
asinh()
atan()
atan2()
atanh()
cbrt()
ceil()
cos()
cosh()
E
exp()
floor()
LN2
LN10
log()
LOG2E
LOG10E
max()
min()
PI
pow()
random()
round()
sin()
sqrt()
SQRT1_2
SQRT2
tan()
tanh()
trunc()
JS Number
constructor
isFinite()
isInteger()
isNaN()
isSafeInteger()
MAX_VALUE
MIN_VALUE
NEGATIVE_INFINITY
NaN
POSITIVE_INFINITY
prototype
toExponential()
toFixed()
toLocaleString()
toPrecision()
toString()
valueOf()
JS OperatorsJS RegExp
constructor
compile()
exec()
g
global
i
ignoreCase
lastIndex
m
multiline
n+
n*
n?
n{X}
n{X,Y}
n{X,}
n$
^n
?=n
?!n
source
test()
toString()
(x|y)
.
\w
\W
\d
\D
\s
\S
\b
\B
\0
\n
\f
\r
\t
\v
\xxx
\xdd
\uxxxx
JS Statements
break
class
continue
debugger
do…while
for
for…in
for…of
function
if…else
return
switch
throw
try…catch
var
while
JS String
charAt()
charCodeAt()
concat()
constructor
endsWith()
fromCharCode()
includes()
indexOf()
lastIndexOf()
length
localeCompare()
match()
prototype
repeat()
replace()
search()
slice()
split()
startsWith()
substr()
substring()
toLocaleLowerCase()
toLocaleUpperCase()
toLowerCase()
toString()
toUpperCase()
trim()
valueOf()
JS Tutorial
JS HOMEJS IntroductionJS Where ToJS OutputJS StatementsJS SyntaxJS CommentsJS VariablesJS OperatorsJS ArithmeticJS AssignmentJS Data TypesJS FunctionsJS ObjectsJS EventsJS StringsJS String MethodsJS NumbersJS Number MethodsJS ArraysJS Array MethodsJS Array SortJS Array IterationJS DatesJS Date FormatsJS Date Get MethodsJS Date Set MethodsJS MathJS RandomJS BooleansJS ComparisonsJS ConditionsJS SwitchJS Loop ForJS Loop WhileJS BreakJS Type ConversionJS BitwiseJS RegExpJS ErrorsJS ScopeJS HoistingJS Strict ModeJS this KeywordJS LetJS ConstJS Arrow FunctionJS ClassesJS DebuggingJS Style GuideJS Best PracticesJS MistakesJS PerformanceJS Reserved WordsJS VersionsJS Version ES5JS Version ES6JS JSON
Escape Character
Because strings must be written within quotes, JavaScript will misunderstand this string:
var x = «We are the so-called «Vikings» from the north.»;
The string will be chopped to «We are the so-called «.
The solution to avoid this problem, is to use the backslash escape character.
The backslash () escape character turns special characters into string characters:
Code | Result | Description |
---|---|---|
\’ | ‘ | Single quote |
\» | « | Double quote |
\\ | \ | Backslash |
The sequence inserts a double quote in a string:
Example
var x = «We are the so-called \»Vikings\» from the north.»;
The sequence inserts a single quote in a string:
Example
var x = ‘It\’s alright.’;
The sequence inserts a backslash in a string:
Example
var x = «The character \\ is called backslash.»;
Six other escape sequences are valid in JavaScript:
Code | Result |
---|---|
\b | Backspace |
\f | Form Feed |
\n | New Line |
\r | Carriage Return |
\t | Horizontal Tabulator |
\v | Vertical Tabulator |
The 6 escape characters above were originally designed to control
typewriters, teletypes, and fax machines. They do not make any sense in HTML.