Captilizing first letter after punctuation

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
2 Likes

It works but how would i prevent it from thinking richtext tags are punctuation?

Do you set richtext tags manually, from a script or is it set by the player?

1 Like

Richtext tags are set from a script.

Then you could just set the richtext tags after capitalizing letters

1 Like

Alright thank you ill go try it.

1 Like

Here’s how you could have done it with gsub:

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.

1 Like

Thanks ill use this since it’ll be better for performance.

Theres a problem with this though if theres a space after it then it won’t work and I don’t know where i would put %s.

I’m not exactly sure what you mean, adding a space anywhere in the string seems to work for me:

local String = " He l l o  !  a c b  "

local result = string.gsub(String,"%p.-%w",string.upper)
print(result) --  He l l o  !  A c b

This is what I mean.

local String = "Hello. Yes"
local result = string.gsub(String,"%p.-%w",string.upper)
print(result) -- Hello. yes

It prints “Hello. Yes” from what I see.
image

Oh weird because on my dialogue it wasn’t working correctly but now that i print it out it works correctly.

See it isn’t working on the gui but it works when its printed out.

Could you send a snippet of code where it sets the text of the gui?

					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

Try making the capitalizeLetters print the text of the dialogue label and see what it says

edit: Just make sure it’s setting the correct textlabel’s text in the function

This is what the output says:

This is a dialogue system which includes keywords and can be sped up and sped down. PRetty cool huh?

But the text isn’t the same

You’re probably just settings the text of the wrong textlabel