How would I remove every character from a string that is not a capital character using code?
(“Hello World”) > (“HW”)
How would I remove every character from a string that is not a capital character using code?
(“Hello World”) > (“HW”)
Not sure if this is the best way, but it works:
local function GetUpperCase(Text:string)
local Uppercase = {}
for index, Character:string in pairs(string.split(Text,'')) do
if string.upper(Character) == Character then
Uppercase[index] = Character
end
end
return table.concat(Uppercase,''), Uppercase
end
This function should return a string that contains all the uppercase characters, and then an array of the characters
local OnlyUppercase:string, ListOfCharacters = GetUpperCase('Frodev is SO cool')
print(OnlyUppercase) -> "FSO"
Hey there! String functions can save your day! They are highly optimized, so usually you shall seek for a string function that works!
string.gsub("Hello World!", "[%l%d%p ]", "")
The code above will return HW
string.gsub("Goodbye World!", "[%l%d%p ]", "")
The code above will return GW
If you’re wondering how it works, I’m simply using the string gsub function, which replaces certain parts of a string with whatever you’d like. I simply make it find spaces, lowecase letters and other special symbols like !
and have it replace it with an empty string: ""
.
If you’d like further understanding of string patterns for future use, Roblox has an official documentation here.
I knew there was someway to go about using gsub and formatting, I just havent figured it out lol.
On the subject of this, what are all the formatting options? There are so many I cant keep up with it.
Def use this over my reply, its much better.
thank you for the quick reply!
As I mentioned in my now edited message, Roblox has a documentation on most of it here.
I’d recommend going through the full String Pattern Reference
section for a full understanding of it.
Ok cool, thanks!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.