String Manipulation Subbing apostrophe

Hi,

   I’m trying to modify a string so that and I can use it with my REST API. I need to replace the escape characters. So far I’ve been able to do it with most characters, except the apostrophe. Here’s the code that I have.

someText = string.gsub(someText, "(%')", "%%27");

Any ideas on how I can replace the apostrophe? Thank you very much.

All the best,

Don

If I can’t find out a way of doing something I usually invent a long winded but working solution, this is what I would do…

I would convert the text string into a numerical string using string.byte

You are then working with numbers, the ascii code of each character.

You can then manipulate the number string and replace the escape characters and apostrophe easily.

Then convert it back to a text string using string.char

Just one way of doing it.

Thanks QuizMaster.

Happy to help, have fun.

your code as written would substitute the literal ’ with the literal %27

(so if that’s not what you want, then show us the two literal strings you DO want)

btw, you don’t need the parenthesis in that pattern, fe…

someText = "it's isn't won't" pattern = "%'" replace = "%%27" fixedText = string.gsub(someText,pattern,replace) print(fixedText)  

Thanks Davebollinger. I’ll give it a try.

I am trying to replace the literal ’ with the literal %27. Unfortunately this didn’t seem to work :frowning:

local someText = "it's isn't won't" local pattern = "%'" local replace = "%%27" local fixedText = string.gsub(someText,pattern,replace) print(fixedText) -- returns it's isn't won't

I would like fixedText to return “it%27s isn%27t won%27t”

For anyone who might be interested, this is the solution that I am currently using.

local textLength = tonumber(string.len( someText )); local byteText = {}; for i = 1, textLength do local byteNum = someText:byte(i); table.insert(byteText,byteNum); end local stringText = {}; for i = 1, #byteText do if (byteText[i] ~= 156 and byteText[i] ~= 152 and byteText[i] ~= 153 and byteText[i] ~= 226 and byteText[i] ~= 128)then table.insert(stringText, string.char(byteText[i])); elseif (byteText[i] == 156)then table.insert(stringText, "%22"); elseif (byteText[i] == 152 or byteText[i] == 153)then table.insert(stringText, "%27"); end end someText = table.concat(stringText);

That’s how I would have done it.

Glad you got your result.

weird, works as expected for me, wonder if your source is unicode?

If I can’t find out a way of doing something I usually invent a long winded but working solution, this is what I would do…

I would convert the text string into a numerical string using string.byte

You are then working with numbers, the ascii code of each character.

You can then manipulate the number string and replace the escape characters and apostrophe easily.

Then convert it back to a text string using string.char

Just one way of doing it.

Thanks QuizMaster.

Happy to help, have fun.

your code as written would substitute the literal ’ with the literal %27

(so if that’s not what you want, then show us the two literal strings you DO want)

btw, you don’t need the parenthesis in that pattern, fe…

someText = "it's isn't won't" pattern = "%'" replace = "%%27" fixedText = string.gsub(someText,pattern,replace) print(fixedText)  

Thanks Davebollinger. I’ll give it a try.

I am trying to replace the literal ’ with the literal %27. Unfortunately this didn’t seem to work :frowning:

local someText = "it's isn't won't" local pattern = "%'" local replace = "%%27" local fixedText = string.gsub(someText,pattern,replace) print(fixedText) -- returns it's isn't won't

I would like fixedText to return “it%27s isn%27t won%27t”

For anyone who might be interested, this is the solution that I am currently using.

local textLength = tonumber(string.len( someText )); local byteText = {}; for i = 1, textLength do local byteNum = someText:byte(i); table.insert(byteText,byteNum); end local stringText = {}; for i = 1, #byteText do if (byteText[i] ~= 156 and byteText[i] ~= 152 and byteText[i] ~= 153 and byteText[i] ~= 226 and byteText[i] ~= 128)then table.insert(stringText, string.char(byteText[i])); elseif (byteText[i] == 156)then table.insert(stringText, "%22"); elseif (byteText[i] == 152 or byteText[i] == 153)then table.insert(stringText, "%27"); end end someText = table.concat(stringText);

That’s how I would have done it.

Glad you got your result.

weird, works as expected for me, wonder if your source is unicode?