from Hacker News

The “Split Strings” Challenge Using Java

by aogl on 8/10/20, 6:45 AM with 1 comments

  • by oldandtired on 8/10/20, 7:52 AM

    in unicon/icon it would be

    procedure StringSplit(str)

        #
        # define a local to hold an initial empty list 
        #
        local l := list()
    
        #
        # if the length of the string is odd then concatenate an "_" character
        #
        str ||:= ((*str % 2 = 1) & "_")
        #
        # scan the string and put each two characters onto the end of the list
        # created above, fail when you reach the end of the string
        #
        str ? while put(l, move(2))
        #
        # return the list to the calling program
        #
        return l
     end
    
    and for an example string of "this is a string" it will return the list ["th", "is", " i", "s ", "a ", "st", "ri", "ng"]

    and for an example string of "this is a string2" it will return the list ["th", "is", " i", "s ", "a ", "st", "ri", "ng", "2_"]

    Nice and simple compared to the example given in Java.