0% found this document useful (0 votes)
87 views

String Extension Methods 2

The document describes how to add string class extension methods in VB.NET. It defines several extension methods like GetFirstNumber, GetLastNumber, ReplaceFirst, and ReverseString. These extension methods extend the string class to add new functionality.

Uploaded by

info.glcom5161
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

String Extension Methods 2

The document describes how to add string class extension methods in VB.NET. It defines several extension methods like GetFirstNumber, GetLastNumber, ReplaceFirst, and ReverseString. These extension methods extend the string class to add new functionality.

Uploaded by

info.glcom5161
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

How to Add String Class Extension Methods ?

''StringExtension.vb

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Namespace StringExt
Public Module StringExtension
Sub New()

End Sub
<System.Runtime.CompilerServices.Extension()> _
Public Function GetFirstNumber(ByVal strInsput As String) As Integer
Dim number As Integer = 0
Dim strNumber As String = ""
Dim bIsContNo As Boolean = True
Dim bNoOccued As Boolean = False

Try
Dim arry = strInsput.ToCharArray(0, strInsput.Length - 1)

For Each item As Char In arry


If Char.IsNumber(item) Then
strNumber = strNumber & item.ToString()

bIsContNo = True

bNoOccued = True
Else
bIsContNo = False
End If

If bNoOccued AndAlso Not bIsContNo Then


Exit For

End If
Next

number = Convert.ToInt32(strNumber)
Catch ex As Exception

Return 0
End Try

Return number

End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function GetLastNumber(ByVal strInsput As String) As Integer
Dim number As Integer = 0
Dim strNumber As String = ""
Dim bIsContNo As Boolean = True
Dim bNoOccued As Boolean = False
Dim strLastNo As String = ""

Try
Dim arry = strInsput.ToCharArray(0, strInsput.Length)

For Each item As Char In arry


If Char.IsNumber(item) Then
strNumber = strNumber & item.ToString()

bIsContNo = True

bNoOccued = True

strLastNo = strNumber
Else
bIsContNo = False
End If

If bNoOccued AndAlso Not bIsContNo Then


strLastNo = strNumber
strNumber = ""
bNoOccued = False

End If
Next

number = Convert.ToInt32(strLastNo)
Catch ex As Exception

Return 0
End Try

Return number

End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function GetIndexsInString(ByVal strInput As String, ByVal SubString
As String) As Integer()
Dim Index As Integer() = New Integer(1) {}

If strInput.Contains(SubString) Then
For startIndex As Integer = 0 To strInput.Length - 1
If strInput.Substring(startIndex, SubString.Length) = SubString
Then
Index(0) = startIndex
Index(1) = startIndex + SubString.Length - 1

Exit For
End If
Next
End If

Return Index
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function RemoveString(ByVal strInput As String, ByVal StringValue As
String) As String
Dim strResult As String = strInput

Dim Index As Integer() = New Integer(1) {}

Index = strInput.GetIndexsInString(StringValue)

If Index(0) <> Index(1) Then


strResult = strInput.Remove(Index(0), (Index(1) - Index(0)) + 1)
End If

Return strResult
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function ReplaceFirst(ByVal strInput As String, ByVal strOldString
As String, ByVal strNewString As String) As String
Dim strResult As String = strInput

If strInput.Contains(strOldString) Then
Dim Index As Integer() = New Integer(1) {}

Index = strInput.GetIndexsInString(strOldString)

If Index(0) <> Index(1) Then


Dim LowerHalfString As String = strInput.Substring(0, Index(0))
Dim UpperHalfString As String = strInput.Substring(Index(1) +
1, (strInput.Length - (LowerHalfString.Length + strOldString.Length)))

strResult = LowerHalfString & strNewString & UpperHalfString


End If
End If

Return strResult
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function ToSingleQuote(ByVal strInput As String) As String
Return InlineAssignHelper(strInput, "'" & strInput & "'")
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function UpperCaseFirstLetters(ByVal strInput As String) As String
Dim strResult As String = ""

Dim Temp As String = ""

Dim bUpperCaseNext As Boolean = False

For Index As Integer = 0 To strInput.Length - 1

If [Char].IsWhiteSpace(strInput(Index)) OrElse 0 = Index Then

Temp = Convert.ToString(strInput(Index))

If 0 = Index Then

Temp = Temp.ToUpper()
Else
bUpperCaseNext = True
End If

strResult = strResult & Temp


Else
Temp = Convert.ToString(strInput(Index))

If bUpperCaseNext Then
Temp = Temp.ToUpper()

bUpperCaseNext = False
End If

strResult = strResult & Temp


End If
Next

Return strResult
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function SortWordAlphabatical(ByVal strInput As String) As String
Dim strResult As String = ""

Dim array As String() = strInput.Split(" "c)

System.Array.Sort(Of String)(array)

For Each c In array


strResult = strResult & " " & c
Next

Return strResult
End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function ReverseString(ByVal strInput As String) As String

Dim arr As Char() = strInput.ToCharArray()


Array.Reverse(arr)
Return New String(arr)

End Function

<System.Runtime.CompilerServices.Extension()> _
Public Function ReverseWords(ByVal strInput As String) As String
Dim strResult As String = ""

Dim array As String() = strInput.Split(" "c)

For Each c In array

strResult = strResult & " " & ReverseString(c)


Next

Return strResult
End Function
Private Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As
T) As T
target = value
Return value
End Function
End Module
End Namespace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
''Module1.vb

Imports StringExtensionTest.StringExt
Module Module1

Sub Main()
Dim MyString As String = "this was the 1 original string i made on 7 7
2015"

Console.WriteLine(MyString)

Console.WriteLine("This is first number in string :{0}",


MyString.GetFirstNumber())

Console.WriteLine("This is Last number in string :{0}",


MyString.GetLastNumber())

Dim Index As Integer() = New Integer(1) {}

Index = MyString.GetIndexsInString("original")

Console.WriteLine("This is Indexes of original in string LowerIndex:


{0},UpperIndex:{01}", Index(0), Index(1))

Console.WriteLine("MyString before replacing original with real:")


Console.WriteLine(MyString)

MyString = MyString.ReplaceFirst("original", "real")

Console.WriteLine("MyString After replacing original with real:")


Console.WriteLine(MyString)

Console.WriteLine("MyString Remove:{0}", MyString.RemoveString("real"))

Console.WriteLine("MyString UpperCaseFirstLetters:{0}",
MyString.UpperCaseFirstLetters())

Console.WriteLine("MyString ToSingleQuote:{0}", MyString.ToSingleQuote())

Console.WriteLine("MyString SortWordAlphabatical:{0}",
MyString.SortWordAlphabatical())

Console.WriteLine("MyString SortWordAlphabatical:{0}",
MyString.ReverseString())

Console.ReadLine()

End Sub

End Module

You might also like