November 28, 2020, 02:03:33 AM

Author Topic: [bmx] GetWord() & CountWords() by misth [ 1+ years ago ]  (Read 363 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] GetWord() & CountWords() by misth [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : GetWord() & CountWords()
Author : misth
Posted : 1+ years ago

Description : I found this old code which I've made with the Demo version of BlitzMax. I hope I get the money for it! :D

Anyway, here they are:
GetWord() - Get the word from string with index number and separator.
CountWords() - Count how many words your input has, split with separator too.


Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. ' FUNCTIONS
  4.  
  5. ' Return word from sentence
  6. Function GetWord:String(_line:String, index:Int, separ:String = " ")
  7.         Return _line.Split(separ)[index - 1]
  8. End Function
  9.  
  10. ' Return word count in sentence
  11. Function CountWords:Int(_line:String, separ:String = " ")
  12.         Return _line.Split(separ).Length
  13. End Function
  14.  
  15.  
  16. ' EXAMPLE PROGRAM as speed-test
  17.  
  18. Local line:String = "I am a sentence. You can split me any way you want."
  19. Const TESTS:Int = 500000
  20. Local start:Int, _end:Int
  21.  
  22. For Local i:Int = 1 To CountWords(Line)
  23.         Print GetWord(line, i)
  24. Next
  25.  
  26. Print "GetWord test start"
  27. start = MilliSecs()
  28. For Local i:Int = 1 To TESTS
  29.         GetWord(Line, 1)
  30. Next
  31. _end = (MilliSecs() - start)
  32. Print TESTS + " GetWord tests in " + _end + "ms"
  33.  
  34. Print "CountWords test start"
  35. start = MilliSecs()
  36. For Local i:Int = 1 To TESTS
  37.         CountWords(line, 1)
  38. Next
  39. _end = (MilliSecs() - start)
  40. Print TESTS + " CountWords tests in " + _end + "ms"


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal