Hallo.
Microsoft hat uns ja mit dem StringBuilder eine performante Alternative zu normalen Strings gegeben. Leider findet sie (zumindest bei mir) nicht sehr oft den Einsatz, da einige wesentliche Funktionen von Strings fehlen. Da ich gerade nichts anderes zu tun hatte, habe ich da mal Abhilfe geschafft. Im Folgenden findet ihr eine ganze Reihe von Extension-Methoden, die von Strings bekannte Operationen auf einem StringBuilder durchführen.
VB
C#
Ich habe die Funktionen nicht alle getestet, weswegen sie möglicherweise kleine Fehler enthalten könnten (glaub ich zwar nicht, ich schreibs aber lieber mal dazu).
Außerdem ist der VB-Code nur aus dem C#-Code konvertiert und ich weiß nicht, ob der Converter richtig gearbeitet hat (wenn euch was auffällt sagts mir bitte).
Microsoft hat uns ja mit dem StringBuilder eine performante Alternative zu normalen Strings gegeben. Leider findet sie (zumindest bei mir) nicht sehr oft den Einsatz, da einige wesentliche Funktionen von Strings fehlen. Da ich gerade nichts anderes zu tun hatte, habe ich da mal Abhilfe geschafft. Im Folgenden findet ihr eine ganze Reihe von Extension-Methoden, die von Strings bekannte Operationen auf einem StringBuilder durchführen.
VB.NET-Quellcode
- Imports System.Collections.Generic
- Imports System.Linq
- Imports System.Text
- Imports System.Threading.Tasks
- Imports System.Runtime.CompilerServices
- Namespace Artentus
- Namespace Utils
- Namespace StringBuilderExtensions
- Public Module Extensions
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function StartsWith(sb As StringBuilder, value As String) As Boolean
- If sb.Length < value.Length Then
- Return False
- End If
- For i As Integer = 0 To charsSb.Length - 1
- If sb(i) <> value(i) Then
- Return False
- End If
- Next
- Return True
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function StartsWith(sb As StringBuilder, value As Char) As Boolean
- Return sb.Length > 0 AndAlso sb(0) = value
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function EndsWith(sb As StringBuilder, value As String) As Boolean
- If sb.Length < value.Length Then
- Return False
- End If
- For i As Integer = 0 To charsSb.Length - 1
- If charsSb((sb.Length - value.Length) + i) <> value(i) Then
- Return False
- End If
- Next
- Return True
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function EndsWith(sb As StringBuilder, value As Char) As Boolean
- Return sb.Length > 0 AndAlso sb(sb.Length - 1) = value
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function IndexOf(sb As StringBuilder, value As String, startIndex As Integer, count As Integer) As Integer
- Dim maxIndex = Math.Min(sb.Length - value.Length, startIndex + count)
- For i As Integer = startIndex To maxIndex - 1
- Dim found = True
- For j As Integer = 0 To value.Length - 1
- If sb(i + j) <> value(j) Then
- found = False
- Exit For
- End If
- Next
- If found Then
- Return i
- End If
- Next
- Return -1
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function IndexOf(sb As StringBuilder, value As Char, startIndex As Integer, count As Integer) As Integer
- Dim maxIndex = Math.Min(sb.Length, startIndex + count)
- For i As Integer = startIndex To maxIndex - 1
- If sb(i) = value Then
- Return i
- End If
- Next
- Return -1
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function IndexOf(sb As StringBuilder, value As String, startIndex As Integer) As Integer
- Return sb.IndexOf(value, startIndex, sb.Length - startIndex)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function IndexOf(sb As StringBuilder, value As Char, startIndex As Integer) As Integer
- Return sb.IndexOf(value, startIndex, sb.Length - startIndex)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function IndexOf(sb As StringBuilder, value As String) As Integer
- Return sb.IndexOf(value, 0, sb.Length)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function IndexOf(sb As StringBuilder, value As Char) As Integer
- Return sb.IndexOf(value, 0, sb.Length)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function LastIndexOf(sb As StringBuilder, value As String, startIndex As Integer, count As Integer) As Integer
- Dim minIndex = Math.Min(-1, startIndex - count)
- For i As Integer = startIndex To minIndex + 1 Step -1
- Dim found = True
- For j As Integer = 0 To value.Length - 1
- If sb(i + j) <> value(j) Then
- found = False
- Exit For
- End If
- Next
- If found Then
- Return i
- End If
- Next
- Return -1
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function LastIndexOf(sb As StringBuilder, value As Char, startIndex As Integer, count As Integer) As Integer
- Dim minIndex = Math.Max(-1, startIndex - count)
- For i As Integer = startIndex To minIndex + 1 Step -1
- If sb(i) = value Then
- Return i
- End If
- Next
- Return -1
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function LastIndexOf(sb As StringBuilder, value As String, startIndex As Integer) As Integer
- Return sb.LastIndexOf(value, startIndex, sb.Length - startIndex)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function LastIndexOf(sb As StringBuilder, value As Char, startIndex As Integer) As Integer
- Return sb.LastIndexOf(value, startIndex, sb.Length - startIndex)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function LastIndexOf(sb As StringBuilder, value As String) As Integer
- Return sb.LastIndexOf(value, 0, sb.Length)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function LastIndexOf(sb As StringBuilder, value As Char) As Integer
- Return sb.LastIndexOf(value, 0, sb.Length)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function PadLeft(sb As StringBuilder, totalWidth As Integer, paddingChar As Char) As StringBuilder
- While sb.Length < totalWidth
- sb.Insert(0, paddingChar)
- End While
- Return sb
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function PadLeft(sb As StringBuilder, totalWidth As Integer) As StringBuilder
- Return sb.PadLeft(totalWidth, " "C)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function PadRight(sb As StringBuilder, totalWidth As Integer, paddingChar As Char) As StringBuilder
- While sb.Length < totalWidth
- sb.Append(paddingChar)
- End While
- Return sb
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function PadRight(sb As StringBuilder, totalWidth As Integer) As StringBuilder
- Return sb.PadRight(totalWidth, " "C)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function SubString(sb As StringBuilder, startIndex As Integer, length As Integer) As StringBuilder
- Dim newSb = New StringBuilder(length - startIndex)
- Dim chars = New Char(length - 1) {}
- sb.CopyTo(startIndex, chars, 0, length)
- newSb.Append(chars)
- Return newSb
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function SubString(sb As StringBuilder, startIndex As Integer) As StringBuilder
- Return sb.SubString(startIndex, sb.Length - startIndex)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function TrimStart(sb As StringBuilder, trimChar As Char) As StringBuilder
- While sb.StartsWith(trimChar)
- sb.Remove(0, 1)
- End While
- Return sb
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function TrimEnd(sb As StringBuilder, trimChar As Char) As StringBuilder
- While sb.EndsWith(trimChar)
- sb.Remove(sb.Length - 1, 1)
- End While
- Return sb
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Trim(sb As StringBuilder, trimChar As Char) As StringBuilder
- While sb.StartsWith(trimChar)
- sb.Remove(0, 1)
- End While
- While sb.EndsWith(trimChar)
- sb.Remove(sb.Length - 1, 1)
- End While
- Return sb
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function ToCharArray(sb As StringBuilder) As Char()
- Dim chars = New Char(sb.Length - 1) {}
- sb.CopyTo(0, chars, 0, sb.Length)
- Return chars
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Replace(sb As StringBuilder, oldString As String, newString As String) As StringBuilder
- For i As Integer = sb.Length - oldString.Length To -1 + 1 Step -1
- If sb.SubString(i).StartsWith(oldString) Then
- sb.Remove(i, oldString.Length)
- sb.Insert(i, newString)
- End If
- Next
- Return sb
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Replace(sb As StringBuilder, oldChar As Char, newChar As Char) As StringBuilder
- For i As Integer = 0 To sb.Length - 1
- If sb(i) = oldChar Then
- sb(i) = newChar
- End If
- Next
- Return sb
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Split(sb As StringBuilder, separators As IEnumerable(Of String), count As Integer, options As StringSplitOptions) As StringBuilder()
- Dim parts = New List(Of StringBuilder)()
- Dim i = 0
- While i < sb.Length
- If count > -1 AndAlso count = parts.Count Then
- Exit While
- End If
- Dim builder = New StringBuilder()
- parts.Add(builder)
- While i < sb.Length
- Dim foundEnd = False
- For Each separator As var In separators
- If (i + separator.Length < sb.Length) AndAlso sb.SubString(i, separator.Length).StartsWith(separator) Then
- i += separator.Length
- foundEnd = True
- Exit For
- End If
- Next
- If Not foundEnd Then
- builder.Append(sb(i))
- i += 1
- End If
- End While
- End While
- Dim result = New List(Of StringBuilder)()
- For Each part As var In parts
- If Not ((options = StringSplitOptions.RemoveEmptyEntries) AndAlso (part.Length = 0)) Then
- result.Add(part)
- End If
- Next
- Return result.ToArray()
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Split(sb As StringBuilder, separators As IEnumerable(Of Char), count As Integer, options As StringSplitOptions) As StringBuilder()
- Dim parts = New List(Of StringBuilder)()
- Dim i = 0
- While i < sb.Length
- If count > -1 AndAlso count = parts.Count Then
- Exit While
- End If
- Dim builder = New StringBuilder()
- parts.Add(builder)
- While i < sb.Length
- Dim foundEnd = False
- For Each separator As var In separators
- If (i + 1 < sb.Length) AndAlso (sb.SubString(i, 1)(0) = separator) Then
- i += 1
- foundEnd = True
- Exit For
- End If
- Next
- If Not foundEnd Then
- builder.Append(sb(i))
- i += 1
- End If
- End While
- End While
- Dim result = New List(Of StringBuilder)()
- For Each part As var In parts
- If Not ((options = StringSplitOptions.RemoveEmptyEntries) AndAlso (part.Length = 0)) Then
- result.Add(part)
- End If
- Next
- Return result.ToArray()
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Split(sb As StringBuilder, separators As IEnumerable(Of String)) As StringBuilder()
- Return sb.Split(separators, -1, StringSplitOptions.None)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Split(sb As StringBuilder, separators As IEnumerable(Of Char)) As StringBuilder()
- Return sb.Split(separators, -1, StringSplitOptions.None)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Split(sb As StringBuilder, separator As String) As StringBuilder()
- Return sb.Split(New String() {separator}, -1, StringSplitOptions.None)
- End Function
- <System.Runtime.CompilerServices.Extension> _
- Public Shared Function Split(sb As StringBuilder, separator As Char) As StringBuilder()
- Return sb.Split(New Char() {separator}, -1, StringSplitOptions.None)
- End Function
- End Class
- End Namespace
- End Namespace
- End Namespace
Der C#-Code hat leider nicht mehr in den Post gepasst. Ich werde ihn noch nachreichen.
Ich habe die Funktionen nicht alle getestet, weswegen sie möglicherweise kleine Fehler enthalten könnten (glaub ich zwar nicht, ich schreibs aber lieber mal dazu).
Außerdem ist der VB-Code nur aus dem C#-Code konvertiert und ich weiß nicht, ob der Converter richtig gearbeitet hat (wenn euch was auffällt sagts mir bitte).
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Artentus“ ()