Bug? regex single char split

Discussion in 'Bugs & wishlist' started by sorex, Dec 30, 2013.

  1. sorex

    sorex Expert Licensed User

    I don't know if this is a bug in a library or default behaviour in Java
    but this seems suspicious and it's not the output I got in several other languages.


    Code:
    Log ( hours )
    Log ("!" & DateTime.GetHour(DateTime.Now) &"!")
    Log ( "0:" & Regex.Split("",DateTime.GetHour(DateTime.Now))(0) )
    Log ( "1:" & Regex.Split("",DateTime.GetHour(DateTime.Now))(1) )
    Log ( "2:" & Regex.Split("",DateTime.GetHour(DateTime.Now))(2) )
    Log ( "C:" & Regex.Split("",DateTime.GetHour(DateTime.Now)).Length)

    the output is:

    19
    !19!
    0: <- should be 1
    1:1 <- should be 9
    2:9 <- should not exist
    C:3 <- should be 2

    the value in the array at position 0 should not even exist since there is no leading space (the !19! proves this)
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    This is how the underlying Regex engine behaves. What is the purpose of splitting text with an empty string as the separator???
     
  3. sorex

    sorex Expert Licensed User

    that will make an array of each individual charater, so it's easy to find/fetch the right char.

    I know you can do the same with substr but I use(d) split/explode a lot so it's kind of a habbit to stick to it :)

    I'll keep in mind that the empty split starts at 1 instead of the traditional 0 in the array
    (works fine (pos 0 in the array) with the none empty split, just tested this)
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code:
    Code:
    For i = 0 To SomeString.Length - 1
     
    Dim c As Char = SomeString.CharAt(i)
    Next
     
  5. sorex

    sorex Expert Licensed User

    that's my point, why use a loop when 1 command does the same?

    (Ok internally it probably uses the same trick anyway)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice