Tải bản đầy đủ (.pdf) (6 trang)

Các hàm UNICODE thông dụng pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (102.59 KB, 6 trang )

Các hàm UNICODE thông dụng
Đổi chữ thường thành chữ hoa:
Function UpperUniChar(Ch) As String
' Return the Uppercase for a given vowel or dd
Dim Pos ' Position of character in Unicode vowel list
' Locate the character in list of Unicode vowels
Pos = InStr(UVowels, Ch)
If (Pos > 67) Then
UpperUniChar = Ch ' It's already uppercase - leave it alone
ElseIf (Pos > 0) Then
' It's a Lowercase Unicode Vowel - so get the corresponding Uppercase
vowel in the list
UpperUniChar = Mid(UVowels, Pos + 67, 1)
Else
' It's just a normal ANSI character
UpperUniChar = UCase(Ch)
End If
End Function

Đọc Text UNICODE từ file



Public Function ReadTextFile(FileName) As String
' Write a Unicode String to UTF-16LE Text file
' Remember to Project | References "Microsoft Scripting Runtime" to support
' FileSystemObject & TextStream
Dim Fs As FileSystemObject
Dim TS As TextStream
' Create a FileSystem Object
Set Fs = CreateObject("Scripting.FileSystemObject")


' Open TextStream for Input.
' TriStateTrue means Read Unicode UTF-16LE
Set TS = Fs.OpenTextFile(FileName, ForReading, False, TristateTrue)
ReadTextFile = TS.ReadAll ' Read the whole content of the text file in one
stroke
TS.Close ' Close the Text Stream
Set Fs = Nothing ' Dispose FileSystem Object
End Function


Viết UNICODE text ra file



Public Sub WriteTextFile(FileName, StrOutText)
' Read a Unicode String from UTF-16LE Text file
' Remember to Project | References "Microsoft Scripting Runtime" to support
' FileSystemObject & TextStream
Dim Fs As FileSystemObject
Dim TS As TextStream
' Create a FileSystem Object
Set Fs = CreateObject("Scripting.FileSystemObject")
' Open TextStream for Output, create file if necesssary
' TriStateTrue means Write Unicode UTF-16LE
Set TS = Fs.OpenTextFile(FileName, ForWriting, True, TristateTrue)
TS.Write StrOutText ' Write the whole StrOutText string in one stroke
TS.Close ' Close the Text Stream
Set Fs = Nothing ' Dispose FileSystem Object
End Sub


Cách gọn và tiện nhất để đọc và viết UTF-8 text files là dùng một VB6 Class
tên clsUnicodeText dựa vào MS DOM (Document Object Model) và XML như
sau:



Dim MyUnicodeText As clsUnicodeText
Set MyUnicodeText = New clsUnicodeText
' Read Unicode Text from file txtFileName and display in TextBox1
TextBox1.Text = MyUnicodeText.ReadUnicode(txtFileName)

Listing của Class clsUnicodeText như sau:


Option Explicit
Private mDOMTextFile As DOMDocument
Private mXMLPath As String
Public Function ReadUnicode(TXMLPath)
Dim objTextFileRoot As IXMLDOMElement
Set mDOMTextFile = New DOMDocument
mXMLPath = TXMLPath
mDOMTextFile.Load mXMLPath
'start at the root element of the XML
Set objTextFileRoot = mDOMTextFile.documentElement
ReadUnicode = objTextFileRoot.nodeTypedValue
End Function

Public Sub WriteUnicode(OutText, Optional TXMLPath)
Dim tDOMNode As IXMLDOMElement ' Temporary Node for DOM
If IsMissing(TXMLPath) Then

' Save the information on the screen by creating a new element and add its
children to the DOM object
mDOMTextFile.documentElement.Text = OutText
' Update the XML file
mDOMTextFile.save mXMLPath
Else
Set mDOMTextFile = New DOMDocument
' Create a Node called "Text" in DOM
Set tDOMNode = mDOMTextFile.createElement("Text")
' Make it the Root Node
mDOMTextFile.appendChild tDOMNode
' Assign Output Text to Root Node
mDOMTextFile.documentElement.Text = OutText
' Update the XML file
mDOMTextFile.save TXMLPath
End If
End Sub
Nguồn: Vovisoft
{/tab}


×