티스토리 뷰

* ASP, VBScript 에서 한글은 2byte 이며 영문은 1byte 이다.
* MS-SQL 에서 한글/영문 공통 1byte 이다.

VBS 이용방식

'//TODO: 한영혼합문장의 왼쪽에서부터 수량만큼 문자를 읽어온다.
Function LeftH(str, strlen)
    Dim rValue, tmpStr, tmpASC, lenSum, f
 
    If isset(str) Then
        lenSum = 0
        rValue = ""
 
        for f = 1 to Len(str)
            tmpStr = MID(str,f,1)
            tmpASC = ASC(tmpStr)
            if tmpASC>0 and tmpASC<255 then lenSum = lenSum + 1    else lenSum = lenSum + 2
            rValue = rValue & tmpStr
            if(lenSum > strlen) then exit for
        next
        LeftH = rValue
    End If
End Function
'//TODO: 한영혼합문장의 오른쪽에서부터 수량만큼 문자를 읽어온다.
Function RightH(str, strlen)
    Dim rValue, tmpStr, tmpASC, lenSum, f
 
    If isset(str) Then
        lenSum = 0
        rValue = ""
        str = strReverse(str)
        for f = 1 to Len(str)
            tmpStr = MID(str,f,1)
            tmpASC = ASC(tmpStr)
            if tmpASC>0 and tmpASC<255 then lenSum = lenSum + 1    else lenSum = lenSum + 2
            rValue = rValue & tmpStr
            if(lenSum > strlen) then exit for
        next
        RightH = strReverse(rValue)
    End If
End Function
'//TODO: 한영혼합문장의 start 위치에서부터 length 만큼 문자를 읽어온다.
Function MidH(s, start, length)
    Dim f, CharAt, VBLength, VBn1, VBn2, BLength, AddByte
    VBn2=length
    VBLength=Len(s)
    BLength=0
    for f=1 to VBLength
        CharAt=mid(s, f, 1)
        if asc(CharAt)>0 and asc(CharAt)<255 then
            BLength=BLength + 1
        else
            BLength=BLength + 2
        end if
        If BLength>=start Then
            Exit For
        End If
    next
 
    VBn1=f
    If VBn1<1 Then VBn1=1
 
    BLength=0
    for f=VBn1 to VBLength
        CharAt=mid(s, f, 1)
        if asc(CharAt)>0 and asc(CharAt)<255 then
            BLength=BLength + 1
        else
            BLength=BLength + 2
        end if
        If BLength=length Then
            VBn2=f+1
            Exit For
        ElseIf BLength>length Then
            VBn2=f
            Exit For
        End If
    next
    MidH=Mid(s, VBn1, VBn2-VBn1)
End Function
'//TODO: 한영혼합문장의 길이를 잰다
Function LenH(s)
    Dim f, CharAt, Result, VBLength
    Result=0
    VBLength=Len(s)
    for f=1 to VBLength
        CharAt=mid(s, f, 1)
        if asc(CharAt)>0 and asc(CharAt)<255 then
            Result=Result+1
        else
            Result=Result+2
        end if
    next
    LenH=Result
End Function


SQL 이용하는 방식

'//TODO: 한영혼합문장의 왼쪽에서부터 수량만큼 문자를 읽어온다.
Function LeftH(str, strlen)
    sql = "Select Left('"& str &"',"& strLen &")"
    LeftH = sql_result(sql, 0)
End Function
 
'//TODO: 한영혼합문장의 오른쪽에서부터 수량만큼 문자를 읽어온다.
Function RightH(str, strlen)
    sql = "Select Right('"& str &"',"& strLen &")"
    RightH = sql_result(sql, 0)
End Function
 
'//TODO: 한영혼합문장의 start 위치에서부터 length 만큼 문자를 읽어온다.
Function MidH(s, start, length)
    // 아래 두줄은 SQL쿼리로 하는 방법
    sql = "Select Substring(@str, @iOffset, @strLen)"
    sql = str_replace(sql, "@str", s)
    sql = str_replace(sql, "@iOffset", start)
    sql = str_replace(sql, "@strLen", length)
    MidH = sql_result(sql, 0)
End Function
 
'//TODO: 한영혼합문장의 길이를 잰다
Function LenH(s)
    sql = "Select Len('"& str &"')"
    LenH = sql_result(sql, 0)
End Function
'***************************************************************************
'* SQL 쿼리문을 받아 해당 순서의 필드 값을 리턴
'* result = sql_result( 쿼리, 필드순서 )
'***************************************************************************
Function sql_result(strSql, s)
    On Error Resume Next
    Dim result, Rs
    Set Rs = DbCon.Execute(strSql)
    If NOT Rs.EOF Then result = Rs(s)
    Rs.Close : Set Rs = Nothing
    sql_result = result
End Function
 
'******************************************************
'* str_replace(찾아서, 바꿔라, 원본) As String
'******************************************************
Function str_replace(strFrom, strTo, str)
    On Error Resume Next
 
    If IsNull(strTo) then strTo = ""
 
    str_replace = Replace(str, strFrom, strTo)
End Function

'웹프로그래밍 > ASP Classic' 카테고리의 다른 글

ASP SHA1  (0) 2010.06.01
XMLHTTP 를 이용한 GET, PUT, POST 하기  (0) 2010.04.08
Classic ASP Tagged Posts (Link)  (0) 2010.04.02
[ASP] Crazy Beaver  (0) 2010.04.02
ASP Xtreme Evolution Framework  (0) 2010.04.02
댓글