当前位置导航:炫浪网>>网络学院>>网页制作>>ASP教程

ASP字符串操作的集中解决方法

在无忧脚本上看到的《面试官的变态出题 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
%>

相关内容
赞助商链接