Bug? [SOLVED] regex.split failure

sorex

Expert
Licensed User
Longtime User
Hello,

for some reason this fails in B4i while it worked fine in B4A.

B4X:
data=Regex.Split("",num)

num is for example "0200"

it works fine when using 0,2,0,0 & "," as seperator tho but "" should work aswell.


the log gives

B4X:
Error occurred on line: 196 (gameclass)
Error Domain=NSCocoaErrorDomain Code=2048 "The operation couldn’t be completed. (Cocoa error 2048.)" UserInfo=0x145f1b60 {NSInvalidValue=}
Stack Trace: (
  CoreFoundation  <redacted> + 154
  libobjc.A.dylib  objc_exception_throw + 38
  CoreFoundation  <redacted> + 0
  CLAUStrophoba  +[B4IRegex getPattern::] + 536
  CLAUStrophoba  -[B4IRegex Split2:::] + 126
  CLAUStrophoba  -[B4IRegex Split::] + 80
  CoreFoundation  <redacted> + 68
  CoreFoundation  <redacted> + 282
  CLAUStrophoba  +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1928
  CLAUStrophoba  -[B4IShell runMethod:] + 496
CLAUStrophoba  -[B4IShell raiseEventImpl:method:args::] + 1846
CLAUStrophoba  -[B4IShellBI raiseEvent:event:params:] + 1316
CLAUStrophoba  __33-[B4I raiseUIEvent:event:params:]_block_invoke + 74
libdispatch.dylib  <redacted> + 10
libdispatch.dylib  <redacted> + 22
libdispatch.dylib  <redacted> + 254
CoreFoundation  <redacted> + 8
CoreFoundation  <redacted> + 1300
CoreFoundation  CFRunLoopRunSpecific + 522
CoreFoundation  CFRunLoopRunInMode + 106
GraphicsServices  GSEventRunModal + 138
UIKit  UIApplicationMain + 1136
CLAUStrophoba  main + 116
libdyld.dylib  <redacted> + 2
)
 

LucaMs

Expert
Licensed User
Longtime User
"" without a space?

If so, according to "the logic" it should return an array with a single element, "0200"

(I have not B4I and not tried with B4A)


[without a space it is like passing a null parameter, I think].
 

sorex

Expert
Licensed User
Longtime User
I could use charat but I apply a lot of math with those values and charat doesn't seem to cast right from string to my int and won't compile so I need to change a lot more than with my regex.split("",var) solution.
 

sorex

Expert
Licensed User
Longtime User
I used this trick in several languages so it's not my own invention ;)

also... there is a difference between undefined, null and ""
 

LucaMs

Expert
Licensed User
Longtime User
I tried using B4A and the empty string works as you want, but the array must be of type string.

(B4A code, of course)
B4X:
Dim Text As String = "0200"
Dim A() As String = Regex.Split("", Text)
For i = 0 To A.Length - 1
    Log(i & " " & A(i))
Next
  
Dim B(a.Length) As Byte
For i = 0 To A.Length - 1
    B(i) = A(i)
Next

?
 

sorex

Expert
Licensed User
Longtime User
flash actionscript: Splits a String object into substrings by breaking it wherever the specified delimiter parameter occurs and returns the substrings in an array. If you use an empty string ("") as a delimiter, each character in the string is placed as an element in the array.


javascript:
The split() method is used to split a string into an array of substrings, and returns the new array.

Tip: If an empty string ("") is used as the separator, the string is split between each character.


and so on
 

sorex

Expert
Licensed User
Longtime User
I tried using B4A and the empty string works as you want, but the array must be of type string.

right, it works fine in B4A as I said in my initial post.

it might be an IOS limitation.
 

sorex

Expert
Licensed User
Longtime User
bummer... :(


The separator string can't be blank. If you need to separate a string into its individual characters, just loop through the length of the string and convert each char into a new string:

NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
NSString *ichar = [NSString stringWithFormat:mad:"%c", [myString characterAtIndex:i]];
[characters addObject:ichar];
}
 

sorex

Expert
Licensed User
Longtime User
and like I said I need other extra code to get the casting right.

it can be done like this unless Erel has some more decent/elegant method

B4X:
tmpstring=cartypedata.charat(0) 'grab value
xoffset=tmpstring 'cast to int as immediate cast with charat fails (won't compile)
 

sorex

Expert
Licensed User
Longtime User
Yes, that's how I solved it last night.

You can forget/drop this issue. :)
 
Top