Android Question Suggestion to add to popup help

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Wasted some time to debug an algorithm as I wasn't aware that the pipe character (|) in Regex is a special character and needs to be escaped. Took some time to pinpoint the problem and made the mistake to not search the forum and coded a split function:

B4X:
Sub Split(strSplitter As String, strToSplit As String) As String()
 
 Dim i As Int
 Dim n As Int
 Dim iPos As Int
 Dim iPosOld As Int

 iPos = strToSplit.IndexOf(strSplitter)
 
 If iPos = -1 Then
  Dim arrSplit(1) As String
  arrSplit(0) = strToSplit
  Return arrSplit
 End If
 
 Dim arrSplit(strToSplit.Length / 2 + 1) As String

 arrSplit(n) = strToSplit.SubString2(0, iPos)
 n = 1
 
 Do While True
  iPosOld = iPos
  iPos = strToSplit.IndexOf2(strSplitter, iPosOld + 1)
  If iPos = -1 Then Exit
  arrSplit(n) = strToSplit.SubString2(iPosOld + 1, iPos)
  n = n + 1
 Loop
 
 'add any trailing item
 If iPosOld < strToSplit.Length - 1 Then
  arrSplit(n) = strToSplit.SubString(iPosOld + 1)
  n = n + 1
 End If
 
 Dim arrSplit2(n) As String
 
 For i = 0 To n - 1
  arrSplit2(i) = arrSplit(i)
 Next
 
 Return arrSplit2

End Sub

This works all fine, but then did a forum search for Regex, pipe and found the simple solution.
I did look at the RegEx.Split help popup, but no mention of this. I think the pipe character is a
common character used as a separator, so I have a feeling I am not the first one to have made
this mistake (as indeed the forum search showed) and maybe this could be added to the popup
help.


RBS
 

Brandsum

Well-Known Member
Licensed User
Creating own split function is as easy as follows,
B4X:
Sub Split(strSplitter As String, strToSplit As String) As String()
    Dim tempList As List
    tempList.Initialize
    Dim item As String
    For i = 0 To strToSplit.Length-1
        If strToSplit.CharAt(i) = strSplitter Then
            tempList.Add(item)
            item = ""
        Else
            item = item & strToSplit.CharAt(i)
        End If
    Next
    
    Dim SplitedString(tempList.Size) As String
    For i=0 To tempList.size-1
        SplitedString(i) = tempList.Get(i)
    Next
    
    Return SplitedString
End Sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Creating own split function is as easy as follows,
B4X:
Sub Split(strSplitter As String, strToSplit As String) As String()
    Dim tempList As List
    tempList.Initialize
    Dim item As String
    For i = 0 To strToSplit.Length-1
        If strToSplit.CharAt(i) = strSplitter Then
            tempList.Add(item)
            item = ""
        Else
            item = item & strToSplit.CharAt(i)
        End If
    Next
   
    Dim SplitedString(tempList.Size) As String
    For i=0 To tempList.size-1
        SplitedString(i) = tempList.Get(i)
    Next
   
    Return SplitedString
End Sub

Yes, easy, sure, but you don't need it.

RBS
 
Upvote 0
Top