GameMaker: Studio

GameMaker: Studio

View Stats:
PaperClipMan Apr 16, 2018 @ 5:48pm
Removing all after space in string
I'm trying to figure out how to take a string and delete all characters after a space. The problem is, I don't know how many characters in the string the first space will be and all of the string commands I've found work on character placement arguments.

ie:
var1 = "ratta tat tat"
command removes everything after the spaces
output = "ratta"

How can I do this?
Originally posted by Zappy:
You can use string_pos(substr,str) to find the location of the first instance of substr within str. Then you can delete everything from that character and onwards. Tested example:
//blah is the string variable, temp is the character position of the first space in blah var temp=string_pos(" ",blah); //Set temp to the first space in blah if temp!=0 //If a space is found... blah=string_delete(blah,temp,string_length(blah)-temp+1); //...delete the space and everything afterwards
Edit: I've changed if string_pos(" ",blah)!=0 to if temp!=0 as temp is already the result of that function. I've also changed a custom_string_var to blah, as I originally had it as the former, then changed all but one occurence of it to the latter, forgetting the last one at the time. I've also added a +1 to the length of string_delete, and changed "untested example" to "tested example".
< >
Showing 1-11 of 11 comments
maras Apr 16, 2018 @ 9:52pm 
string_replace(yourString, " ", ""); // you will replace space ( " " ) with nothing ( "" )
Last edited by maras; Apr 16, 2018 @ 9:53pm
PaperClipMan Apr 17, 2018 @ 4:28am 
Originally posted by marasovec:
string_replace(yourString, " ", ""); // you will replace space ( " " ) with nothing ( "" )
Only replaces the space itself
PaperClipMan Apr 17, 2018 @ 4:42am 
Originally posted by marasovec:
Then its string_replace_all

That just removes all of the spaces

https://docs.yoyogames.com/source/dadiospice/002_reference/strings/string_replace_all.html
maras Apr 17, 2018 @ 4:44am 
Oh Im sorry. I think I didnt read it all. I have something like that in my older game I'll try to find it
maras Apr 17, 2018 @ 5:07am 
var string_ = "";

for ( var i = 0; i < string_length(str1)); i++)
{
var st = string_char_at(str1, i);
if st = " "
{
str1 = string_;
break;
}
else string_ += st;
}


Last edited by maras; Apr 17, 2018 @ 5:08am
maras Apr 17, 2018 @ 5:10am 
Its adding characters until first space appears. Then replace the original string and breaks
Last edited by maras; Apr 17, 2018 @ 5:12am
The author of this thread has indicated that this post answers the original topic.
Zappy Apr 17, 2018 @ 5:14am 
You can use string_pos(substr,str) to find the location of the first instance of substr within str. Then you can delete everything from that character and onwards. Tested example:
//blah is the string variable, temp is the character position of the first space in blah var temp=string_pos(" ",blah); //Set temp to the first space in blah if temp!=0 //If a space is found... blah=string_delete(blah,temp,string_length(blah)-temp+1); //...delete the space and everything afterwards
Edit: I've changed if string_pos(" ",blah)!=0 to if temp!=0 as temp is already the result of that function. I've also changed a custom_string_var to blah, as I originally had it as the former, then changed all but one occurence of it to the latter, forgetting the last one at the time. I've also added a +1 to the length of string_delete, and changed "untested example" to "tested example".
Last edited by Zappy; Apr 18, 2018 @ 5:50am
maras Apr 17, 2018 @ 5:17am 
Originally posted by Zappy:
You can use string_pos(substr,str) to find the location of the first instance of substr within str. Then you can delete everything from that character and onwards. Untested example:
//blah is the string variable, temp is the character position of the first space in blah var temp=string_pos(" ",blah); //Set temp to the first space in blah if string_pos(" ",blah)!=0 //If a space is found... blah=string_delete(custom_string_var,temp,string_length(blah)-temp); //...delete the space and everything afterwards
Yeah this is probably a better way
PaperClipMan Apr 17, 2018 @ 6:15pm 
Originally posted by Zappy:
You can use string_pos(substr,str) to find the location of the first instance of substr within str. Then you can delete everything from that character and onwards. Untested example:
//blah is the string variable, temp is the character position of the first space in blah var temp=string_pos(" ",blah); //Set temp to the first space in blah if string_pos(" ",blah)!=0 //If a space is found... blah=string_delete(custom_string_var,temp,string_length(blah)-temp); //...delete the space and everything afterwards

thanks, works great now, but an 'n' is added to my example output. this is what I used:
var temp = string_pos(" ",user); //Set temp to the first space in user if string_pos(" ",user)!= 0 { //If a space is found, delete the space and everything afterwards global.user = string_delete(user,temp,string_length(user)-temp); } else global.usersys = user;
I couldn't figure out why, so I wanted to ask why this would be before I messed with it.
Zappy Apr 18, 2018 @ 5:52am 
Originally posted by _Sliding:
thanks, works great now, but an 'n' is added to my example output. -
This means that the example input had a space somewhere and had an "n" at the end. If you had used a "t" at the end, it would be a "t" instead.


First, change if string_pos(" ",user)!= 0 to if temp!= 0.
This has nothing to do with the error, temp just already stores that function's result, so this is probably faster/more efficient/more optimized.

Then, change string_length(user)-temp to string_length(user)-temp+1.
This is the actual cause of the error. It was one character too short. Good thing I included "untested" when I said "untested example".

(I have now edited my previous comment to reflect both of these things.)


Also, just to make sure, is it intentional that you're affecting global.user if there is a space in the string, but global.usersys if there isn't?
And is there a particular reason that you are using curly brackets ( { } ) around the if case's thing but not the else case's thing, when in both cases only one thing is done as a result?
PaperClipMan Apr 18, 2018 @ 7:38am 
Ah. Thanks.

Originally posted by Zappy:
Also, just to make sure, is it intentional that you're affecting global.user if there is a space in the string, but global.usersys if there isn't?
And is there a particular reason that you are using curly brackets ( { } ) around the if case's thing but not the else case's thing, when in both cases only one thing is done as a result?

Global.user does not exist, that was my mistake when I edited the code to post. As for the curley brackets.. that’s just an odd habit ¯\_(ツ)_/¯
Thanks for pointing it out though
< >
Showing 1-11 of 11 comments
Per page: 1530 50

Date Posted: Apr 16, 2018 @ 5:48pm
Posts: 11