Android Question Getting a piece of a text string using a delimiter

Colin Evans

Active Member
Licensed User
Longtime User
Hi, I need to be able to extract a piece if text between two delimiters, i.e. if the 'text' string is something like '/this is one/this is two/this is three/this is four' I need to be able to define the delimiter as '/' and then have the code extract a piece of the data.

In VB6 I'd create a Function

Function Piece(sSource As String, sDelimiter As String, iPiece As Integer)
Dim s As String
s = sSource + sDelimiter
Dim i As Integer
Dim iDelPos As Integer
If iPiece > 1 Then
For i = 1 To iPiece - 1
iDelPos = InStr(s, sDelimiter)
s = Mid(s, iDelPos + 1, Len(s))
Next i
End If
If s = "" Then
Piece = ""
Else
Piece = Mid(s, 1, InStr(s, sDelimiter) - 1)
End If
End Function

And then to extract say the third piece of data from the I'd simply execute the following

dim TextExtract as string
TextExtract = Piece(text, "/",3)

and TextExtract would = 'this is three'

is this possible in B4A
 

Colin Evans

Active Member
Licensed User
Longtime User
How would that work relating to my example where I want to extract a particular piece of the data, where I can then use TextExtract elsewhere.

basically the text I am wanting to extract from contains a path to a filename

//rootfiledir/examples/exampleD/file123.png

and I just want the filename 'file123.png' as it isn't in the path given but is in my files

so I just want to be able to load it with

LoadBitmap(File.DirAssets,TextExtract)

Hope that makes sense, thanks for replying
 
Last edited:
Upvote 0

Sandman

Expert
Licensed User
Longtime User
How would that work relating to my example

I'm not sure I understand where the problem is. LucaMs is absolutely correct, Split is the solution to use. Did you read the linked docs? In it we have this example:

B4X:
Dim components() As String 
components = Regex.Split(",", "abc,def,,ghi") 'returns: "abc", "def", "", "ghi"

Just do that and pick the last item of the array and you'll have the filename.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
so I just want to be able to load it with
LoadBitmap(File.DirAssets,TextExtract)
Here is the snippet that pertains to your case:
B4X:
Dim strVar As String="//rootfiledir/examples/exampleD/file123.png"
    Dim strArray() As String=Regex.Split("/",strVar)
    Dim TextExtract As String=strArray(strArray.Length-1)  'last item which is:  file123.png
Activity.SetBackgroundImage(LoadBitmap(File.DirAssets,TextExtract))
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Here is the snippet that pertains to your case:
B4X:
Dim strVar As String="//rootfiledir/examples/exampleD/file123.png"
    Dim strArray() As String=Regex.Split("/",strVar)
    Dim TextExtract As String=strArray(strArray.Length-1)  'last item which is:  file123.png
Activity.SetBackgroundImage(LoadBitmap(File.DirAssets,TextExtract))
In that specific case you can use:
B4X:
    Dim strVar As String = "//rootfiledir/examples/exampleD/file123.png"
Dim TextExtract As String = strVar.SubString(strVar.LastIndexOf("/") + 1)
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi just a quick question, the date returned in the JSON data is in the format of YYYY-MM-DD i.e 2018-05-28 is there a quick way to change this to DD/MM/YYYY i.e. 28/05/2018 so its displayed this way
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
remember the Dateformat
Set the dateformat to match you datestr
Parse the datestr to a date (LONG)
set dateformat to match your expected output
use the date value to output
set dateformat to the remembered value
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Sorry tried but I can't seem to get it right, basically don't know what to do

B4X:
                    Dim date0 As String = col0.Get("date")
                    DateTime.DateFormat = "yyyy-mm-dd"
                    DateTime.DateParse(date0)
                    DateTime.DateFormat="dd/mm/yyyy"
                    btnDay1.Text = date0

still puts 2018-05-28 in btnDay1.text
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
you forgot that DateTime.DateParse(date0) have a return value
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
B4X:
Dim date0 As String = col0.Get("date")
                    DateTime.DateFormat = "yyyy-mm-dd"
                    Dim lngDate As Long= DateTime.DateParse(date0)
                    DateTime.DateFormat="dd/mm/yyyy"
                    btnDay1.Text = lngDate

This doesn't work either, gives me a long numeric value as the date, sorry to be a pain, I basically have seven dates returned by the JSON data and I simply want to get the Day of the week name for each date, but that function requires the date to be in a specific format as you know
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Amazing, many thanks, and then top get the day of the week name would it be

B4X:
lblDOW1.Text=DateUtils.GetDayOfWeekName(lngDate)

Yes it does but had to change the date format to MM/dd/yyyy, many thanks for all your help
 
Upvote 0
Top