//需求:SWIFT要求报文长度有限制,我们在页面先做一次截取。
//textarea 输入的内容可以有换行, 如果遇到空格的话,会自己检测是否跨行,然后进行软换行。这样js判断的时候就要注意了,因为软换行不能用换行符来判断,所以看下面代码。
function splitByLen(str,len){
if(str.trim().length==0){
return new Array();
}
var totleStr=str.replaceAll("\r\n","\n");
var currStr="";
var beforStr="";
var temp = "";
var n = 0;
var array = new Array();
var newLine =false;
while(totleStr.length>0){
//find current string
totleStr = totleStr.trim();
var index = totleStr.indexOf(" ");
if(index==-1){
currStr = totleStr;
var ind = currStr.indexOf("\n");
if(ind!=-1){
currStr = totleStr.substring(0,ind);
if(currStr.length>len){
currStr = totleStr.substring(0,len);
totleStr = totleStr.substring(len);
}else{
totleStr = totleStr.substring(ind);
}
newLine = true;
}else{
if(currStr.length>len){
currStr = totleStr.substring(0,len);
totleStr = totleStr.substring(len);
}else{
totleStr = "";
}
}
}else{
currStr = totleStr.substring(0,index);
var ind = currStr.indexOf("\n");
if(ind!=-1){
currStr = totleStr.substring(0,ind);
if(currStr.length>len){
currStr = totleStr.substring(0,len);
totleStr = totleStr.substring(len);
}else{
totleStr = totleStr.substring(ind);
}
newLine = true;
}else{
totleStr = totleStr.substring(index);
}
}
//find before string cacl current string.
if(beforStr.length ==0){
temp = currStr;
}else{
temp = beforStr+" "+currStr;
}
if(temp.length>len){
if(beforStr.length<len){
array[n] = beforStr+" ";
totleStr= currStr+totleStr;
}else{
array[n] = beforStr;
totleStr = " "+currStr+totleStr;
}
newLine = true;
beforStr = "";
}else if(newLine == true){
array[n] = temp;
beforStr = "";
}else{
beforStr = temp;
}
temp="";
if( newLine == true){
newLine = false;
n++;
}
}
if(beforStr.length>0){
array[n] = beforStr;
}
return array;
}
function removeLastLine(str,len){
var arr = splitByLen(str,len);
var retStr="";
for(var i =0;i<arr.length-1;i++){
retStr+=arr[i];
}
return retStr;
}
function test(){
while(row_count > row_limit){
var lastLine = lines[count-1];
var strLast = removeLastLine(lastLine,limitField.cols);
row_count--;
if(strLast.trim().length == 0){
count--;
lines.remove(lastLine);
}else{
lines[count-1] = strLast;
}
}
var newArray=lines.splice(0,row_limit);
limitField.value = newArray.join("\n");
}
|