Extra whitespace\numbers with string functions

Using some string functions will print out a random number of spaces and then, for example, in string.gsub will print out how many replacements made, and for string.trim it printed out 1 (I don’t get it, there were several newlines and spaces, but whatever)
Does this happen to everyone? I’ve only tried this client-side, any solutions?

This is some code lines I have problems with:

str is the SteamID string passed through the function, everything works as expect except for what it adds

string.Replace(string.Replace(str,“STEAM_”,""),":","")

  • works fine, except for newlines before and after

string.Trim(string.Replace(string.Replace(str,“STEAM_”,""),":",""))

  • prints whitespace and the number 1

Try using this to do that.

[lua]
string.gsub(str, “[:/\”*%?<>]", “_”)
[/lua]

string.Trim trims the spaces as a whole, whereas your usage of string.gsub probably removes them one at a time.

This:

[lua]str:gsub( “%s”, “” );[/lua]

Will replace each instance of whitespace with nothing. If str had, for instance, five spaces, five replacements would be done.

This:

[lua]str:gsub( “%s+”, “” );[/lua]

Will replace the longest possible substrings of whitespaces with nothing. If str had five whitespaces at the beginning, only one replacement would be done.

If that’s what you’re asking for, that is. I suggest you improve your English before coming here.

[lua]str:gsub( “[^%w_]+”, “_” );[/lua]

Will remove everything that is not alphanumeric or an underscore. Both methods work, though.