|
|
CODE '==============================================================================
' ReplaceViaPosition ' http://www.utteraccess.com/wiki/index.php/ReplaceViaPosition ' Code courtesy of UtterAccess Wiki ' Licensed under Creative Commons License ' http://creativecommons.org/licenses/by-sa/3.0/ ' ' You are free to use this code in any application, ' provided this notice is left unchanged. ' ' ' DATE: 7/1/2011 ' REVISION: 0 (initial release) ' DESCRIPTION: ' An extended version of the Replace() function. This function allows us to ' add, replace or remove text within a string given a start position and ' selection length. ' ' RETURNS: String, modified string per parameters, or the orginal string ' on errors ' ' ARGUMENTS: ' strText: The original text to modify ' intStart: The start position to enter or replace text (0 based) ' intLen: The length of the selection to replace (0 based) (default 0) ' strAdd: The text to add to the string (default ZLS) ' bPadAddition: True to pad the added text with a space (default False) ' If the addition is at the start of the string no leading ' space will be added. ' If the padding results in two consecutive spaces the ' consecutive space will be removed ' Padding operations will be bypassed if strAdd is a ZLS ' ' USAGE NOTES: ' The intStart and intLen arguments work like a textbox's SelStart ' and SelLength properties: if intStart is 0 then strAdd will be added ' to the beginning of the string, and if intLen is 0 then none of the ' original string will be replaced, only added. ' ' If you want to add a some text to the string, use the strAdd argument. ' If you want to remove text from the string based on intStart and intLen ' pass strAdd as a Zero Length String ' ' USAGE EXAMPLES: ' ?ReplaceViaPosition("This is my string", 0, 0, "Yay!", False) ' Yay!This is my string ' ' ?ReplaceViaPosition("This is my string", 0, 0, "Yay!", True) ' Yay! This is my string ' ' ?ReplaceViaPosition("This is my string", 2, 0, "Yay!", False) ' ThYay!is is my string ' ' ?ReplaceViaPosition("This is my string", 2, 0, "Yay!", True) ' Th Yay! is is my string ' ' ?ReplaceViaPosition("This is my string", 2, 2, "Yay!", False) ' ThYay! is my string ' ' ?ReplaceViaPosition("This is my string", 2, 2, "Yay!", True) ' Th Yay! is my string ' ' ?ReplaceViaPosition("This is my string", 1, 2, "") ' Ts is my string ' ' DEPENDANCIES: ' VBA (none) '============================================================================== 'ErrStrV3.2 Public Function ReplaceViaPosition( _ ByVal strText As String, _ intStart As Integer, _ Optional intLen As Integer = 0, _ Optional strAdd As String = "", _ Optional bPadAddition As Boolean = False _ ) As String On Error GoTo Error_Proc Dim Ret As String '========================= Dim strOriginal As String '========================= 'retain the original in case of error strOriginal = strText 'validate the position and length to make sure 'they're within an appropriate range If ((intStart) > Len(strText)) Or _ (intStart + intLen) > Len(strText) Then 'bad entry, let's raise error 9: Subscript out of range Err.Raise 9, "ReplaceViaPosition", "Subscript out of range" End If 'remove any text that is selected due to intLen If intLen > 0 Then strText = Left(strText, intStart) & Mid(strText, intStart + intLen + 1) End If If Len(strAdd) <> 0 Then 'add strAdd to the string in the specified position strText = Left(strText, intStart) & strAdd & Mid(strText, intStart + 1) 'perform the padding if required If bPadAddition Then 'work on the leading space first 'if we're adding to the start of the string we don't need 'any leading spaces If intStart > 0 Then 'find out if the char before the addition is a space If Mid(strText, intStart, 1) <> " " Then 'it's not a space, we'll need to enter one strText = Left(strText, intStart) & " " & Mid(strText, intStart + 1) 'we added a space, so let's increase our intStart by one 'to compensate for the next check intStart = intStart + 1 End If End If 'work on the trailing space next 'find out if the char after the addition is a space If Mid(strText, intStart + 1 + Len(strAdd), 1) <> " " Then 'it's not a space, we'll need to enter one 'but first check if it's the end of the text... If (intStart + Len(strAdd)) < Len(strText) Then 'ok, we're safe to add a space after the addition strText = Left(strText, intStart + Len(strAdd)) & " " & Mid(strText, intStart + Len(strAdd) + 1) End If End If End If End If Ret = strText '========================= Exit_Proc: ReplaceViaPosition = Ret Exit Function Error_Proc: Ret = strOriginal MsgBox "Error: " & Trim(str(Err.Number)) & vbCrLf & _ "Desc: " & Err.Description & vbCrLf & vbCrLf & _ "Module: modCmnUtilStrings, Procedure: ReplaceViaPosition" _ , vbCritical, "Error!" Resume Exit_Proc Resume End Function
|
| This page has been accessed 976 times. This page was last modified 21:18, 1 July 2011 by Jack Leach. Disclaimers |