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

I am trying to replace \ twice with the special character \.

Yes, and two \ refers to ONE , not two. To replace 2 with 1, it would be

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

So then what is \ alone if \\ is one \?

Honestly, I have absolutely no idea.

\ is a magic character such as used for \n, \", \’ or some character that you want to have a special meaning or be ignored. What I am trying to do here is say the input is \\n for example I want to turn it into \n. (\n is just an example and not the only case I am using it for)

No, like please explain your whole pipeline. How do you get input? When do you process it? Where are you displaying the transformed string? Give an example of what the pipeline looks like currently, and what you would rather it look like.

Because the answer to this exact problem was answered by @NyrionDev in the first reply. You need to provide as many details about your setup and problem as possible.

I get the input from a string value, which is set by a text box. After which it can be run by the emulator which decodes it into things that the emulator will use, eventually ending up being sent to a specified output event. If you woundering what the emulator is its a lua emulator for strings.

What is an example input string that is giving you problems? What is its transformation at each step, and what do you want it to be instead at each step?

I’m just pretty confused what your problem is. A backslash doesn’t mean anything special once its actually in a string’s data. If your string has a single backslash in it, it just has one. It’s fine.

Backslashes only matter in string literals, which it doesn’t sound like you actually have.

I’m not certain you actually have a problem here.

For example, I have this setup:

image

With this code:

local box = script.Parent.TextBox
local label = script.Parent.TextLabel

local function Update()
	label.Text = box.Text -- no transformations necessary
end

Update()
box:GetPropertyChangedSignal("Text"):Connect(Update)

(Basically just match up the TextLabel’s Text with the TextBox’s as I enter it)

It works fine. Backslashes don’t matter and are treated like any other character:

image

Here is exactly what I am trying to accomplish:
(This is all in string literals)

There starting string is

“\\”

I am trying to turn it into

“\”

If you’re trying to replace "\\n" with "\n" in a TextBox entry, you must understand this:

One backslash is escaping the end of the quote.

To stop this escape, you must escape the backslash by adding another backslash.

Therefore, "\\" as a variable is the same as just "\" in a user-inputted TextBox Text field.

So, to escape two backslashes, you must escape each backslash.

Therefore, "\\\\", which can be explained as "\\ \\" -- escaping each backslash = "\\" in a user-inputted TextBox Text field.

So to change "\\" into "\", you have to do this:

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

Which is just what the first reply by @NyrionDev said.

1 Like

This is not what I am trying to do, I understand that on display “\\” == “\\\\”,
I am talking off display about the actual raw data…

What do you mean? I tried a test string and it worked just fine:

local line = "\\n \\trolling"
local newLine = string.gsub(line, "\\\\", "\\")
print(newLine)
3 Likes

That is not what I am trying to accomplish. I am trying to turn for example:
“\\n” > “\n”
“\\’” > “\’”
‘\\"’ > ‘\"’
“\\e” > “\e”
(as you would type in a script)

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.

1 Like

Your function outputs

“\\n \\trolling”

which is not equal to

“\n \trolling”

But it doesn’t? What code are you using?
image

2 Likes

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.

image
When I iterated through the string, it outputted what you wanted, and there aren’t 2 consecutive backslashes. What is “raw string data?”

1 Like

The raw string data is like what you type into scripts, such as “\\” before it shows “\”