How to modifiy a list after using Regex.Split

raphael75

Active Member
Licensed User
Longtime User
Hello,

I try to add / insert / remove a row in a list after using Regex.Split, but I probably don't use the commands correctly as I get following error message:
java.lang.UnsupportedOperationException

Here is the code:
B4X:
Dim Str_Msg As String
Str_Msg = "123;456;789"
   
Dim List_Msg As List
List_Msg.Initialize
List_Msg = Regex.Split(";", Str_Msg)
InputList(List_Msg, "List", -1)  ' => This works correctly  ( 123 / 456 / 789 )
   
List_Msg.Add("Extra row")  ' => Error message
InputList(List_Msg, "List", -1)
   
' Insert first row
' List_Msg.InsertAt(0, "This is the new first row")  ' => Error message
   ' Str_Msg = "This is the new first row"
   ' List_Msg.InsertAt(0, Str_Msg)
' InputList(List_Msg, "List", -1)
   
' Remove first row
' List_Msg.RemoveAt(1)  ' => Error message
' InputList(List_Msg, "List", -1)

Following code works correctly (no use of Regex.Split):
B4X:
Dim List_Msg As List
List_Msg.Initialize
List_Msg.Add("1")
List_Msg.Add("2")
List_Msg.Add("3")
InputList(List_Msg, "List", -1)  ' => OK

List_Msg.Add("4")
InputList(List_Msg, "List", -1)  ' => OK

List_Msg.InsertAt(0, "This is the new first row")
InputList(List_Msg, "List", -1)  ' => OK

List_Msg.RemoveAt(0)
InputList(List_Msg, "List", -1)  ' => OK

What is wrong with Regex.Split?
 

NJDude

Expert
Licensed User
Longtime User
This works:
B4X:
Dim Str_Msg As String

Str_Msg = "123;456;789"

Dim List_Msg() As String
Dim xList_Msg As List

List_Msg = Regex.Split(";", Str_Msg)

xList_Msg.Initialize

For I = 0 To List_Msg.Length -1

    xList_Msg.Add(List_Msg(I))

Next

InputList(xList_Msg, "List", -1)

xList_Msg.Add("Extra row")

InputList(xList_Msg, "List", -1)
 
Upvote 0

raphael75

Active Member
Licensed User
Longtime User
Thank you for your help.

I also found another possibility: I create first the list with one row before adding Regex.Split, and then I can still use Add / InsertAt / RemoveAt.
Strange... :)

B4X:
Dim List_Msg As List
List_Msg.Initialize
List_Msg.Add("1")
InputList(List_Msg, "List", -1)  ' => OK ( 1 )

Dim Str_Msg As String
Str_Msg = "123;456;789"
List_Msg.AddAll(Regex.Split(";", Str_Msg))
InputList(List_Msg, "List", -1)  ' => OK ( 1 / 123 / 456 / 789 )

List_Msg.Add("Extra row")
InputList(List_Msg, "List", -1)  ' => OK ( 1 / 123 / 456 / 789 / Extra )

' Insert first row
List_Msg.InsertAt(0, "This is the new first row")
InputList(List_Msg, "List", -1)  ' => OK ( New / 1 / 123 / 456 / 789 / Extra )

' Remove first row
List_Msg.RemoveAt(0)
InputList(List_Msg, "List", -1)  ' => OK ( 1 / 123 / 456 / 789 / Extra )

It seems like a list created with Regex.Split is in a different format/type than a list created with a string (or array of strings).
Maybe this should be mentioned in the documentation.
 
Upvote 0
Top