Home » Programming » C# » Useful extension methods for the String .Net class and string manipulation, with support of IntelliSense

Useful extension methods for the String .Net class and string manipulation, with support of IntelliSense

posted in: C#, Programming, VB.Net 0

Hi to all, I wrote this .NET module that extends String class with useful methods to modify alphanumeric characters and symbols. It fully integrates with the compiler so You are able to recognize extension methods by IntelliSense; in the bottom of the article, see the module’s code.

This is an example of what you can do:

[sourcecode language=”vb”] ‘remove accent marks (ex. "à" to "a")
Dim accents As String = "àèìòù"
accents = accents.StripAccentMarks()

‘remove tags (ex. "<b>test</b>" to "test")
Dim tags As String = "<b>test</b>"
tags = tags.StripTags()

‘remove symbols from the string but leave "," and ";" then trim the spaces
Dim s As String = " hi, this   is a test;   5 is a number! – up-to-date example.   "
Dim symbols_exceptions() As Char = {",", ";"}
Dim new_string As String = s.StripNonAlphanumeric(Nothing, symbols_exceptions).TrimSpaces

‘remove punctuation but not hyphens in words
Dim h As String = "punctuation and hyphens, as ‘-‘, will be deleted but not hyphens in words like ‘up-to-date’."
Dim filtered As String = h.StripPunctuation(True)

‘padding strings…
Dim mystring1 As String = "max"
Dim mystring2 As String = "vergelli"
Console.WriteLine(mystring1.Pad(10, ".") & vbCrLf & mystring2.Pad(10, "."))
‘it will print
‘   "…….max"
‘   "..vergelli"

‘padding integers…
Dim mynumber1 As Integer = 10
Dim mynumber2 As Integer = 1000
Console.WriteLine(mynumber1.Pad(10, "0") & vbCrLf & mynumber2.Pad(10, "0"))
‘it will print
‘   "0000000010"
‘   "0000001000"

‘get the first 3 characters of the string
Dim ls As String = s.Left(3)

‘get the last 3 characters of the string
Dim rs As String = s.Right(3)

‘get characters in the middle of the string
Dim ms As String = s.Mid(5, 3)

‘get only the symbols
Dim symbols As String = s.StripAlphanumeric()

‘leave only one space between words but it does not trim spaces at start and at end of the string
symbols = symbols.TrimSpaces(True, True)

‘get only the numbers
Dim numbers As String = s.StripNonNumeric()

Dim n As String = "5"
If n.IsNumeric Then Console.WriteLine("’n’ variable contains a numeric value.")

‘convert a string to an integer…
n = n.ToInteger()
[/sourcecode]

How to install the module:

all that is required to be able to run these extension methods is that they be in scope, therefore  You have to import this module in your classes with the header “Imports [Project’s name].Extensions”, so all methods will be visible also in IntelliSense.

Notes:

  • All the methods do not change the value of the current instance, instead, they return always a new string.
  • Only an instance of String can use the these methods, any other class trying to use the string extension methods will throw a compilation error.
  • If an extension method is called for an object that is set to Nothing, the extension method executes. This does not apply to ordinary instance methods. You can explicitly check for Nothing in the extension method.
  • Notice that when the extension methods are invoked, You have to omit the first parameter; Why no argument is sent in for the first parameter? The first parameter in the method definitions is only bound to caller object (the instance of String that calls them), so the compiler will treat the argument in the first parameter (named “instance_handler”) as the caller object.

Finally, the source code of the module, enjoy!…  🙂

[sourcecode language=”vb”] ‘    StringExtensions VB.NET module
‘    https://www.maxvergelli.com/

‘    Copyright (c) 2016 Max Vergelli

‘   Vers. 3.0.0
‘   Date: 22/03/2016

‘   Description:
‘   .NET module that extends String class with useful methods to modify
‘   alphanumeric characters and symbols. It fully integrates with the compiler
‘   so You are able to recognize extension methods by IntelliSense.


‘   License:

‘    Permission is hereby granted, free of charge, to any person obtaining a copy
‘    of this software and associated documentation files (the "Software"), to deal
‘    in the Software without restriction, including without limitation the rights
‘    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
‘    copies of the Software, and to permit persons to whom the Software is
‘    furnished to do so, subject to the following conditions:

‘    The above copyright notice and this permission notice shall be included in
‘    all copies or substantial portions of the Software.

‘    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
‘    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
‘    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
‘    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
‘    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
‘    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
‘    THE SOFTWARE.


‘   Installation:

‘   All that is required to be able to run these extension methods is that they be in scope, therefore
‘   You have to import this module in your classes with the header "Imports [Project’s name].Extensions",
‘   so all methods will be visible also in IntelliSense.


‘   Notes:

‘   1) All the methods do not change the value of the current instance, instead, they return always a new string.

‘   2) Only an instance of String can use the these methods, any other class trying to use the string extension
‘      methods will throw a compilation error.

‘   3) If an extension method is called for an object that is set to Nothing, the extension method executes. This
‘      does not apply to ordinary instance methods. You can explicitly check for Nothing in the extension method.

‘   4) Notice that when the extension methods are invoked, You have to omit the first parameter;
‘      Why no argument is sent in for the first parameter?
‘      The first parameter in the method definitions is only bound to caller object (the instance of String that
‘      calls them), so the compiler will treat the argument in the first parameter (named "instance_handler") as
‘      the caller object.


‘   Usage:

‘   Imports {Your project’s name}.Extensions

‘   Public Class Test
‘       Public Sub TestExtensions()

‘        ‘remove accent marks (ex. "à" to "a")
‘        Dim accents As String = "àèìòù"
‘        accents = accents.StripAccentMarks()

‘        ‘remove tags (ex. "<b>test</b>" to "test")
‘        Dim tags As String = "<b>test</b>"
‘        tags = tags.StripTags()

‘        ‘remove symbols from the string but leave "," and ";" then trim the spaces
‘        Dim s As String = " hi, this   is a test;   5 is a number! – up-to-date example.   "
‘        Dim symbols_exceptions() As Char = {",", ";"}
‘        Dim new_string As String = s.StripNonAlphanumeric(Nothing, symbols_exceptions).TrimSpaces

‘        ‘remove punctuation but not hyphens in words
‘        Dim h As String = "punctuation and hyphens, as ‘-‘, will be deleted but not hyphens in words like ‘up-to-date’."
‘        Dim filtered As String = h.StripPunctuation(True)

‘        ‘padding strings…
‘        Dim mystring1 As String = "max"
‘        Dim mystring2 As String = "vergelli"
‘        Console.WriteLine(mystring1.Pad(10, ".") & vbCrLf & mystring2.Pad(10, "."))
‘        ‘it will print
‘        ‘   "…….max"
‘        ‘   "..vergelli"

‘        ‘padding integers…
‘        Dim mynumber1 As Integer = 10
‘        Dim mynumber2 As Integer = 1000
‘        Console.WriteLine(mynumber1.Pad(10, "0") & vbCrLf & mynumber2.Pad(10, "0"))
‘        ‘it will print
‘        ‘   "0000000010"
‘        ‘   "0000001000"

‘        ‘get the first 3 characters of the string
‘        Dim ls As String = s.Left(3)

‘        ‘get the last 3 characters of the string
‘        Dim rs As String = s.Right(3)

‘        ‘get characters in the middle of the string
‘        Dim ms As String = s.Mid(5, 3)

‘        ‘get only the symbols
‘        Dim symbols As String = s.StripAlphanumeric()

‘        ‘leave only one space between words but it does not trim spaces at start and at end of the string
‘        symbols = symbols.TrimSpaces(True, True)

‘        ‘get only the numbers
‘        Dim numbers As String = s.StripNonNumeric()

‘        Dim n As String = "5"
‘        If n.IsNumeric Then Console.WriteLine("’n’ variable contains a numeric value.")

‘        ‘convert a string to an integer…
‘        n = n.ToInteger()

‘       End Sub
‘   End Class

Imports System.Runtime.CompilerServices
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Web

Namespace Extensions

Module StringExtensions

<Extension()> _
Public Function IsNumeric(ByRef instance_handler As Object) As Boolean
Dim is_numeric As Boolean = False
‘Define variable to collect out parameter of the TryParse method.
‘If the conversion fails, the out parameter is zero.
Dim tmp As Double
‘The TryParse method converts a string in a specified style and culture-specific format to its
‘double-precision floating point number equivalent.
‘The TryParse method does not generate an exception if the conversion fails. If the conversion passes,
‘True is returned. If it does not, False is returned.
is_numeric = [Double].TryParse(Convert.ToString(instance_handler), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, tmp)
Return is_numeric
End Function

<Extension()> _
Public Function ToInteger(ByRef instance_handler As String) As Integer
Return Integer.Parse(instance_handler)
End Function

<Extension()> _
Public Function Left(ByRef instance_handler As String, length As Integer) As String
Return instance_handler.Substring(0, length)
End Function

<Extension()> _
Public Function Right(ByRef instance_handler As String, length As Integer) As String
Return instance_handler.Substring(instance_handler.Length – length, length)
End Function

<Extension()> _
Public Function Mid(ByRef instance_handler As String, start_index As Integer, length As Integer) As String
Return instance_handler.Substring(start_index, length)
End Function

<Extension()> _
Public Function StripNonAlphanumeric(ByRef instance_handler As String, _
Optional ByVal replace_nonalphanumeric_with As String = Nothing, _
Optional ByVal nonalphanumeric_exceptions() As Char = Nothing) As String
Dim h As String = instance_handler.Trim
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To h.Length – 1
c = h.Chars(i)
If Char.IsLetterOrDigit(c) = True Then
s.Append(c)
Else
If nonalphanumeric_exceptions IsNot Nothing AndAlso nonalphanumeric_exceptions.Contains(c) Then
s.Append(c)
ElseIf replace_nonalphanumeric_with IsNot Nothing Then
s.Append(replace_nonalphanumeric_with)
End If
End If
Next
Return s.ToString
End Function

<Extension()> _
Public Function StripAlphanumeric(ByRef instance_handler As String) As String
Dim h As String = instance_handler.Trim
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To h.Length – 1
c = h.Chars(i)
If Char.IsLetterOrDigit(c) = False Then
s.Append(c)
End If
Next
Return s.ToString
End Function

<Extension()> _
Public Function StripAlphabetic(ByRef instance_handler As String) As String
Dim h As String = instance_handler.Trim
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To h.Length – 1
c = h.Chars(i)
If Char.IsLetter(c) = False Then
s.Append(c)
End If
Next
Return s.ToString
End Function

<Extension()> _
Public Function StripNumeric(ByRef instance_handler As String) As String
Dim h As String = instance_handler.Trim
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To h.Length – 1
c = h.Chars(i)
If Char.IsDigit(c) = False Then
s.Append(c)
End If
Next
Return s.ToString
End Function

<Extension()> _
Public Function StripNonNumeric(ByRef instance_handler As String) As String
Dim h As String = instance_handler.Trim
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To h.Length – 1
c = h.Chars(i)
If Char.IsDigit(c) = True Then
s.Append(c)
End If
Next
Return s.ToString
End Function

<Extension()> _
Public Function StripPunctuation(ByRef instance_handler As String, _
Optional ByVal preserve_only_hyphenated_words As Boolean = False, _
Optional ByVal punctuation_exceptions() As Char = Nothing) As String
Dim h As String = instance_handler.Trim
Dim s As New StringBuilder()
Dim c As Char
Dim hyphens As List(Of Char) = New List(Of Char)(New Char() {"_"c, "-"c, "‐"c, "‒"c, "–"c, "—"c, "―"c})
For i As Integer = 0 To h.Length – 1
c = h.Chars(i)
If Char.IsPunctuation(c) = False Or (punctuation_exceptions IsNot Nothing AndAlso punctuation_exceptions.Contains(c)) Then
s.Append(c)
ElseIf preserve_only_hyphenated_words = True AndAlso hyphens.Contains(c) _
AndAlso i – 1 > -1 AndAlso i + 1 < h.Length _
AndAlso Char.IsLetterOrDigit(h.Chars(i – 1)) AndAlso Char.IsLetterOrDigit(h.Chars(i + 1)) Then
s.Append(c)
End If
Next
Return s.ToString
End Function

<Extension()> _
Public Function StripSpaces(ByRef instance_handler As String, Optional ByVal replace_space_with As String = "") As String
Dim h As String = instance_handler.Trim
Dim r As New Regex("(s+)", RegexOptions.Compiled And RegexOptions.IgnoreCase And RegexOptions.Singleline)
Dim s As String = r.Replace(h, replace_space_with)
r = Nothing
Return s
End Function

<Extension()> _
Public Function TrimSpaces(ByRef instance_handler As String, _
Optional StripSpaceAtStart As Boolean = True, _
Optional StripSpaceAtEnd As Boolean = True) As String
Dim h As String = instance_handler.Trim
Dim r As New Regex("(s+)", RegexOptions.Compiled And RegexOptions.IgnoreCase And RegexOptions.Singleline)
Dim s As String = r.Replace(h, " ")
If StripSpaceAtStart = True Then
s = s.TrimStart(" "c)
End If
If StripSpaceAtEnd = True Then
s = s.TrimEnd(" "c)
End If
r = Nothing
Return s
End Function

<Extension()> _
Public Function ReplaceGermanUmlauts(ByRef instance_handler As String) As String
Dim s As String = instance_handler
s = s.Replace("ä", "ae")
s = s.Replace("ö", "oe")
s = s.Replace("ü", "ue")
s = s.Replace("Ä", "Ae")
s = s.Replace("Ö", "Oe")
s = s.Replace("Ü", "Ue")
s = s.Replace("ß", "ss")
Return s
End Function

<Extension()> _
Public Function StripAccentMarks(ByRef instance_handler As String) As String
Dim n_s As String = instance_handler.Normalize(NormalizationForm.FormD)
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To n_s.Length – 1
c = Convert.ToChar(n_s(i))
If System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) <> System.Globalization.UnicodeCategory.NonSpacingMark Then
s.Append(c)
End If
Next
Return s.ToString
End Function

<Extension()> _
Public Function StripTags(ByRef instance_handler As String) As String
Dim b As Boolean = False
Dim s As New StringBuilder()
Dim c As Char
For i As Integer = 0 To instance_handler.Length – 1
c = instance_handler.Chars(i)
If c = "<"c Then b = True
If Not b Then s.Append(c)
If c = ">"c Then b = False
Next
Return s.ToString
End Function

<Extension()> _
Public Function SpecialCharsToHtmlEntities(ByRef instance_handler As String) As String
Return HttpUtility.HtmlEncode(instance_handler)
End Function

<Extension()> _
Public Function HtmlEntitiesToSpecialChars(ByRef instance_handler As String) As String
Return HttpUtility.HtmlDecode(instance_handler)
End Function

<Extension()> _
Public Function Pad(ByRef instance_handler As String, ByVal length As Integer, Optional ByVal padding_char As Char = " "c) As String
Dim s As String = String.Empty
If length > instance_handler.Length Then
s = instance_handler.PadLeft(length, padding_char)
Else
s = instance_handler.Substring(length – 1)
End If
Return s
End Function

<Extension()> _
Public Function Pad(ByRef instance_handler As Integer, ByVal length As Integer, Optional ByVal padding_char As Char = "0"c) As String
Dim s As String = String.Empty
Dim nb As String = instance_handler.ToString
If length > nb.Length Then
s = nb.PadLeft(length, padding_char)
Else
s = nb.Substring(length – 1)
End If
Return s
End Function

End Module

End Namespace
[/sourcecode]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.