在無憂腳本上看到的《面試官的變態出題 ASP字符串操作!》,發上來,以開拓思路。
引用內容
現有兩個字符串:
a=“abcde”
b=“12345”
1.請編寫一組自定義函數,將上面兩個字符串轉換成:
c=“a1b2c3d4e5”
2.再將c=“a1b2c3d4e5” 逆轉換成
a=“abcde”
b=“12345”
時間:10分鐘內。
以下是幾個解題思路:
一、ASP解決:
程序代碼
<%
function change_str(a,b)
if len(a)>len(b) then
length_=len(b)
else
length_=len(a)
end if
return_value=""
for i=1 to length_
return_value=return_value&mid(a,i,1)&mid(b,i,1)
next
change_str=return_value
end function
function change_str_(a)
return_value_a=""
return_value_b=""
for i=1 to len(a)
if i mod 2=0 then
return_value_a=return_value_a&mid(a,i,1)
else
return_value_b=return_value_b&mid(a,i,1)
end if
next
change_str_=return_value_b&return_value_a
end function
a="abcde"
b="12345"
response.Write(change_str_(change_str(a,b)))
%>
二、VbScript,與一思路相似:
程序代碼
<script type="text/vbscript">
<!--
dim a,b,c
a="abcde"
b="12345"
c="a1b2c3d4e5"
msgbox addstr(a,b)
msgbox breakstr(c)
function addstr(a,b)
dim c
c=""
for i=1 to len(a)
c=c & mid(a,i,1) &mid(b,i,1)
next
addstr = c
End function
function breakstr(c)
dim a,b
a=""
b=""
for i=1 to len(c)
if i mod 2 =1 then
a = a & mid(c,i,1)
else
b = b & mid(c,i,1)
End if
next
breakstr = "a=" & a & vbcrlf & "b=" & b
End function
-->
</script>
三、JavaScript解決:
程序代碼
<script language="Javascript">
function toghther(){
var a = "abcde";
var b = "12345";
var c ="";
var d ="";
var e ="";
var f = "";
var a_1 = "";
var b_1 = "";
for (i=0;i<a.length;i++){
a_1 = a.charAt(i);
b_1 = b.charAt(i);
c += a_1 + b_1;
}
alert(c);
for (j=0;j<c.length;j++){
if (j%2 == 0) { d += c.charAt(j);}
else { e += c.charAt(j);}
}
alert(d);
alert(e);
}
toghther();
</script>
四、JavaScript解決:
程序代碼
<script type="text/Javascript">
var a = "abcde";
var b = "12345";
function two2one(a, b){
var i = 0;
var z = b.split("");
return a.replace(/(.)/g, function(x, y){
return x + z[i ++];
});
}
function one2two(c){
var r1 = [], r2 = [];
var i = 0;
c.replace(/(.)/g, function(x, y){
i ++ % 2 ? r1.push(y) : r2.push(y);
});
return [r2.join(""), r1.join("")];
}
var c = two2one(a, b);
alert(c);
var d = one2two(c);
var a = d[0];
var b = d[1];
alert(a + "\n" + b);
</script>
程序就是這樣的,很多種解決辦法,閒著沒事,給補上了一個,數組,主要是以前就寫過類似的函數為了自己使用。
http://www.knowsky.com/
程序代碼
<%
dim a:a = "abcde"
dim b:b = "12345"
dim aa:aa = toArray(a,"|")
dim bb:bb = toArray(b,"|")
dim cc:cc = AddStr(aa,bb,"|")
Response.write AddStr(aa,bb,"|")
Response.Write "<br>"
Call BreakStr(cc)
'將普通字符串轉為數組函數,方便入庫等操作
Function toArray(byVal StrA,byVal StrSplit)
dim StrALen,i,StrArray:StrALen = Len(StrA)
For i = 1 to StrALen
StrArray = StrArray & mid(StrA,i,1) & StrSplit
Next
StrArray = Left(StrArray, Len(StrArray)-1)
toArray = StrArray
End Function
Function AddStr(byVal ArrayA,byVal ArrayB,byVal StrSplit)
Dim ArrayASPlit,ArrayBSplit,ArrayAUbound
ArrayASPlit = Split(ArrayA,StrSplit)
ArrayBSplit = Split(ArrayB,StrSplit)
ArrayAUbound = Ubound(ArrayASPlit)
dim i
For i = 0 to ArrayAUbound
ArrayStr = ArrayStr & ArrayASPlit(i) & ArrayBSplit(i)
Next
AddStr = ArrayStr
End Function
Sub BreakStr(byVal StrA)
dim ArrayA,ArrayASPlit,ArrayAUbound,ArrayStrA,ArrayStrB
ArrayA = toArray(StrA,"|")
ArrayASPlit = Split(ArrayA,"|")
ArrayAUbound = Ubound(ArrayASPlit)
dim i,j
For i = 0 to ArrayAUbound
if i mod 2=0 then
ArrayStrA = ArrayStrA & ArrayASPlit(i)
Else
ArrayStrB = ArrayStrB & ArrayASPlit(i)
end if
Next
Response.Write ArrayStrA & "<br>" &ArrayStrB
End Sub
%>