Word Extract via LotusScript Code

This LotusScript function will extract a word from a text string much the same way the @WORD formula does. The function has these 3 parameters: the text string to be searched, the character to be used as a delimeter, the occurance of that delimeter.
Function Field(InputStr As String, DelimChr As String, PosNo As Integer)
	'The DelimChr must be a single character or else function will return an error
	MaxLen = Len(InputStr)
	ErrMsg = ""
	If PosNo < 1 Then
		ErrMsg = "Starting position < 1"
	Elseif DelimChr = "" Then
		ErrMsg = "Delimeter is null"
	Elseif Len(DelimChr) > 1  Then
		ErrMsg = "Delimeter is > 1 character"
	Elseif MaxLen < 1 Then
		Field = ""
		Exit Function	'No input string passed to function - just return an empty string - not considered an error
	End If
	If ErrMsg <> "" Then
		Msgbox "Error: Invalid parameter!" + Chr(13) + Chr(13) + ErrMsg,16,"Field Function"
		Field = ""
		Exit Function
	End If
	DelimCnt = 0	'Keeps track of how many delimiters were found
	StartSrchPos = 1	'Initialize where to start looking in the string
	StartDelimPos = 0	'Will hold the physical postion where the desired delimiter was found
	EndDelimPos = 0	'Will hold the physical position where the next delimiter was found
	For J = 1 To PosNo
		DelimPos = Instr(StartSrchPos,InputStr,DelimChr)
		If DelimPos > 0 Then			'Delimiter found in the sub-string
			DelimCnt = DelimCnt + 1	'Keep track of what number of delimiter this is
			If DelimCnt = PosNo - 1 Then
				StartDelimPos = DelimPos	'Keep going - not the right delimiter position
			Elseif DelimCnt = PosNo Then
				EndDelimPos = DelimPos
				Exit For              'Exit loop - the right delimiter position has been found
			End If
			StartSrchPos = DelimPos + 1	'The next search will begin just after the last delimiter was found
			Exit For               'Exit loop - no more delimiters found
		End If
	Next J
	If StartDelimPos > 0 And EndDelimPos > 0 Then
		If StartDelimPos+1 = EndDelimPos Then 
			Field = ""
			Exit Function	'There is no gap between the two delimiter positions - return nothing
		End If
	End If
	If StartDelimPos > 0 Or EndDelimPos > 0 Then
		If EndDelimPos > 0 Then
			Field = Mid$(InputStr,StartDelimPos+1,EndDelimPos-(StartDelimPos+1))	'Return those bytes between the 2 delimiter positions
			Field = Mid$(InputStr,StartDelimPos+1,MaxLen)		'No subsequent delimiter was found - return the remainder of the string
		End If
		'If the input string doesn't contain a delimiter and the 1st occurrance was requested, just return the entire string else return nothing
		If PosNo = 1 And DelimCnt = 0 Then Field = InputStr Else Field = ""
	End If
End Function

This was first published in February 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.