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 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
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?
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.
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)
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
It isn’t an incorrect way to use it, it just is terribly inefficient because JSON can interpret strings and numbers as Lua can. You’re calling two functions redundantly when you can achieve the exact same thing with proper string escape patterns. Not to mention JSON decoding it interprets it as the line break character instead of literally “\n” so you aren’t even achieving what you want to. Not to mention your method is 20x (2000%!) slower.
local clock = os.clock()
http = game:GetService("HttpService")
local line = "\\n"
line = http:JSONEncode(line)
line = string.gsub(line, "\\\\", "\\")
line = http:JSONDecode(line)
local _end = os.clock()
print('HTTP took', _end - clock)
print(line)
task.wait(1)
local clock = os.clock()
local line = "\\n"
line = string.gsub(line, "\\\\", "\\")
local _end = os.clock()
print('No HTTP took', _end - clock)
print(line)
yours:
04:43:50.988 HTTP took 0.000025999994250014424 - Edit
04:43:50.988
- Edit
no httpservice:
04:43:51.990 No HTTP took 0.0000012000018614344299 - Edit
04:43:51.990 \n - Edit
My method actually achieves exactly what I want to do. Talk to me about speed once you find a method that does what mine does. In addition, you have said what everyone else has said on this thread which in each case I have explained that what you did is not what I’ve been trying to do.
Okay, well if you listened to what literally everyone else has been saying and taken advice given by people who are voluntarily providing you with quality information based on previous experience instead of asking for support and then being outraged when it isn’t exactly what you asked for, and then accepting a response that is both inefficient and an improper use of the method, maybe you would have received a response that does what you’re asking for in a proper and efficient way.
Anyway, now that you’ve doubted literally everyone who has taken their time out of their day to provide you with information’s competence despite every one of their responses being competent and their responses being literally what you ask for, let’s talk speed, why don’t we?
So, by your response, you’re telling me that inputting “\\n” should output the line break character and not literally “\n”. That is literally what nicemike40 (one of the most competent people I’ve ever interacted with on this forum FYI) said in response 45, their response results in exactly what you’re looking to achieve.
your method:
19:15:14.931 HTTP took 0.000016799999684735667 - Edit
19:15:14.931
- Edit
nicemike40s method:
19:15:15.943 No HTTP took 0.0000039000005926936865 - Edit
19:15:15.943
- Edit
Look at that, we were able to define a function, a table containing 12 entries, a variable, and a call to a function 400% faster than we were able to get a service, json encode and json decode a string. The output is identical.
Testing code
local clock = os.clock()
http = game:GetService("HttpService")
local line = "\\n"
line = http:JSONEncode(line)
line = string.gsub(line, "\\\\", "\\")
line = http:JSONDecode(line)
local _end = os.clock()
print('HTTP took', _end - clock)
print(line)
task.wait(1)
local clock = os.clock()
local escapes = {
["\\a"] = "\a",
["\\b"] = "\b",
["\\f"] = "\f",
["\\n"] = "\n",
["\\r"] = "\r",
["\\t"] = "\t",
["\\v"] = "\v",
["\\\\"] = "\\",
["\\\""] = "\"",
["\\'"] = "\'",
["\\["] = "\[",
["\\]"] = "\]",
}
local function AddEscapes(text)
return string.gsub(text, "\\.", escapes)
end
local line = "\\n"
line = AddEscapes(line)
local _end = os.clock()
print('No HTTP took',_end - clock)
print(line)