But that’s what it’s doing? It’s really hard to tell what you are trying to do. I input those strings into the function and it outputted those exact strings.
Your function outputs
“\\n \\trolling”
which is not equal to
“\n \trolling”
But it doesn’t? What code are you using?
The raw string data for what you outputed is
“\\n \\trolling”
I am talking about raw string data, not what is displayed after invoking the metamethods.
When I iterated through the string, it outputted what you wanted, and there aren’t 2 consecutive backslashes. What is “raw string data?”
The raw string data is like what you type into scripts, such as “\\” before it shows “\”
I don’t think this is possible, what you could do is set \
to a special Unicode character which you then can swap out to get your intended output. Escape characters will act like escape characters when set by a script.
Eg:
• = \
(you may not want to use a bullet point for this, but use a character that is highly unlikely to be used)
Raw string: “••n"
After swapping out: “\n”.
I don’t really believe you, that’s the problem
The very first reply told you how to do that.
I think you’re maybe confused about the syntax. Look at how "\\"
is a single character:
assert(string.len("\\") == 1) -- Just one backslash
assert(string.len("\\\\") == 2) -- Two backslashes
assert([[\]] == "\\") -- A different way to write a single backslash
print(string.gsub([[Some text with \ a single backslash \n and a newline and \\ two backslashes]], "\\\\", "\\"))
-- prints:
-- Some text with \ a single backslash \n and a newline and \ two backslashes
"text"
is a literal
[[text]]
is a raw literal, where no escaping happens
Tom is right, the problem he described is what I am facing. I am trying to replace a string “\\” with an escape character.
Maybe I understand. Is this a correct rephrasing of your question?
Given a string with the characters:
{h, e, l, l, o, \, n, w, o, r, l, d, !}
How I translate that into a string with the characters
{h, e, l, l, o, \n, w, o, r, l, d, !}
, where the “backslash + character” combination has been replaced by its associated escape sequence?
Perhaps this is what you wanted:
local escapes = {
["\\a"] = "\a",
["\\b"] = "\b",
["\\f"] = "\f",
["\\n"] = "\n",
["\\r"] = "\r",
["\\t"] = "\t",
["\\v"] = "\v",
["\\\\"] = "\\",
["\\\""] = "\"",
["\\'"] = "\'",
["\\["] = "\[",
["\\]"] = "\]",
}
local function AddEscapes(text)
local esc = string.gsub(text, "\\.", escapes)
return esc
end
local text = [[Hello\nWorld!]]
print(text) -- Hello\nWorld!
print(AddEscapes(text))
-- Hello
-- World!
Not quite as that references cases with the following character, I was hoping that in example for your list I could just do
["\\"] = “\”
Which is not quite easy due to the last character of the string getting ignored by the escape character.
"\\"
means a single backslash.
string.len("\\") == 1
"\\\\"
means two backslashes.
string.len("\\\\") == 2
"\n"
means a newline.
string.len("\n") == 1
I have no idea what you’re asking! As far as I can tell, every possible way to interpret your question has been answered in this thread! Good luck!
We have gone over this 50 times, no need to restate the same thing on loop. Read my question, and answer it. I know how escape characters work, and you do not need to change what I am trying to achieve.
Unfortontly, THIS is not what I am attempting to achieve. I am attempting to replace “\\” with “\” (examples would be what you are trying to type in a script.)
Here is the code I am trying to execute
string.gsub(line, "\\", "\")
But due to the string end being ignored I am unable to replace exactly \\ with \.
It is impossible for you to do what you are trying to do without doing what nicemike40 did here (manually handling every single possible of \ and a letter) due to how Lua handles magic characters.
I figured out a method,
http = game:GetService("HttpService")
line = "\\n" --test line
line = http:JSONEncode(line) print(line) --turn it to json
line = string.gsub(line, "\\\\", "\\") print(line) --modify json
line = http:JSONDecode(line) --turn it back into string
print(line)
line = "\\n"
line = string.gsub(line, "\\\\", "\\")
print(line) --\n
JSONEncode expects a table value & returns a string value, I’m not sure why you’re passing it a string value.
JSON is not only used for table values, but is capable of storing many types of values. JSONEncode does not expect table values, it is able to handle strings, number values, bools and table. An example being is if you have the number 651, JSON would simply encode it as
651
Another example for the boolean true
true
And for the example string I gave it would store it as
\\n
I hope you learnt something about JSON.
I’m aware of its ability to handle non-table types but sending it such values is an incorrect use of the method.
https://developer.roblox.com/en-us/api-reference/function/HttpService/JSONEncode
The JSONEncode function transforms a Lua table into a JSON object or array based on the following guidelines