12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- /** @format */
- // ---------String-----------
- interface String {
- /**
- * 对一个字符串中出现的字符串全部替换为另一个字符串
- * @param oldString 被替换的字符串
- * @param newString 新的字符串
- */
- replaceAll(oldString: string, newString: string): string
- trimLeftAndRight(): string
- trimLeft(): string
- trimRight(): string
- /**
- * 计算字符串长度,半角长度为1,全角长度为2
- * @return 字符串长度
- */
- getStrLen(): number
- /**
- * 判断字符是全角还是半角
- * @param char 字符
- * @return true:半角 false:全角
- */
- isDbcCase(char: number): boolean
- zeroPrefix(num: number, n: number): string
- }
- String.prototype.replaceAll = function (oldString: string, newString: string) {
- return this.replace(new RegExp(oldString, 'g'), newString)
- }
- String.prototype.trimLeftAndRight = function () {
- return this.replace(/(^\s*)|(\s*$)/g, '')
- }
- String.prototype.trimLeft = function () {
- return this.replace(/(^\s*)/g, '')
- }
- String.prototype.trimRight = function () {
- return this.replace(/(\s*$)/g, '')
- }
- String.prototype.isDbcCase = function (char: number) {
- let ret = false
- if (char >= 32 && char <= 127) {
- ret = true // 基本拉丁字母(即键盘上可见的,空格、数字、字母、符号)
- } else if (char >= 65377 && char <= 65439) {
- ret = true // 日文半角片假名和符号
- }
- return ret
- }
- String.prototype.getStrLen = function () {
- let len = 0
- for (let i = 0; i < this.length; i++) {
- len += this.isDbcCase(this.charCodeAt(i)) ? 1 : 2
- }
- return len
- }
- String.prototype.zeroPrefix = function (num: number, n: number): string {
- return (Array(n).join('0') + num).slice(-n)
- }
|