`
aine_pan
  • 浏览: 43905 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
解析text area内容
//需求: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");
}
read file content by byte
public static String readFileContent(File f) throws Exception{
		BufferedInputStream reader = null;
		BufferedReader intwo = null;
		StringBuffer buff = new StringBuffer();
		char[] data = new char[100];
		if(!f.exists()){
			throw new Exception("Input file is not exist.");
		}
		try{
			reader = new BufferedInputStream(new FileInputStream(f));
			intwo = new BufferedReader(new InputStreamReader(reader));
			while(intwo.read(data)!=-1){
				buff.append(data);
				data = new char[100];//care about this.
			}
			return StringUtil.convertString(buff.toString().trim());
		}catch(Exception e){
			throw e;
		}finally{
			reader.close();
			intwo.close();
		}
	}

//if you don't clear the char[], it may still has the old value in last cycles.
Global site tag (gtag.js) - Google Analytics