Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Added new extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
AptiviCEO committed Feb 10, 2021
1 parent c06f092 commit 77b6b68
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Extensification.Tests/String.vb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ Public Class StringTests
Assert.AreEqual("Hello", TargetString.Reverse)
End Sub

''' <summary>
''' Tests reserving orders of characters in a string
''' </summary>
<TestMethod> Public Sub TestRepeat()
Dim TargetString As String = "Hi! "
Assert.AreEqual("Hi! Hi! Hi! ", TargetString.Repeat(3))
End Sub

''' <summary>
''' Tests removing null characters or whitespaces at the end of the string
''' </summary>
<TestMethod> Public Sub TestRemoveNullsOrWhitespacesAtTheEnd()
Dim TargetString As String = "Hi! "
TargetString.RemoveNullsOrWhitespacesAtTheEnd
Assert.AreEqual("Hi!", TargetString)
End Sub

''' <summary>
''' Tests removing null characters or whitespaces at the beginning of the string
''' </summary>
<TestMethod> Public Sub TestRemoveNullsOrWhitespacesAtTheBeginning()
Dim TargetString As String = " Hi!"
TargetString.RemoveNullsOrWhitespacesAtTheBeginning
Assert.AreEqual("Hi!", TargetString)
End Sub

#If NET45 Then
''' <summary>
''' Tests removing letters from a string
Expand Down
46 changes: 46 additions & 0 deletions Extensification/String.vb
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,52 @@ ParseSequence:
Return String.Join("", Str.ToCharArray.Reverse)
End Function

''' <summary>
''' Repeats a string 'n' times
''' </summary>
''' <param name="Str">Target string</param>
''' <param name="Times">Number of times to be repeated</param>
<Extension>
Public Function Repeat(ByVal Str As String, ByVal Times As Long) As String
If Str Is Nothing Then Throw New ArgumentNullException(NameOf(Str))
If Times <= 0 Then Throw New ArgumentException("Zero or negative times aren't allowed.", NameOf(Str))
Dim Target As String = ""
For i As Long = 1 To Times
Target += Str
Next
Return Target
End Function

''' <summary>
''' Removes null characters or whitespaces at the end of the string
''' </summary>
''' <param name="Str">Target string</param>
<Extension>
Public Sub RemoveNullsOrWhitespacesAtTheEnd(ByRef Str As String)
If Str Is Nothing Then Throw New ArgumentNullException(NameOf(Str))
Dim StrList As List(Of Char) = Str.ToCharArray.ToList
Dim CharIndex As Integer = Str.Length - 1
Do While String.IsNullOrWhiteSpace(StrList(CharIndex))
StrList.RemoveAt(CharIndex)
CharIndex -= 1
Loop
Str = String.Join("", StrList)
End Sub

''' <summary>
''' Removes null characters or whitespaces at the beginning of the string
''' </summary>
''' <param name="Str">Target string</param>
<Extension>
Public Sub RemoveNullsOrWhitespacesAtTheBeginning(ByRef Str As String)
If Str Is Nothing Then Throw New ArgumentNullException(NameOf(Str))
Dim StrList As List(Of Char) = Str.ToCharArray.ToList
Do While String.IsNullOrWhiteSpace(StrList(0))
StrList.RemoveAt(0)
Loop
Str = String.Join("", StrList)
End Sub

#If NET45 Then
''' <summary>
''' Evaluates a string
Expand Down

0 comments on commit 77b6b68

Please sign in to comment.