Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    splitString: split a string using the specified delimiter

    Functions
    2
    2
    308
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • pianofire
      pianofire Fuze Team last edited by pianofire

      "splitString" by @pianofire
      Share Code : 5Z537MNDNN

      // function :   splitString
      // description: split a string using the specified delimiter
      // author :     @pianofire
      // arguments :
      //     stringVar: string to be split
      //     delimiter : delimiting character (or string)
      // returns :
      //     array of strings
      function splitString( stringVar, delimiter, removeEmpty )
          array result[0]
          int words = 0
          vector pos = { 0, 0 }  
          if ( len( stringVar ) > 0 ) then
              stringVar += delimiter
          endIf
          while( pos.y < len( stringVar ) ) loop
              char = stringVar[ pos.y : pos.y ]
              if ( char == delimiter ) then
                  word = stringVar[ pos.x : pos.y - 1 ]
                  if ( word != "" or !removeEmpty ) then
                    result[ words ] = word
                    words += 1
                  endif
                  pos.x = pos.y + 1
              endif
              pos.y += 1
          repeat
      return result
      
      // Unit test code by @PB____
      
      // print with new line
      function printLn( value )
          print( value, "\n" )
      return void
      
      // join a series of values together in a string
      function join(values)
          string result = ""
          int i
          for i = 0 to len( values ) loop
              result += str( values[ i ] )
          repeat
      return result
      
      // assert that value1 is equal to value2
      function assertEqual( value1, value2 )
          string str1 = str( value1 )
          string str2 = str( value2 )
          string outcome = ""
          if str1 == str2 then
              ink( lime )
              outcome = "success"
          else
              ink( red )
              outcome = "fail"
          endif
          println( join( [ "assert ", str1, " equals to ", str2, ": ", outcome ] ) )
      return void
      
      // print test description
      function description( text )
          ink( { 0.5, 0.5, 0.5, 1 } )
          println(text)
      return void
      
      // run unit tests with the specified delay at the end
      function runUnitTests( duration )
          array output[0]
       
          description( "test word" ) 
          string stringVar = "wibble"
          output = splitString( stringVar, " ", false )
          assertEqual( output, strReplace("[ 'wibble' ]", "'", chr(34) ) )
      
          description( "test words" )
          string stringVar = "the quick brown fox"
          output = splitString( stringVar, " ", false )
          assertEqual( output, strReplace("[ 'the', 'quick', 'brown', 'fox' ]", "'", chr(34) ) )
      
          description( "test remove empty" )
          string stringVar = "the  quick  brown  fox"
          output = splitString( stringVar, " ", true )
          assertEqual( output, strReplace("[ 'the', 'quick', 'brown', 'fox' ]", "'", chr(34) ) )
      
          description( "test empty string" ) 	
          string stringVar = ""
          output = splitString( stringVar, " ", false )
          assertEqual( output, "[  ]" )
      
          description( "test list" ) 
          string stringVar = "0,1,2,3,4"
          output = splitString( stringVar, ",", false )
          assertEqual( output, strReplace("[ '0', '1', '2', '3', '4' ]", "'", chr(34) ) )
      
          update()
          sleep( duration )
      return void
      
      runUnitTests( 10 )
      
      P 1 Reply Last reply Reply Quote 4
      • P
        petermeisenstein F @pianofire last edited by

        @pianofire thats awesome

        1 Reply Last reply Reply Quote 1
        • First post
          Last post