Advertisement
Guest User

Remixful's string_trim GML script - revised #2

a guest
Jan 23rd, 2017
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. /// string_trim(string Inputstring, string side="both", string char=" ")
  2. // Description: Removes leading and trailing matches of a string.
  3. // --- Arguments ---
  4. // Inputstring - the input to trim
  5. // (optional) side - "left", "right", or "both". Uses "both" when empty.
  6. // (optional) char - the character to remove. Uses the " " character (space) when empty
  7. /// Created by Remixful
  8.  
  9. var str = string(argument[0])
  10. var side = "both"
  11. var char = " "
  12. if argument_count >= 2 { if (argument[1] == "left" or argument[1] == "right" or argument[1] == "both") side = argument[1] }
  13. if argument_count == 3 { char = string(argument[2]) }
  14. var new_string = str
  15. if side == "left" or side == "both" {
  16. var _start = 0
  17. for(i=1;i <= string_length(new_string);i++){
  18. if string_char_at(new_string, i) != char{
  19. _start = i - 1
  20. break
  21. }
  22. }
  23. if _start != 0{new_string = string_delete(new_string,1,_start)}
  24. }
  25. if side == "right" or side == "both"{
  26. var _end = 0
  27. for(i=string_length(new_string);i > 0;i--){
  28. if string_char_at(new_string, i) != char{
  29. _end = i + 1
  30. break
  31. }
  32. }
  33. if _end != 0{new_string = string_delete(new_string,_end,string_length(new_string))}
  34. }
  35. return new_string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement