KAKAN

GetTok

Feb 9th, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.62 KB | None | 0 0
  1. function GetTok( string, separator, n, m = 0 ){
  2.     //To support the old calculation, we need to do this.
  3.     n = n - 1;
  4.     m = m > 0 ? m : n + 1;
  5.     local tokenized = split( string, separator ),
  6.             text = "";
  7.     if( n > tokenized.len() || n < 0 || m > tokenized.len() ) return null;
  8.     tokenized = tokenized.slice( n, m );
  9.     return tokenized.reduce( function( prev, next ){ return ( prev + separator + next ) } );
  10. }
  11.  
  12. // THIS SCRIPT IS FOR TESTING THIS FUNCTION:
  13.  
  14. local test = "I'm a quick brown little fox.";
  15. print( GetTok( test, " ", 1 ) );
  16. print( GetTok( test, " ", 1, 6 ) );
  17.  
  18. // OUTPUT
  19. /*
  20. I'm
  21. I'm a quick brown little fox.
  22. */
Add Comment
Please, Sign In to add comment