Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I remove part of a string?

Asked by
lucas4114 607 Moderation Voter
8 years ago

I know how to add onto strings, it would be this for example: String.Value = "Hi"..Player.Name.."!" But how do I remove parts of a string an exmple would be I have something named "Player's_House" and I want to remove "'s_House" off of it?

0
It's probably a wise idea to just remember the player's name in the first place (elsewhere) BlueTaslem 18071 — 8y

1 answer

Log in to vote
5
Answered by 8 years ago

This is easily accomplished with patterns! The function you need is string.gsub. This function takes in a string to replace in, a pattern, a replacement string, and optionally a limit of the number of substitutions. Patterns, which are similar in function to Regex (though far more lightweight), are a way to search for various substrings based on specified information. In this case, however, you just want to search for a specific string, "'s_House", and remove it. Thus, your code would be as follows:

string.gsub("Player's_House", "'s_House", "")

Further reading on patterns: http://www.lua.org/pil/20.2.html Further reading on the string library: http://lua-users.org/wiki/StringLibraryTutorial

0
So I would do: String.Value = String.gsub("Player's_House", "'s_House", "") lucas4114 607 — 8y
0
Is String a StringValue? That's pretty poor naming. Also, capitalization matters, it should be 'string.gsub'. CoffeeFlux 55 — 8y
0
String is a string value.. lucas4114 607 — 8y
Ad

Answer this question