Home  >  Article  >  Backend Development  >  A useful string interception function (a Chinese character counts as two characters, and an English character counts as one character)

A useful string interception function (a Chinese character counts as two characters, and an English character counts as one character)

WBOY
WBOYOriginal
2016-07-25 09:02:501367browse
  1. '****************************************** ***********
  2. 'Function name: gotTopic
  3. 'Function: truncate the string, one Chinese character counts as two characters, and one English character counts as one character
  4. 'Parameter: str ----Original string
  5. ' strlen ---- intercept length
  6. 'Return value: intercepted string
  7. '********************************** *******************
  8. Function gotTopic(ByVal str, ByVal strlen)
  9. If str = "" Then
  10. gotTopic = ""
  11. Exit Function
  12. End If
  13. Dim l , t, c, i, strTemp
  14. str = Replace(Replace(Replace(Replace(str, " ", " "), """, Chr(34)), ">", ">"), " <", "<")
  15. l = Len(str)
  16. t = 0
  17. strTemp = str
  18. strlen = CLng(strlen)
  19. For i = 1 To l
  20. c = Abs(Asc(Mid(str, i , 1)))
  21. If c > 255 Then
  22. t = t + 2
  23. Else
  24. t = t + 1
  25. End If
  26. If t >= strlen Then
  27. strTemp = Left(str, i)
  28. Exit For
  29. End If
  30. Next
  31. If strTemp <> str Then
  32. strTemp = strTemp & "…"
  33. End If
  34. gotTopic = Replace(Replace(Replace(Replace(strTemp, " ", " "), Chr(34), " ""), ">", ">"), "<", "<")
  35. End Function
  36. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn