当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JavaScript 字符串与数组转换函数[不用split与join]

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JavaScript 字符串与数组转换函数[不用split与join]


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 390 ::
收藏到网摘: n/a

下面的代码,不考虑他的运行效率的话,思路不错,大家可以看看,但在实际的应用中,我们仍然使用split或join来实现数组的操作。

两个自定义的实现字符串与数组相互转换的js函数,希望能对大家有用:

复制代码 代码如下:

function StringToArray(str,substr) {
/* 函数功能:字符串按照指定字符串分割转换为数组
参数:
str :需转换的字符串
substr:分割字符串
返回值:
转换后的数组
*/
var arrTmp = new Array();
if(substr=="") {
arrTmp.push(str);
return arrTmp;
}
var i=0, j=0, k=str.length;
while(i<k) {
j = str.indexOf(substr,i);
if(j!=-1) {
if(str.substring(i,j)!="") { arrTmp.push(str.substring(i,j)); }
i = j+1;
} else {
if(str.substring(i,k)!="") { arrTmp.push(str.substring(i,k)); }
i = k;
}
}
return arrTmp;
}
function ArrayToString(arr,str) {
/* 函数功能:数组根据分割字符(串)转换为字符串
参数:
arr:需转换的字符串数组
str:分割字符串
返回值:
转换后的字符串
*/
var strTmp = "";
for(var i=0;i<arr.length;i++) {
if(arr[i]!="") {
if(strTmp=="") {
strTmp = arr[i];
} else {
strTmp = strTmp + str + arr[i];
}
}
}
return strTmp;
}

具体的应用可以参考下软晨学习网的相关文章。
javascript数组使用调用方法汇总
http://www.ruanchen.com/"http://www.ruanchen.com/" style="color: #000">http://www.ruanchen.com/rticle/19987.htm