March 05, 2021, 07:23:55 AM

Author Topic: [bb] String Handling Functions by Grey Alien [ 1+ years ago ]  (Read 460 times)

Offline BlitzBot

Title : String Handling Functions
Author : Grey Alien
Posted : 1+ years ago

Description : Various useful string handling functions.  Should be easy to convert to Blitz Max.

Code :
Code: BlitzBasic
  1. ; -----------------------------------------------------------------------------
  2. ; pad a string with spaces on the right (best for left aligning strings)
  3. ; -----------------------------------------------------------------------------
  4. Function ccPadRight$(TheString$, TheSize)
  5.         sl = TheSize - Len(TheString$) ; Get length of string
  6.         If sl > 0 Then ; Needs padding?
  7.                 For p = 1 To sl                                                                        
  8.                         z$ = z$ + " " ; make a blank string
  9.                 Next
  10.                 TheString$ = TheString$ + z$; add the blank string to the main string
  11.         EndIf
  12.         Return TheString$
  13. End Function
  14.  
  15. ; -----------------------------------------------------------------------------
  16. ; pad a string with spaces on the right (best for right aligning numbers numbers)
  17. ; -----------------------------------------------------------------------------
  18. Function ccPadLeft$(TheString$, TheSize)
  19.         sl = TheSize - Len(TheString$) ; Get length of string
  20.         If sl > 0 Then ; Needs padding?
  21.                 For p = 1 To sl                                                                        
  22.                         z$ = z$ + " " ; make a blank string
  23.                 Next
  24.                 TheString$ = z$ + TheString$; add the blank string to the main string
  25.         EndIf
  26.         Return TheString$
  27. End Function
  28.  
  29. ; -----------------------------------------------------------------------------
  30. ; First String (return first part of string up to comma)
  31. ; -----------------------------------------------------------------------------
  32. Function ccFirstString$(s$)
  33.         ;doesn't call ccFirstStringToSub so it stays fast for big files
  34.         ;pass in a string, this will only return the first part up to, but not including, the comma (or end)           
  35.         pos% = Instr(s$, ",")
  36.         ;If pos = 0 then then end of the was reached, so return the whole thing.
  37.         If pos = 0 Then
  38.                 Return s$
  39.         Else
  40.                 Return Mid(s$, 1, pos-1)
  41.         EndIf
  42. End Function
  43.  
  44. ; -----------------------------------------------------------------------------
  45. ; Last String (return last part of string from comma)
  46. ; -----------------------------------------------------------------------------
  47. Function ccLastString$(s$)
  48.         ;doesn't call ccLastStringToSub so it stays fast for big files
  49.         ;pass in a string, this will only return the last part from, but not including, the comma
  50.         pos% = Instr(s$, ",")
  51.         ;If pos = 0 then then end of the was reached, so return nothing
  52.         If pos = 0 Then
  53.                 Return ""
  54.         Else
  55.                 Return Mid(s$, pos+1, Len(s$)-pos)
  56.         EndIf
  57. End Function
  58.  
  59. ; -----------------------------------------------------------------------------
  60. ; IniFirst String (return first part of string up to = sign)
  61. ; -----------------------------------------------------------------------------
  62. Function ccIniFirstString$(s$)
  63.         ;pass in a string, this will only return the first part up to, but not including, the = sign (or end)          
  64.         Return ccFirstStringToSub(s$, "=")
  65. End Function
  66.  
  67. ; -----------------------------------------------------------------------------
  68. ; IniLast String (return last part of string from = sign)
  69. ; -----------------------------------------------------------------------------
  70. Function ccIniLastString$(s$)
  71.         ;pass in a string, this will only return the last part from, but not including, the = sign
  72.         Return ccLastStringToSub(s$, "=")
  73. End Function
  74.  
  75. ; -----------------------------------------------------------------------------
  76. ; First String To Sub (return first part of string up to Substring)
  77. ; -----------------------------------------------------------------------------
  78. Function ccFirstStringToSub$(s$, sub$)
  79.         ;pass in a string, this will only return the first part up to, but not including, the substring (or end)
  80.         pos% = Instr(s$, sub$)
  81.         ;If pos = 0 then then end of the was reached, so return the whole thing.
  82.         If pos = 0 Then
  83.                 Return s$
  84.         Else
  85.                 Return Mid(s$, 1, pos-1)
  86.         EndIf
  87. End Function
  88.  
  89. ; -----------------------------------------------------------------------------
  90. ; Last String To Sub (return last part of string from substring)
  91. ; -----------------------------------------------------------------------------
  92. Function ccLastStringToSub$(s$, sub$)
  93.         ;pass in a string, this will only return the last part from, but not including, the substring
  94.         pos% = Instr(s$, sub$)
  95.         ;If pos = 0 then then end of the was reached, so return nothing
  96.         If pos = 0 Then
  97.                 Return ""
  98.         Else
  99.                 Return Mid(s$, pos + Len(sub$), Len(s$)-pos)
  100.         EndIf
  101. End Function
  102.  
  103. ; -----------------------------------------------------------------------------
  104. ; Percent To String
  105. ; -----------------------------------------------------------------------------
  106. Function ccPercentToString(Per#)
  107.         ;simply times by 100 them look for decimal place and chop it and everything past it
  108.         ;warning no range checking is performed
  109.         Local PerString$ = Per * 100
  110.         Return Mid$(PerString, 1, Instr(PerString, ".", 1)-1)
  111. End Function
  112.  
  113. ; -----------------------------------------------------------------------------
  114. ; ccPadWithZeros
  115. ; -----------------------------------------------------------------------------
  116. Function ccPadWithZeros$(TheText$, NumDigits%)
  117.         ;this does not truncate, only enlarges or does nothing
  118.         While Len(TheText) < NumDigits
  119.                 TheText = "0" + TheText
  120.         Wend
  121.         Return TheText
  122. End Function
  123.  
  124. ; -----------------------------------------------------------------------------
  125. ; ccGetEnvVar
  126. ; -----------------------------------------------------------------------------
  127. Function ccGetEnvVar$(VariableName$)
  128.     TempBank = CreateBank(1)
  129.     size = api_GetEnvironmentVariable%(VariableName$,TempBank,1)
  130.     If size > 0 Then
  131.         ResizeBank TempBank,Size+1
  132.         api_GetEnvironmentVariable%(VariableName$,TempBank,Size+1)
  133.     Else
  134.         Return ""
  135.     End If
  136.  
  137.     retstring$ = ""
  138.     For t = 0 To BankSize(tempbank)-3
  139.         retstring$ = retstring$ + Chr(PeekByte(tempbank,t))
  140.     Next
  141.        
  142.     FreeBank tempbank
  143.     Return retstring$
  144. End Function


Comments :


RGR(Posted 1+ years ago)

 ccPadLeft ? Why not use LSet(String,Length)?ccPadRight ? RSet(String,Length)


Grey Alien(Posted 1+ years ago)

 because when I wrote that, I didn't know it existed :-)  Anyway it may serve as a code example for someone and you can modify it too.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal