Splitting a String

PCowling

Member
Licensed User
Longtime User
This has to be one of the most helpful communities and support forums.

But I have another problem!?

I am trying to split the returned value of the NMEA GPS.

For example Location1.ConvertToMinutes(Location1.Latitude) would return somthing like

53:42.51202

I would like to split this into

Deg = 53
Min = 42
Sec = 51202

I have converted it to a string looking to use a VB split function but cant find anything similar here.

Thanks for the help

P Cowling
 

joseluis

Active Member
Licensed User
Longtime User
I'm not in front of the editor so I can't test if this works but you can use the string functions for that. Assuming that the separators are always ':' and '.' between the 3 chunks, this is the way to do it:

B4X:
Dim originalStr As String
Dim degrees, minutes, seconds As Int
Dim sep1, sep2 As Int ' index of separators

originalStr = "53:42.51202"

sep1 = originalStr.IndexOf(":") ' it should be 2 in this case
sep2 = originalStr.IndexOf(".") ' it should be 5 in this case

degrees = originalStr.SubString2(0, sep1)
minutes = originalStr.SubString2(sep1 + 1, sep2)
seconds = originalStr.SubString( sep2 + 1)
 
Upvote 0

PCowling

Member
Licensed User
Longtime User
Thanks will give that a go, will be a day or two now, but I will get back to you.

I have been faffing about with regex.split to no avail!

Thank you again for your time.

Phil
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
PCowling,
Watch which function you use to get Degree, Minutes and Seconds!

ConvertToMinutes returns a string in form of DDD:MM.MMMMM ie Minutes and decimals of minutes. Whereas ConvertToSeconds returns a string in the form of DDD:MM:SS.SSSSS, which I think is what you're looking for!

You'll have to modify joseluis's code slightly to use the 'Index2' string method which allows specification of a starting offset.

Cheers,
Douglas
 
Upvote 0

PCowling

Member
Licensed User
Longtime User
Douglas

Thanks for the help also, right now the provided code works well, but like you say, it was designed to work with those numbers. Some more testing will be needed Im sure. Ill have a look at the different .ConvertTo...

Thanks again so much

Phil
 
Upvote 0

PCowling

Member
Licensed User
Longtime User
In fact yes, I see what you mean now, will see about splitting on two "." now. But it is some clever code none the less.

Phil
 
Upvote 0

mearleybrook

Member
Licensed User
Longtime User
I am also new to this stuff and have ended up using Stringbuilder for all such manipulations.
Very easy flexible and quick. Never found anything it had trouble with.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
B4X:
dim str(2) as string

str = regex.split(".", originalStr)
seconds = str(1)
str = regex.split(":", str(0))
degrees = str(0)
minutes = str(1)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here is another solution:
B4X:
Sub Process_Globals
   Type DMS (Deg As Int, Minutes As Int, Seconds As Int)
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim d As DMS
   d = ParseDMS("53:42.51202")
   Log(d.Deg & " " & d.Minutes & " " & d.Seconds)
End Sub

Sub ParseDMS(val As String) As DMS
   Dim m As Matcher
   Dim d As DMS
   m = Regex.Matcher("(\d+):(\d+)\.(\d+)", val) 'assuming that there is always a seconds section
   If m.Find = False Then
      Msgbox("Invalid value: " & val, "")
   Else
      d.Deg = m.Group(1)
      d.Minutes = m.Group(2)
      d.Seconds = m.Group(3)
   End If
   Return d
End Sub
 
Upvote 0

PCowling

Member
Licensed User
Longtime User
Thank you all so very much, this has really helped and as ever improved my enjoyment of coding in B4A.

At the end of the day getting enjoyment from the challenges is what its all about.

Phil
 
Upvote 0

GKTechnology

New Member
Licensed User
Longtime User
String Splitting Code

I use this in my program for splitting lines out of a file for setting to buttons text.

B4X:
Sub StringSplit(InputString As String) As List
   
   Dim OutList As List
   Dim CommaLoc As Int
   
   OutList.Initialize
   CommaLoc=InputString.IndexOf(",")
   
   Do While CommaLoc >-1      
      Dim LeftSide As String : LeftSide= InputString.SubString2(0,CommaLoc)
      Dim RightSide As String :RightSide= InputString.SubString(CommaLoc+1)
      OutList.Add(LeftSide)
      InputString=RightSide
      CommaLoc=InputString.IndexOf(",")
   Loop
   
   OutList.Add(InputString)
   
   Return OutList

End Sub
 
Upvote 0
Top