So I’m trying to capitalize the first letter after punctuation in a string.
This is what I’ve done so far.
local function capitalizeLetters()
DialogueLabel.Text = string.gsub(DialogueLabel.Text,"%p",string.upper(string.sub("%p",1,3))..string.sub("%p",3))
end
And it gives me this error.
ReplicatedFirst.LocalScript.ClientModule:81: invalid use of '%' in replacement string
local function capitalizeLetters()
local text = DialogueLabel.Text
local x = string.find(text, "%p%s")
while x do
text = text:sub(0,x-1) .. text:sub(x, x+2):upper() .. text:sub(x+3, #text)
x = string.find(text, "%p%s", x + 1)
end
x = string.find(text, "%p%s")
while x do
text = text:sub(0,x-1) .. text:sub(x,x+1):upper() .. text:sub(x+2, #text)
x = string.find(text, "%p", x+1)
end
DialogueLabel.Text = text
end
local String = "Hello.acb"
local result = string.gsub(String,"%p.-%a",string.upper)
print(result) -- Hello.Acb
There were a few things going wrong in your code. The "%p" in the string.upper function doesn’t refer to a letter captured in the text, you would need to use the capture index: “%1”. You also should be passing the string.upper function itself, not what it returns. Lastly, the pattern %p only captures a punctuation character, you need to capture the letter too, so a pattern like %p.-%a would likely work better.
Edit: probably should use, the %a class over %w, so numbers aren’t matched too.
userInputService.InputBegan:Connect(function(input,gameProcessedEvent)
if gameProcessedEvent then return end
local mouseInput = {Enum.UserInputType.MouseButton1,Enum.UserInputType.MouseButton2,Enum.UserInputType.MouseButton3}
if table.find(mouseInput,input.UserInputType) then
if DialogueMainFrame.BackgroundTransparency == 0 then
if DialogueMainFrame:FindFirstChild("WritingFromFunction") then
DialogueSkippedValue.Value = true
DialogueLabel.Text = DialogueMainFrame:FindFirstChild("WritingFromFunction").Value
capitalizeLetters()
highlightKeywords()
else
DialogueSkippedValue.Value = true
DialogueLabel.Text = Text
capitalizeLetters()
highlightKeywords()
end
end
end
end)
and
for i = 0,string.len(Text) do
if DialogueSkippedValue.Value == false then
DialogueLabel.Text = string.sub(Text,1,i)
capitalizeLetters()
highlightKeywords()
wait(Interval)
end
end