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

How do I remove parts of a string?

Asked by
lucas4114 607 Moderation Voter
7 years ago

How do I do things like remove the first character of a string, or remove the 4th character, or find parts of a string and remove it like removing "Delete me!" in the string: "Hello. Delete me!"??

2 answers

Log in to vote
1
Answered by 7 years ago

There are a lot of ways to delete characters from a string.

Here are my examples which use gsub:-

local word1 = "Hello. Delete me!"

print(string.gsub(word1,"Delete me!", "")) 
--output
--Hello.

function myCharAt(txt, num) 
    if string.len(txt) < num then
        return "number exceeds string len"
    end

    return string.gsub(txt, string.sub(txt,num,num), "")
end

print(myCharAt(word1,1))
-- output
--ello. Delete me!

Hope this helps

Ad
Log in to vote
3
Answered by 7 years ago
Edited 7 years ago

String manipulation


There are specific functions built into Lua that handle the operations you're describing. You can access these functions through the String library, or you can use them as methods on strings indirectly (make sure you have them enclosed around parenthesis, or it's in the form of a variable). Here's an example of something that would convert an entire string to lower case characters:

local test = "UPPER CASE STRING"
print(test:lower()) -- prints 'upper case string'

-- or, you could use the lower function from the string library, like so:

print(string.lower(test)) -- still prints 'upper case string' as lower case

Notice that using the method example of the code above, wouldn't work by calling the method on the string directly like this: print("Something":lower()) - This is invalid.

  • string.sub

The specific function you're looking for is string.sub. This function allows you to cut string values down to a certain length, the length in which you pass as the argument to the function. Here's an example:

local test = "String value! This part will get cut"

test = test:sub(1,13) -- '13' being where 'String value!' ends
print(test) -- prints 'String value!'

You can also use sub to cut a string at a given length, by passing only a single argument (which is the position in where it's getting cut off) Here's another example:

local test = "String value! Hello world"

test = test:sub(14) -- This is the position of the space between 'value!' and 'Hello'
print(test) -- this will print 'Hello world' only.

The example above will start the string at position 14, disposing of everything before it. This is very useful for creating something like admin commands, or anything based around a key/value relationship in a string.

  • string.find

Now, sometimes you might want to cut a string off at a certain point, but the length of that point will vary. For this, you can use string.find to return the length of where a certain character is located. Please note, this function actually returns 2 values:

  • Where the string starts (number)

  • Where the string ends (number)

You will only have to worry about the second value if the string you're passing to the function is more than 1 character. Here's an example:

local test = "string/value"
local valuePos = test:find("/") -- find the length where '/' is found

-- It'd be wise to use an if statement to see if the character
-- was found, so your script doesn't error.
if valuePos then
    test = test:sub(valuePos+1) -- Cut the string at the length 1 ahead of what was returned.
end

print(test) -- this will print 'value' from the string.

Questions

Hope this helped, if you have any questions just let me know. I also highly suggest you look into string patterns as well, they're very fun to play around with (despite how intimidating it may look at first).

Answer this question