این متود نمونه دیگه ای از همون متود پست قبل هست که نامش به EndWhiteSpace تغییر کرده و نوع خروجی اون یک Structure با نام WhitespaceString هست که کاربرد بیشتری نسبت به متود قبلی داره:
WhitespaceString Structure:
Structure WhitespaceString
Sub New(Value As String)
Me.ValueValue = Value
End Sub
Private ValueValue As String
Public ReadOnly Property Value() As String
Get
Return ValueValue
End Get
End Property
Public ReadOnly Property Length(Optional kind As WhitespaseKind = WhitespaseKind.All) As Integer
Get
Dim Chars() As Char
Dim CompChar As Char = vbNullChar
Dim StartIndex%, Len%
Dim WhiteSpaces% = 0
Len = Value.Length
Select Case kind
Case WhitespaseKind.All
Return Len
Case WhitespaseKind.Space
CompChar = Chr(32)
Case WhitespaseKind.vbCr
CompChar = Chr(13)
Case WhitespaseKind.vbCrlf
For i = 0 To Len - 1 Step 2
If i = Len - 1 Then Exit For
'Debug.Print("->", Value.Substring(i, 2))
If Value.Substring(i, 2).CompareTo(Chr(13) & Chr(10)) = 0 Then
WhiteSpaces += 1
End If
Next
GoTo ReturnLine
Case WhitespaseKind.vbLf
CompChar = Chr(10)
End Select
Chars = Value.ToCharArray(StartIndex, Len)
For i = 0 To Chars.Length - 1
If Chars(i).CompareTo(CompChar) = 0 Then
WhiteSpaces += 1
End If
Next
ReturnLine:
Return WhiteSpaces
End Get
End Property
Public Function Contains(chr As String) As Boolean
Return Value.Contains(chr)
End Function
End Structure
EndWhitespace Extension Method:
<Extension()> Function EndWhitespace(str As String) As WhitespaceString
If str.Length = 0 Then Return Nothing
Dim WhiteSpaceLen As Int16 = str.Length - str.TrimEnd.Length
Dim StartIndex%, Len%
StartIndex = (str.Length - WhiteSpaceLen)
Len = WhiteSpaceLen
Return New WhitespaceString(str.Substring(StartIndex, Len))
End Function
WhitespaceKind Enumeration هم که در پست شماره 8 هست و اینجا برای خصوصیت Length از WhitespaceString Structure بکار گرفته می شه.





پاسخ با نقل قول
