-
Notifications
You must be signed in to change notification settings - Fork 2
/
StringUtil.ts
50 lines (48 loc) · 1.49 KB
/
StringUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* *****************************************************
*/
export class StringUtil {
public static splitSimpleKeyValuePair(pairString: string): Array<string> {
const index = pairString.indexOf(':');
let code = null;
let desc = '';
if (index > -1) {
if (index > 0) {
code = pairString.substr(0, index);
}
desc = pairString.length > index ? pairString.substr(index + 1) : '';
} else {
code = pairString;
}
return [code, desc];
}
/* tslint:disable no-bitwise */
public static hashCode(s: string) {
let hash = 0,
i,
chr,
len;
if (s.length === 0) {
return hash;
}
for (i = 0, len = s.length; i < len; i++) {
chr = s.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
public static endsWith(subjectString: string, searchString: string, position?: number): boolean {
if (
typeof position !== 'number' ||
!isFinite(position) ||
Math.floor(position) !== position ||
position > subjectString.length
) {
position = subjectString.length;
}
position -= searchString.length;
const lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
}
}