Fungsi-Fungsi String ala VB.NET

'Menghilangkan karakter-karakter tertentu pada awal String
Function TrimStart(Source As Variant, ParamArray sChar()) As String
    Dim i As Integer
    Dim j As Integer
    Dim strNewChar As String
    Dim strResult As String
  
    Source = Trim(CStr(Source))
  
    If Len(Source) = 0 Then Exit Function
  
    strResult = ""
  
    For i = 1 To Len(CStr(Source))
        strNewChar = Mid(Source, i, 1)
        For j = LBound(sChar) To UBound(sChar)
            If strNewChar = sChar(j) Then
                strNewChar = ""
            End If
        Next
      
        strResult = strResult & strNewChar
      
        If strNewChar < > "" Then
            Dim intLast As Integer
            intLast = i + 1
          
            strResult = strResult & Mid(Source, i + 1)
         
            Exit For
        End If
    Next
  
    Source = strResult
    TrimStart = Source
End Function

'Menghilangkan karakter-karakter tertentu pada akhir String
Function TrimEnd(Source As Variant, ParamArray sChar()) As String
    Dim i As Integer
    Dim j As Integer
    Dim strNewChar As String
    Dim strResult As String
  
    Source = Trim(CStr(Source))
    Source = StrReverse(CStr(Source))
  
    If Len(Source) = 0 Then Exit Function
  
    strResult = ""
  
    For i = 1 To Len(CStr(Source))
        strNewChar = Mid(Source, i, 1)
        For j = LBound(sChar) To UBound(sChar)
            If strNewChar = sChar(j) Then
                strNewChar = ""
            End If
        Next
      
        strResult = strResult & strNewChar
      
        If strNewChar < > "" Then
            Dim intLast As Integer
            intLast = i + 1
            strResult = strResult & Mid(Source, i + 1)
            Exit For
        End If
    Next
  
    Source = StrReverse(strResult)
    TrimEnd = Source
End Function

'Menghilangkan karakter-karakter tertentu pada awal dan akhir String
Function TrimAll(Source As Variant, ParamArray sChar()) As String
    Dim i As Integer
    Dim j As Integer
    Dim strNewChar As String
    Dim strResult As String
  
    Dim intLast As Integer
  
    Source = Trim(CStr(Source))
  
    If Len(Source) = 0 Then Exit Function
  
    strResult = ""
  
    For i = 1 To Len(CStr(Source))
        strNewChar = Mid(Source, i, 1)
        For j = LBound(sChar) To UBound(sChar)
            If strNewChar = sChar(j) Then
                strNewChar = ""
            End If
        Next
      
        strResult = strResult & strNewChar
      
        If strNewChar < > "" Then
            intLast = i + 1
          
            strResult = strResult & Mid(Source, i + 1)
         
            Exit For
        End If
    Next
  
    Source = strResult
  
    Source = Trim(CStr(Source))
    Source = StrReverse(CStr(Source))
  
    If Len(Source) = 0 Then Exit Function
  
    strResult = ""
  
    For i = 1 To Len(CStr(Source))
        strNewChar = Mid(Source, i, 1)
        For j = LBound(sChar) To UBound(sChar)
            If strNewChar = sChar(j) Then
                strNewChar = ""
            End If
        Next
      
        strResult = strResult & strNewChar
      
        If strNewChar < > "" Then
            intLast = i + 1
            strResult = strResult & Mid(Source, i + 1)
            Exit For
        End If
    Next
  
    Source = StrReverse(strResult)
    TrimAll = Source
End Function

'Memeriksa kandungan teks tertentu pada seluruh bagianString
Function Contains(Source As String, FindText As String) As Boolean
    If InStr(1, Source, FindText, vbTextCompare) < > 0 Then
        Contains = True
    End If
End Function

'Memeriksa kandungan teks tertentu pada awal String
Function StartWith(Source As String, FindText As String) As Boolean
    If LCase(Mid(Source, 1, Len(FindText))) = LCase(FindText) Then
        StartWith = True
    End If
End Function

'Memeriksa kandungan teks tertentu pada akhir String
Function EndsWith(Source As String, FindText As String) As Boolean
    If LCase(Right(Source, Len(FindText))) = LCase(FindText) Then
        EndsWith = True
    End If
End Function



komentar anda berarti buat kami....

0 komentar:

Posting Komentar