Skip to main content
Kofax

Remove all spaces or other characters from a string in a Validation or Recognition Script

1490

QAID # 1490 Published

Question / Problem:

How can I remove all spaces or other characters from a string in a Validation or Recognition Script?

Answer / Solution:

Below is a function that can loop through a string and remove all spaces in the string. This can also be applied to other characters by simply changing the character the function is looking for.

'***********************************************************
'*** Name: Remove_Spaces ***
'*** Date: 7/21/2003 ***
'*** Purpose: This function will remove all spaces ***
'*** from a given string. ***
'*** Input: sString is string to remove spaces from ***
'*** Output: returns string with no spaces ***
'***********************************************************

Function Remove_Spaces(sString as string) As String
        Dim iSearch As Integer     '*** Used to hold location in the string of space char
        Dim sNew As String         '*** String to return
        Dim sLeft As String        '*** Left side of string for concatenation
        Dim sRight As String       '*** Right side of string for concatenation

        sNew = sString                 '*** Assign the param string to the parsing string
        iSearch = InStr(1, sNew, " ")  '*** Get initial value of the first location of a space

        While iSearch                  '*** Loops until no spaces.
                                       '*** InStr returns 0 or False if no string found

        sLeft = Mid(sNew, 1, iSearch - 1)     '*** Get the left side of the found space
        sRight = Mid(sNew, iSearch + 1)       '*** Get the right side of the found space
        
        sNew = sLeft & sRight                 '*** Concatenate the left and right side. Note,
                                              ' this is done because Mid cannot change the
                                              ' the number of characters for a string it is
                                              ' replacing.
       
        isearch = InStr(1, sNew, " ")   '*** Get the next space.
    
    Wend
    
    Remove_Spaces = sNew
    
End Function

You would call the function slightly differently in a Recognition Script and a Validation Script.

In a Recognition Script, you would likely want to call the Remove_Spaces function in the KfxPostRecognition function, so that you can remove the spaces from the results that were already recognized.

In the KfxPostRecognition function, you would call the Remove_Spaces function as follows:

KfxValue = Remove_Spaces(KfxValue)

In a Validation Script, you would likely want to call the Remove_Spaces function in a Pre- or Post-field function, as:

KfxFieldName = Remove_Spaces(KfxFieldName)

(where FieldName would be replaced by the actual name of your Index Field) or

KfxFieldName = Remove_Spaces(EnteredValue)

(The possible use of EnteredValue depends on where in the Pre- or Post- function you call the Remove_Spaces function, and what type of Field Type that the Index Field uses.)

Applies to:

Product Version
CAPTURE 10.0
  10.1
  10.2
  11.0