PDA

View Full Version : "Locate" Function For Delimited Strings



gungadout
12-10-2010, 08:04
Hi Folks,

My eyeballs must not be working properly.

I cannot find in the documentation a function to return the index of a particular string within a delimited string.

The syntax would be something like Index = LOCATE$(SearchString, MainString, StringDelimiter, StartIndex), where: StartIndex is the nth delimited area to start looking in; Index = the first delimited area found containing the SearchString, the whole SearchString and nothing but the SearchString (to borrow from legalese). The Index of the position in front of the first occurrence of the StringDelimiter = 1. If the SearchString is not found, the value of Index would be zero.

My wife always accuses me of suffering from domestic blindness (not being able to see things right in front of my face). Maybe she's right.

I've found things like GRAB$, PARSE$, PARSESET$ and PATCH$, but they naturally assume that you know the index position you want.

I need to locate a key string in one delimited array, then pull data out of other delimited arrays using that index position.

I can code around it, but if there is a function that I have overlooked, that would be better.

What did I miss?

Thanks,

Peter H. (gungadout)

Petr Schreiber
12-10-2010, 08:36
Hi Peter,

to seek String inside of another String, you can use INSTR.

Syntax:


INSTR([StartPos,] MainString, [ANY] MatchString [, nthOccurience ])


Sample code:


Dim s As String = "Hi Peter, you Peter, I mean Peter"

MsgBox 0, "First match at: "+InStr(s, "Peter", 1)
MsgBox 0, "Second match at: "+InStr(s, "Peter", 2)
MsgBox 0, "Third match at: "+InStr(s, "Peter", 3)


If you need to find match in thing like CSV, I think that is not yet present, but could be done as demonstrated here:



Dim s As String = "Eros,Charles,Peter,Gordon,Master Chief"

MsgBox 0, Locate("Charles", s, ",", 1)
MsgBox 0, Locate("Master Chief", s, ",", 3) ' seeking from 3rd position, relative found position would be 3, but locate returns 5 (in absolute)
MsgBox 0, Locate("Mr.Walrus", s, ",", 1) ' not present, will give 0

Function Locate(SearchString As String, MainString As String, StringDelimiter As String, StartIndex As Long) As Long

Dim Items() As String
Dim nItems As Long = Parse(MainString, Items, StringDelimiter)

Dim indexAt As Long = Array Scan Items(StartIndex), = SearchString
If StartIndex > 1 Then indexAt += StartIndex - 1 ' to return absolute

Return indexAt

End Function


Let me know. Maybe LOCATE$ could be nice addition to TB, I will post a request.


Petr

Petr Schreiber
12-10-2010, 09:05
I posted a request with slightly modified name and parameter order follows the logic INSTR provides, to maintain language consistency:
PARSELOC: New friend of PARSE$ and PARSECOUNT (http://community.thinbasic.com/index.php?topic=3714.msg27281#msg27281)


Petr

gungadout
12-10-2010, 12:24
Hi Petr,

INSTR() is what I was looking for.

I missed it because I was expecting $ to be part of the function name (e.g. INSTR$())

We need no special function for CSV or TAB-delimited strings, because INSTR does the job well, as you demonstrated.

Once again, thanks very much for your help.

Regards,

Peter H. (gungadout)