Daily Archives: November 24, 2020
VB.NET || How To Remove Excess Whitespace From A String Using VB.NET
The following is a module with functions which demonstrates how to remove excess whitespace from a string, replacing multiple spaces into just one using VB.NET.
1. Remove Excess Whitespace
The example below demonstrates the use of ‘Utils.ToSingleSpace‘ to remove excess whitespace from a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
' Remove Excess Whitespace Imports Utils Dim word = " This document uses 3 other documents " ' Creates single space string Dim result = word.ToSingleSpace Debug.Print(result) ' expected output: ' This document uses 3 other documents |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Removes excess whitespace from a string ''' </summary> ''' <param name="source">The source string</param> ''' <returns>The modified source string</returns> <Runtime.CompilerServices.Extension()> Public Function ToSingleSpace(source As String) As String Dim pattern = "\s\s+" Dim result = System.Text.RegularExpressions.Regex.Replace(source, pattern, " ").Trim Return result End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils Public Module Program Sub Main(args As String()) Try Dim word = " This document uses 3 other documents " ' Creates single space string Dim result = word.ToSingleSpace Display(result) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Remove All Whitespace From A String Using VB.NET
The following is a module with functions which demonstrates how to remove all whitespace from a string using VB.NET.
1. Remove All Whitespace
The example below demonstrates the use of ‘Utils.RemoveWhitespace‘ to remove all whitespace from a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
' Remove All Whitespace Imports Utils Dim word = " This document uses 3 other documents " ' Remove whitespace from the string Dim result = word.RemoveWhitespace Debug.Print(result) ' expected output: ' Thisdocumentuses3otherdocuments |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Removes all whitespace from a string ''' </summary> ''' <param name="source">The source string</param> ''' <returns>The modified source string</returns> <Runtime.CompilerServices.Extension()> Public Function RemoveWhitespace(source As String) As String Dim pattern = "\s" Dim result = System.Text.RegularExpressions.Regex.Replace(source, pattern, String.Empty) Return result End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils Module Program Sub Main(args As String()) Try Dim word = "This document uses 3 other documents " ' Remove whitespace from the string Dim result = word.RemoveWhitespace Display(result) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Remove Non Alphanumeric Characters From A String Using VB.NET
The following is a module with functions which demonstrates how to remove and replace non alphanumeric characters from a string using VB.NET.
1. Alphanumeric Characters
The example below demonstrates the use of ‘Utils.ToAlphaNumeric‘ to create an alphanumeric string.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
' Alphanumeric Characters Imports Utils Dim word = "This-document_uses-3#other@documents!!!" ' Create alpha numeric string Dim result = word.ToAlphaNumeric Debug.Print(result) ' expected output: ' This document uses 3 other documents |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Returns the string as alphanumeric ''' </summary> ''' <param name="source">The source string</param> ''' <param name="replacement">The string replacement for invalid characters</param> ''' <returns>The modified source string</returns> <Runtime.CompilerServices.Extension()> Public Function ToAlphaNumeric(source As String, Optional replacement As String = " ") As String Dim pattern = "[^a-zA-Z0-9]+" Dim result = System.Text.RegularExpressions.Regex.Replace(source, pattern, replacement).Trim Return result End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils Module Program Sub Main(args As String()) Try Dim word = "This-document_uses-3#other@documents!!!" ' Create alpha numeric string Dim result = word.ToAlphaNumeric Display(result) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Replace Entire Words In A String Using VB.NET
The following is a module with functions which demonstrates how to replace entire words in a string using VB.NET.
This function is different from String.Replace in that instead of replacing all substring instances in a word, it replaces all instances of matching entire words in a string.
1. Replace Entire Word
The example below demonstrates the use of ‘Utils.ReplaceWord‘ to replace an entire word.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
' Replace Entire Word Imports Utils Dim word = "This docment uses 3 other docments to docment the docmentation" ' Replace the entire word in the string Dim result = word.ReplaceWord("docment", "document") Debug.Print(result) ' expected output: ' This document uses 3 other docments to document the docmentation |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Replaces the entire word in the string ''' </summary> ''' <param name="source">The source string</param> ''' <param name="wordToReplace">The word to replace</param> ''' <param name="replacementWord">The replacement word</param> ''' <param name="regexOptions">The search options</param> ''' <returns>The modified source string</returns> <Runtime.CompilerServices.Extension()> Public Function ReplaceWord(source As String, wordToReplace As String, replacementWord As String _ , Optional regexOptions As System.Text.RegularExpressions.RegexOptions = System.Text.RegularExpressions.RegexOptions.None) As String Dim pattern = $"\b{System.Text.RegularExpressions.Regex.Escape(wordToReplace)}\b" Dim result = System.Text.RegularExpressions.Regex.Replace(source, pattern, replacementWord, regexOptions) Return result End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Imports Utils Module Program Sub Main(args As String()) Try Dim word = "This docment uses 3 other docments to docment the docmentation" ' Replace the entire word in the string Dim result = word.ReplaceWord("docment", "document") Display(result) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
VB.NET || How To Manually Copy Two Streams From One To Another Using VB.NET
The following is a module with functions which demonstrates how to manually copy two steams from one to another using VB.NET.
1. Manually Copy Stream
The example below demonstrates the use of ‘Utils.CopyStream‘ to copy two streams.
1 2 3 4 5 6 7 |
' Manually Copy Stream Dim stream1 As System.IO.Stream Dim stream2 As System.IO.Stream ' Copy stream1 to stream2 Utils.CopyStream(stream1, stream2) |
2. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils ''' <summary> ''' Reads bytes from the 'streamFrom' and writes them to 'streamTo' ''' </summary> ''' <param name="streamFrom">The source stream</param> ''' <param name="streamTo">The destination stram</param> ''' <returns>The total bytes written</returns> <Runtime.CompilerServices.Extension()> Public Function CopyStream(streamFrom As System.IO.Stream, streamTo As System.IO.Stream, Optional bufferSize As Integer = 4096) As Integer Dim bytesWritten As Integer = 0 Dim bytBuffer(bufferSize - 1) As Byte Dim bytesRead As Integer = 0 Dim tempPos = If(streamFrom.CanSeek, CType(streamFrom.Position, Integer?), Nothing) If streamFrom.CanSeek Then streamFrom.Position = 0 Do bytesRead = streamFrom.Read(bytBuffer, 0, bytBuffer.Length) If bytesRead > 0 Then streamTo.Write(bytBuffer, 0, bytesRead) bytesWritten += bytesRead End If Loop While bytesRead > 0 If tempPos.HasValue Then streamFrom.Position = tempPos.Value Return bytesWritten End Function End Module End Namespace ' http://programmingnotes.org/ |
3. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 24, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Module Program Sub Main(args As String()) Try Dim stream1 As System.IO.Stream Dim stream2 As System.IO.Stream ' Copy stream1 to stream2 Utils.CopyStream(stream1, stream2) Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.