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

While making a Lua emulator for my game I am attempting to replace “\\” with a special identifier.

string.gsub(line, "\\","\")

though due to \" causing the string to end I am not able to replace “\\” with the special identifier.

Unfortunately I am unable to find any solutions for my problem.

4 Likes

\\ is \ and \ is used to escape special characters(such as ", ', \), instead you should translate them as following:

local line = "\\\\" --to avoid having to escape special characters, add a property here, for example TextLabel.Text
local result = line:gsub("\\\\", "\\") --\\\\ is \\ and \\ is \ 
print(result)
6 Likes

Unfortunately this solution would not work for me,
I am attempting to make a LUA Emulator and I want to have the actual magic character incase in the code editor a player types in

local var = "Test \n Test"

which when in the emulator would start out as "Test \\n Test " which I am attempting to turn it into simply "Test \n Test " with the special string formatting.

1 Like

Try this:

local line = Emulator.Line.Text --no need to escape anything here if it's a property
local result = line:gsub("\\\\", "\\")
print(result)
2 Likes

I think you are confused, what the player typing in on their screen is “\”, what you believe what they are typing in is “\\”

The base string is going to be “\\” for what they are typing in, I am attempting to change it to “\”

1 Like

That’s incorrect, you’re unaware that \ is an escape character, lua translates my \\\\ as \\ and \\ as \, as I said try applying my code snippet to see if it works.

I am aware that \ is an escape character, I want to change \\ into the escape character.

use string.gsub as @NyrionDev said, but, use it like this:

local line = "Test \\\\\\\\\\\\\n test"
line = line:gsub("%\\+", "\\")
print(line)

I ran that code except slightly modified it:

local line = "Test \\n test"
line = line:gsub("%\\+", "'\'"):gsub("%'+", "")
print(line)

And got the output of

Test n test

I am expecting the output of

Test
Test

Which would be “Test \n Test”

I went through to try and find any errors, it seems like it’s because of the way Roblox does their strings. fixed code:

local line = "Test \\\\\n test" 
line = line:gsub("%\\+", "\\") 

local num = #line:split("\\") 

if line:find("\n") then 
    line = line:gsub("\\","", 1) 
end 

print(line)

This is a solution for “\n” but I am looking for a solution specifically without referencing the following character for “\”. Such as in a case that a single gsub would for for say “\’”, “\”", “\n” “\”, ect.

oh, if that was the problem, then you could simply replace it with the identifier with the first 2 lines.

local line = "Test \\\\\ test" 
local identifier = "i"
line = line:gsub("%\\+", identifier) 
print(line)

Is there a way to replace the raw data of “\\” with “\” ?

that’s exactly what that script does, though? Or do you mean replacing 2 “” with 1?

When I ran your code it outputed

Test i test

that’s what I set it to print, if you meant for it to print 1 “” then change the identifier to “\”

local line = "Test \\\\\ test" 
local identifier = "\\"
line = line:gsub("%\\+", identifier) 
print(line)

I don’t quite understand what you’re asking, but maybe this is helpful:

a = [[\n\]] -- you can define raw literal strings with [[ ]]
print(a) -- \n\

I think it would also help if you walked through, step-by-step, exactly what the problem is. What you’re seeing, and what you want to see. It seems like you’re asking a question involving TextLabels, but I can’t be sure.

How would I set the identifier to “”? Doing

local identifier = "\"

Would not work because the \ makes the interpreter ignore the end of the string.

I am trying to use

string.gsub(line, "\\","\")

But due to the \ making the interpreter ignore the end of the string it is impossible.

In other words I am trying to replace an ignored magic character with a magic character.

doing \ once is is a special character, doing \ twice, refers to the letter/whatever itself, so, it would be

local identifier = "\\"