萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 中英混排字串截取函數:按字元寬度精準截斷並加省略號

核心難點

中文字佔 2 個字元寬度 (全形),英文/數字/符號佔 1 個 (半形)。單純用 `Len` 或 `Left` 會導致中英文混排時顯示寬度不一致。

完整函數庫

<%
' ==========================================
' 1. 計算字串顯示寬度 (中文=2, 英文=1)
' ==========================================
Function StrDisplayWidth(str)
    Dim i, ch, width
    If IsNull(str) Or str = "" Then
        StrDisplayWidth = 0
        Exit Function
    End If
    width = 0
    For i = 1 To Len(str)
        ch = Mid(str, i, 1)
        ' AscW < 0 或 > 255 視為全形 (中文、日文、全形符號等)
        If AscW(ch) < 0 Or AscW(ch) > 255 Then
            width = width + 2
        Else
            width = width + 1
        End If
    Next
    StrDisplayWidth = width
End Function

' ==========================================
' 2. 按顯示寬度截取並加省略號
' ==========================================
Function StrCutWidth(str, maxWidth, ellipsis)
    Dim i, ch, width, result
    If IsNull(str) Or str = "" Then
        StrCutWidth = ""
        Exit Function
    End If
    If IsMissing(ellipsis) Or ellipsis = "" Then ellipsis = "..."
    
    If StrDisplayWidth(str) <= maxWidth Then
        StrCutWidth = str
        Exit Function
    End If
    
    width = 0
    result = ""
    For i = 1 To Len(str)
        ch = Mid(str, i, 1)
        If AscW(ch) < 0 Or AscW(ch) > 255 Then
            width = width + 2
        Else
            width = width + 1
        End If
        
        If width > maxWidth - StrDisplayWidth(ellipsis) Then
            Exit For
        End If
        result = result & ch
    Next
    
    StrCutWidth = result & ellipsis
End Function

' ==========================================
' 3. 截取指定起迄標記之間內容 (支援巢狀)
' ==========================================
Function StrExtractBetween(content, startTag, endTag, includeTags)
    Dim startPos, endPos, startLen
    If IsNull(content) Or content = "" Then
        StrExtractBetween = ""
        Exit Function
    End If
    
    startPos = InStr(1, content, startTag, 1) ' vbTextCompare
    If startPos = 0 Then
        StrExtractBetween = ""
        Exit Function
    End If
    
    startLen = Len(startTag)
    If includeTags Then
        endPos = InStr(startPos, content, endTag, 1)
        If endPos = 0 Then StrExtractBetween = "" : Exit Function
        StrExtractBetween = Mid(content, startPos, endPos - startPos + Len(endTag))
    Else
        endPos = InStr(startPos + startLen, content, endTag, 1)
        If endPos = 0 Then StrExtractBetween = "" : Exit Function
        StrExtractBetween = Mid(content, startPos + startLen, endPos - startPos - startLen)
    End If
End Function
%>

使用範例

<%
Dim title, summary, html
title = "【重磅】ASP 字串截取函數:完美支援中英混排顯示寬度計算"
summary = "這是一個很長的摘要內容,包含中文、English、數字123、符號!@#,需要精準截取..."

Response.Write "

原標題 (" & StrDisplayWidth(title) & "寬度)

" Response.Write "

" & Server.HTMLEncode(title) & "

" Response.Write "

截取 40 寬度

" Response.Write "

" & Server.HTMLEncode(StrCutWidth(title, 40)) & "

" Response.Write "

截取 60 寬度

" Response.Write "

" & Server.HTMLEncode(StrCutWidth(title, 60, "…")) & "

" ' HTML 內容提取範例 html = "

標題

內容段落

" Response.Write "

提取

內容

" Response.Write "

" & Server.HTMLEncode(StrExtractBetween(html, "

", "

", False)) & "

" %>

進階:CSS 方案 (現代瀏覽器推薦)

純前端單行/多行截斷,無需伺服端計算:

<style>
.text-ellipsis-1 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 300px; /* 必須指定寬度 */
}
.text-ellipsis-3 {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;
    overflow: hidden;
}
</style>

<div class="text-ellipsis-1">單行超出顯示省略號...</div>
<div class="text-ellipsis-3">多行最多三行,超出顯示省略號,支援中英混排自動換行...</div>

選擇建議

場景推薦方案
伺服端產生 Meta Description、RSS、純文字郵件ASP StrCutWidth 函數
網頁顯示、響應式設計、多語系CSS text-overflow / -webkit-line-clamp
舊版瀏覽器相容 (IE6-9)ASP 伺服端截取
需精確控制字數 (如簡訊 70 字限制)ASP StrDisplayWidth + 邏輯判斷

掌握伺服端精準寬度計算與現代 CSS 截斷雙技能,靈活應對各類字串顯示需求。

copyright © 萬盛學電腦網 all rights reserved