交叉合并:如字符串一为: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
}
1、split()方法——字符串打散为数组
2、indexOf()——返回字符串中匹配子串的第一个字符的下标。如果不包含匹配子串,返回-1。可以用作判断字符串是否包含特定字符
3、lastIndexOf()——返回字符串匹配子串的最后一个字符的下标。
4、join()——将数组用指定的分隔符合并为一个字符串
5、substring()——返回指定下标之间的字符串
6、substr——返回一个指定位置开始的指定长度的子字符串