storing a string with '#' in it in rails [duplicate] - ruby-on-rails

This question already has an answer here:
Ruby string prepend '\' character
(1 answer)
Closed 7 years ago.
I tried storing a string in rails like
string = 'abc#$123'
but the string stores "abc\ #$123". I tried removing "\" by using string.delete("\",'') but didn't work
Is there any way to solve this problem ?

This is correct, Ruby interpreter is just escaping the character #$ by using backslash (\) character.
It is not exactly changing your string and adding the unwanted (\) character. You can verify this by doing puts string and it should print abc#$123

The "\" is a way to escape the # character, which has a special meaning in ruby. So the "\" does not actually exist, it is just a convenience used by ruby to store your string value.
So, don't bother about it. You will see that if you print your string, the "\" will magically disappear.
irb(main):001:0> s = 'abc#$123'
=> "abc\#$123" # internal representation of your string
irb(main):002:0> print s
abc#$123=> nil # printed string value

Related

What is difference between escape sequence in Single quotes and Double quotes in Ruby [duplicate]

This question already has answers here:
Double vs single quotes
(7 answers)
Closed 6 years ago.
I have the following string and want to replace \n with < br>
string = "Subject: [Information] \n Hi there, \n...\n\n\n\\ud83d\\ude00\n--\n"
string.gsub('\n','<br>') #don't work
Than I tried
string.gsub("\n",'<br>')
It works, can explain this to me. String in double quotes are different from string in single quotes or it has to do with escape sequence.
Thanks
Single quotes preserve characters like \ as-is. Double quotes interpolate them, where \n means newline-character.
There's others, many of these coming from things like C.

SWIFT string with special characters without escape

How to print all special characters without inserting escape sign before every of them? I have very large textiles with many special characters and I'm looking for something like # in c# which prints string literally as it is
What you're referring to, is called a verbatim string literal in C# and that concept does not translate exactly to Swift.
However, with the introduction of multiline string Literals in Swift 4, you can get close.
let multilineString = """
Here you can use \ and newline characters.
Also single " or double "" are allowed.
"""
For reference, find the grammar of a Swift String literal here.

concatenate backslash in swift [duplicate]

This question already has answers here:
(Swift) how to print "\" character in a string?
(4 answers)
Closed 7 years ago.
How do you concatenate a backslash in swift?
i.e. "some string" + "\"
escaping the backslash gives me "some string\\" but I want "some string\"
Any ideas on how to accomplish this?
EDIT: I don't want to print out the string, I just want to concatenate the backslash. Escaping the backslash will store the string with two backslashes but I only want one.
EDIT 2: I think I figured it out. I used "\"" and that seems to work for me.
The double backslash solution is correct (see console output in my small sample)
Duplicate
(Swift) how to print "\" character in a string?
But..
If you're in a playground and type:
print("\\Hello, World")
... then yes both slashes and even a newline character appear in the results to the right as:
\\Hello, World\n
but that's not the same as actual output.
If you open the debug console (Cmd-Shit-Y), or click the tiny eyeball icon that shows you the output, you'll see that there's only one \ and the escape works as expected.
For concantenation & string interpolation you could do...
var string = "some string"
print("\(string)\\") //prints "some string\"
Or...
string +="\\"
print(string) //also prints "some string\"
Again, the right-column preview output is slightly different, so you have to look at the console. This is a somewhat confusing & annoying feature of Playgrounds. Not sure why they don't ensure both preview & console output are the same, or show the debug console by default.

How to define a ruby array that contains a backslash("\") character?

I want to define an array in ruby in following manner
A = ["\"]
I am stuck here for hours now. Tried several possible combinations of single and double quotes, forward and backward slashes. Alas !!
I have seen this link as well : here
But couldn't understand how to resolve my problem.
Apart from this what I need to do is -
1. Read a file character by character (which I managed to do !)
2. This file contains a "\" character
3. I want to do something if my array A includes this backslash
A.includes?("\")
Any help appreciated !
There are some characters which are special and need to be escaped.
Like when you define a string
str = " this is test string \
and this contains multiline data \
do you understand the backslash meaning here \
it is being used to denote the continuation of line"
In a string defined in a double quotes "", if you need to have a double quote how would you doo that? "\"", this is why when you put a backslash in a string you are telling interpretor you are going to use some special characters and which are escaped by backslash. So when you read a "\" from a file it will be read as "\" this into a ruby string.
char = "\\"
char.length # => 1
I hope this helps ;)
Your issue is not with Array, your question really involves escape sequences for special characters in strings. As the \ character is special, you need to first prepend it (escape it) with a leading backslash, like so.
"\\"
You should also re-read your link and the section on escape sequences.
You can escape backslash with a backslash in double quotes like:
["\\"].include?("\\")

How do I remove this backslash in Ruby

How do I remove this backslash?
s = "\""
I have tried s.gsub("\\", "") and that doesn't remove it, it returns the same string.
there's actually no backslash character in your String. The Backslash in your example simply escapes the following double quote and prevent's that it would terminate the string and thereby resulting in a syntax error (unterminated double quote ).
So what you see when you print that string in IRB is actually not the backslash as is, but the backslash in combination with the following dobule quote as an indication that the double quote is escaped. Kind of hard to grasp when you encounter it the first time. Have a look at http://en.wikibooks.org/wiki/Ruby_Programming/Strings#Escape_sequences
long story short: there is no backslash in your string so you can't remove it :)
gsub takes a regular expression as the first parameter. I believe that if you pass it a string, it will first convert it into a regex. This means you need extra escaping:
s.gsub("\\\\", "")
If you use regex notation, you can stop it from doubling up:
s.gsub(/\\/, "")
This is because you don't have to escape twice: once because double-quoted strings need you to escape the \ character, and once because the regular expression requires you to as well.
that's actually an escape quote sign (do a print s to see it)
I'm not sure if this is a solution to YOUR problem, but seeing that this is one of the first SO questions I looked at when trying to solve my problem and have in fact, solved it, here is what I did to fix my problem.
So I had some CSV.read output with a load of \ (backslashes) and unwanted quotation marks.
arr_of_arrays = CSV.read("path/to/file.csv")
processed_csv = arr_of_arrs.map {|t| eval(t)}
the key here is the eval() method.

Resources