B4J Question How to get all enclosed strings with #?

Mashiane

Expert
Licensed User
Longtime User
Hi there

Let's assume, this is the master content..

B4X:
Today #I# got to #experiment# #with# reef. I #needed# this to try #and# see if #I# can #wrap# it with #BANano#.png

Now I need to return a list that will have

B4X:
I,experiment,with,needed,and,wrap,BANano

And kindly need a code snippet for such. Preferably not RegEx please.

Thanks
 

emexes

Expert
Licensed User
Preferably not RegEx please.
I know you thought you'd never hear these words from me, but: this is probably better done with regex :-/

Nonetheless... try this:
B4X:
Dim L As List
L.Initialize

Dim S As String = "Today #I# got to #experiment# #with# reef. I #needed# this to try #and# see if #I# can #wrap# it with #BANano#.png"

Dim OpenHashAt As Int = -1

For I = 0 To S.Length - 1
    If S.CharAt(I) = "#" Then
        If OpenHashAt = -1 Then    'if not currently inside a hash field
            OpenHashAt = I
        Else    'currently inside a hash field
            L.Add(S.SubString2(OpenHashAt + 1, I))
            OpenHashAt = -1    'now outside of hash field, get ready for next one
        End If
    End If
Next I
 
Last edited:
Upvote 0

vpires

Member
Licensed User
Longtime User
Well ... with the regEx stuff that you don't want. Lol.
B4X:
     Dim text As String ="Today #I# got to #experiment# #with# reef. I #needed# this to try #and# see if #I# can #wrap# it with #BANano#.png"
    Dim pattern As String = "#(.*?)#"
    Dim Matcher1 As Matcher
    Dim l As List : l.initialize
   
    Matcher1 = Regex.Matcher(pattern, text)
    Do While Matcher1.Find
        Log("Found: " & Matcher1.Group(1))
        l.Add(Matcher1.Group(1))
    Loop
 
Upvote 0
Top