How to replace "\\" with "\" in strings?

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 :slight_smile:

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!
1 Like

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.

1 Like

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.

1 Like

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

1 Like

Please explain why parsing non-table values is an incorrect usage of JSON?

In regards to your first message, if you execute the code that is given you can see what it does.

Couldn’t you just use the double bracket notation to define the string?

local line = [[\\n]]
local line2 = string.gsub(line, [[\\]],[[\]])
print(line2) --> \n

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. :slight_smile:

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)