GameMaker: Studio

GameMaker: Studio

View Stats:
machineguy Jan 21, 2017 @ 10:50pm
Trimming spaces from a string
Need to trim the start and end from spaces.
Example " hello world " is "hello world" trimmed.

What's the easiest way to implement this?
Last edited by machineguy; Jan 21, 2017 @ 11:31pm
< >
Showing 1-5 of 5 comments
sandboxgod Jan 21, 2017 @ 11:17pm 
the function string_letters() takes a string and removes everything but the letters. So should remove spaces as well. Unless the description of the function is lying.
machineguy Jan 21, 2017 @ 11:29pm 
Already tried that. I need spaces in the string but just not at the beginning or end.
So " hello world " would become "hello world".
The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.)
Last edited by machineguy; Jan 21, 2017 @ 11:31pm
machineguy Jan 22, 2017 @ 12:19am 
/// string_trim(string Inputstring, string side="both", string char=" ")
// Description: Removes leading and trailing matches of a string.
// --- Arguments ---
// Inputstring - the input to trim
// (optional) side - "left", "right", or "both". Uses "both" when empty.
// (optional) char - the character to remove. Uses the " " character (space) when empty
/// Created by Remixful

var str = string(argument[0])
var side = "both"
var char = " "
if argument_count >= 2 { if (argument[1] == "left" or argument[1] == "right" or argument[1] == "both") side = argument[1] }
if argument_count == 3 { char = string(argument[2]) }
var new_string = str
if side == "left" or side == "both" {
var _start = 0
for(i=1;i <= string_length(new_string);i++){
if string_char_at(new_string, i) != char{
_start = i - 1
break
}
}
if _start != 0{new_string = string_delete(new_string,1,_start)}
}
if side == "right" or side == "both"{
var _end = 0
for(i=string_length(new_string);i > 0;i--){
if string_char_at(new_string, i) != char{
_end = i + 1
break
}
}
if _end != 0{new_string = string_delete(new_string,_end,string_length(new_string))}
}
return new_string

Made a script just now. pastebin link for formatting purposes[pastebin.com]
Example: http://i.imgur.com/gC1Q5ev.png
Last edited by machineguy; Jan 23, 2017 @ 12:22am
frumple Jan 22, 2017 @ 4:48am 
Nicely done.
sandboxgod Jan 22, 2017 @ 9:38am 
Originally posted by Nobody:
Already tried that. I need spaces in the string but just not at the beginning or end.
So " hello world " would become "hello world".
The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.)
Ah yes. Sorry wasnt thinking. But good job on the script.
< >
Showing 1-5 of 5 comments
Per page: 1530 50

Date Posted: Jan 21, 2017 @ 10:50pm
Posts: 5