January 15, 2021, 06:00:55 PM

### Author Topic: [bmx] Instr2 by Chroma [ 1+ years ago ]  (Read 594 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] Instr2 by Chroma [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Instr2
Author : Chroma
Posted : 1+ years ago

Description : Here's how this works.  You have a string separated by commas:

str = "1,2,3,4876,325"

There are 4 commas in that string.  This function will return the position of the commas specified.  If you want to know the position of comma #3 then you would go:

c3 = Instr2( str, ",",3 )

It will return a 6.  I wrote this so I could return large strings of information and then extract certain numbers from it.

If you wanted to extract the 4th value in your string, you would go:

index1 = Instr2(str,",",3)
index2 = Instr2(str,",",4)
return Mid(str,index1+1,index2-index1)

Simple!

This could also be useful for extracting information from multiplayer packets.

You could make a function like so:

Code :
Code: BlitzMax
1. Function Instr2:Int(str:String,sub:String,occ:Int)
2.         Local i:Int,f:Int,index:Int
3.         For i = 1 To occ
4.                 f = Instr(str,sub,index)
5.                 index = f + 1
6.         Next
7.         Return f
8. End Function
9.
10.
11. Function GetPacketValue(strPkt,strSep,intValue)
12.    Select intValue
13.       Case 1
14.          Return Mid( strPkt, 1, Instr2( strPkt, Chr(44), 1) - 1) )
15.       Case 2
16.          index1 = Instr2( strPkt, Chr(44), 1)
17.          index2 = Instr2( strPkt, Chr(44), 2)
18.          Return Mid( strPkt, index1+1, index2-index1)
19.       Case 3
20.          'blah blah
21.    End Select
22. End Function

Comments : none...

SimplePortal 2.3.6 © 2008-2014, SimplePortal