JS交叉合并

JavaScript021

JS交叉合并,第1张

交叉合并:如字符串一为:abcd  字符串二为:1234。则结果为:a1b2c3d4。

import java. util. ArrayListimport java. util. Listpublic class A {

public static void main(string[] args){

string a[]={"a","b","c"}

string b[]={"1","2","3"}

CombineTwoArray(a,b)

system. out. println(CombineTwoArray(a,b))

}

public static List<string>combineTwoArray(string[] first, String[] second)t List<string>outlist=new ArrayList<string>()

int index=0

while(index<first. length &&index<second. length){

outlist. add(first[ index])outlist. add(second[ index])

++indexif(index>=first. length){

for (int i=indexi<second. lengthi++){

outlist. add(second[i])

}

} else{

for (int i=indexi<first. lengthi++){

outlist. add(first[i])

}

return outlist

}

}

js的写法:

<!DOCTYPE html pUBLIC"-//w3c//DTD XHTMT 1.0

Transitional//EN""http://www.w3.org/TR/xhtm11/DrD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtm1">

<head>

<title>第十五题:交叉合并字符串</title>

<style>

#form{

margin:0auto;width:500px;top:100px;height:300px;

</style>

<script type="text/javascript">

var reg="*[A-za-20-9]+$";function check(){

var strl =document.all.strl.value;var str2 =document.all.str2.value;if(str1==""){

alert(“字符串一不允许为空!");document.all.strl.focus();return;

}else if(!/^[A-2a-20-9]+$/.test(str1)){

alert("字符串一只能是数字和字母的组合");document.all.str1.focus();return;

}else if(str2==""){

alert(”字符串二不允许为空!");document.all.str2.focus();

return;

}else if(!/^[A-Za-20-9]+5/.test(str2)){

alert(“字符串二只能是数字和字母的组合");document.a1l.str2.focus();return;var string="";var font1="<font style='color:red;'>";var font2="<font style='color:blue;'>";var fontend="</font>";if(str1.1ength>=str2.1ength){

for(vari=0;i<str1.1ength;i++){

if(i<str2.length){

string +=font1+strl[i]+fontend+ font2+ str2[i]+fontend;

}else{

string+=font1+strl[il;

}else{

for(var i=0;i<str2.1ength;i++){

if(i<strl.length){

document.all.result.innerHTML=string;

</script>

</head>

<body>

<div id="form">

<center><h2>第十五题:交叉合并字符串</h2></center>

字符串1:<input type="text"id="str1"/><br/>字符串2:<input type="text"id="str2"/><br/>

<input type="button"value="交叉合并字符串”onclick="check();"/>

<br/>

<div id="result"></div>

</div>

</body></html>

string+=font1+strl[i]+fontend+font2+str2[i]+fontend;

}else{

string+=font2+str2[i]+fontend;

扩展资料:

/**

* 合并两个函数

* @param functionA 先执行

* @param functionB 执行完 functionA 后返回

* @returns {*}

*/

function mergeFunction (functionA, functionB) {

if (!functionA || !functionB) return

var merge = functionB

functionB = (function () {

merge.call(this)

functionA.call(this)

})()

return functionB = merge

}

方法一:使用"+"将两个字符串"相加"

示例:

var longString = "One piece " + "plus one more piece."

//结果:longString值为:"One piece plus one more piece."

方法二: 要将多个字符串累积为一个字符串,还可以使用"+="操作符:

示例:

var result = ""

result += "My name is Anders"

result += " and my age is 25"

//结果:result值为"My name is Anders and my age is 25"

方法三:方法concat(),它完成与"+"相同的功能:string.concat(value1, value2, ...)

示例:

var str4="字符串连接"

str4=str4.concat("use concat function connec string")

//结果:"字符串链接use concat function connec string"