📄 stringoper.js
字号:
/*--------------------------------------------------|
| 字符串操作库 V1.0 |
|---------------------------------------------------|
| 作者: Roger.Que |
| 创建日期: |
|---------------------------------------------------|
| 修改者: Roger.Que |修改完成日期:2008年10月23日 |
| |
|--------------------------------------------------*/
/**
* 去掉字符串首尾空字符
*/
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
/**
* 保留指定长度的字符串,多余部分以repVal的内容替换。如'abcdefg'变为'abc...'。
* origString: 原字符串。
* maxLength: 保留的最大长度(byte).
* repVal: 替换字符串。
* @return 返回被替换的字符串,但是原字符串不变。
*/
String.prototype.remainPartString = function(origString, maxLength, repVal) {
var textLen = 0;
for (var i = 0; i < origString.length; i++)
{
if (origString.charCodeAt(i) > 255)
{
textLen += 2;
if (textLen >= maxLength)
{
return origString.substring(0, i) + repVal;
}
} else {
textLen++;
if (textLen >= maxLength && textLen-2 > 0)
{
return origString.substring(0, (i-1)/2*2) + repVal;
}
}
}
return origString;
}
/*
* 全程替换字符串中的指定字符串,然后返回已被替换的字符串。
*/
String.prototype.replaceAll = function(strForReplaced, strWouldBe) {
var origString = this;
if (strForReplaced == strWouldBe) {
return origString;
}
while (origString.indexOf(strForReplaced) > -1) {
origString = origString.replace(strForReplaced, strWouldBe);
}
return origString;
}
/*
* 返回所有以fromStr开始,以endStr结束的字符串,返回值都存放在数组中。
*/
String.prototype.findAllSubStrRange = function(fromStr, endStr) {
var arrResult = new Array();
var fromIndex = -1;
var endIndex = 0;
while (this.indexOf(fromStr, fromIndex + 1) > -1) {
fromIndex = this.indexOf(fromStr, fromIndex + 1);
if (this.indexOf(endStr, fromIndex + 1) > -1) {
endIndex = this.indexOf(endStr, fromIndex + 1);
arrResult.push(this.substring(fromIndex, endIndex + 1));
fromIndex = endIndex;
} else break;
}
return arrResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -