Replace All Occurences of Numbers in the Input String

Replace All Occurences of Numbers in the Input String

This function replaces all occurences of numbers in the input string with ‘#‘ and returns the replaced string. It recognizes integers, floating point, and negative numbers. It will not replace digits that are actually part of a word (eg: ‘var5‘ or ‘level9code‘). Special care is taken for numbers in formats like ‘=45‘.

The function keeps scanning the string for ” ” (a space) and then extracts the word before it. If the word is numeric, it replaces that word with ‘#‘. Special care is taken for last word which doesn’t have a space at its end.

'Replaces all occurences of numbers from a string with "#"Function ReplaceNumbers(sample As String) As StringDim fromx As IntegerDim pos As LongDim tmp As Stringfromx = 1Do While fromx < Len(sample)'To recognise numbers of the form "=45"If Mid(sample, fromx, 1) = "=" Then fromx = fromx + 1pos = InStr(fromx, sample, " ", vbTextCompare)If pos <> 0 Then  tmp = Mid(sample, fromx, pos - fromx)  If IsNumeric(tmp) Then     sample = Replace(sample, tmp, "#", 1, 1, vbTextCompare)  End IfElse  Exit DoEnd Iffromx = pos + 1Loop'For last wordIf Mid(sample, fromx, 1) = "=" Then fromx = fromx + 1tmp = Mid(sample, fromx, Len(sample))If IsNumeric(tmp) Then     sample = Replace(sample, tmp, "#", 1, 1, vbTextCompare)End If  ReplaceNumbers = sampleEnd Function
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several