Removing leading and trailing double quotes in string in rails - ruby-on-rails

I want to trim the leading and trailing quotes of a string without any replacement.. I have tried with gsub.. but nothing helped.. I want to achieve something like., "hai" to hai
In Java, I ll use like the following.,
String a="\"hai";
String z=a.replace("\"", "");
System.out.println(z);
Output:
hai
How can I achieve this in rails? Kindly pls help..
In my irb
2.2.3 :008 > str = "\"hai"
=> "\"hai"
2.2.3 :009 > str.tr!('\"', '')
=> "hai"
Why am I not able to get output without double quotes?? Sorry ., If my question doesn't meet your standard..

You can also use .tr method.
str = "\"hai"
str = str.tr('\"', '')
##OR
str.tr!('\"', '')
## OUTPUT
"hai"

You can pass a regex instead, try this
str = "\"hai"
str = str.gsub(/\"/, '')
Hope that helps!

This removes the leading and trailing double quotes from the string only. You get a new string and keep the old one.
str = "\"ha\"i\""
# => "\"ha\"i\""
new_str = str.gsub(/^"+|"+$/, '')
# => "ha\"i"
str
# => "\"ha\"i\""
Or you change the original string.
str.gsub!(/^"+|"+$/, '')
# => "ha\"i"
str
# => "ha\"i"
That's a ruby convention. Method names with an exclamation mark/point modify the object itself.

This should work:
str = "\"hai"
str.tr('"', '')
Note that you only escape (\") double-quotes in a string that is defined using double-quotes ("\""), otherwise, you don't ('"').

Related

How to strip white spaces from a string containing HTML content

I could easily strip white space in the left side of a normal string like this:
" Remove whitespace".lstrip #=> "Remove whitespace"
But if the string contains HTML I am not able to do it:
#html_str = "<p> Remove whitespace </p>"
#html_safe_str = #html_str.html_safe #=> " Remove whitespace"
#html_safe_str.lstrip #=> "<p> Remove whitespace</p>"
#html_str.html_safe.lstrip is not working.
I dont want to remove all the s in the string. I want to remove all the s immediately after the opening <p> tag
Expected result: "Remove whitespace "
How is it possible?
In Rails 4, strip_tags has been combined with gsub. You can use gsub to remove s. Then, you need to use sanitizer method provided by ActionView:
#html_str = "<p> Remove whitespace </p>"
str = #html_str.gsub(/<p>\s*( \s*)+/, "<p>")
ActionView::Base.full_sanitizer.sanitize(str).lstrip # => "Remove whitespace "
It will work for Rails 3 as well as Rails 4.
You can do like this:
#html_str = "<p> Remove whitespace</p>"
#html_str = #html_str.gsub(" ", "") => "<p> Remove whitespace</p>"
#html_str1 = ActionController::Base.helpers.strip_tags(#html_str) => " Remove whitespace" #if you are doing this at controller level
#html_str1 = strip_tags(#html_str) => " Remove whitespace" #if you are doing this at view level
#now at the last
#html_str2 = #html_str1.strip => "Remove whitespace"
Shortly:
#html_str = "<p> Remove whitespace</p>"
#html_str1 = ActionController::Base.helpers.strip_tags(#html_str.gsub(" ", "")).strip => "Remove whitespace"
Hope this will work for you
If html_safe converts 's to white spaces, you can try:
#html_str.html_safe.lstrip
Hope that helps!
You can use gsub for that
#html_str = "<p> Remove whitespace</p>"
#html_str = #html_str.gsub(" ", "")
#html_safe_str = #html_str.html_safe
or
If you completely want to remove tags then as Andrey suggested, you can use strip_tags, but to use it in controller you need to call using ActionController::Base
#html_str = ActionController::Base.helpers.strip_tags("<p> Remove whitespace</p>").gsub(" ", "").lstrip
It will output : Remove whitespace
Hope this helps!
You can do it like this:
ActionController::Base.helpers.strip_tags(#html_str.html_safe).gsub(" ","").lstrip
# => "Remove whitespace"

how to get key/value from xpath string

I'm working on ruby on rails project and I have a string
cmd = "\"//div/table/tbody/tr/td/label[text()=\"Select Year\"]/preceding-sibling::*[1]\" = \"2014\""
I want to get key/value like this:
key: "//div/table/tbody/tr/td/label[text()=\"Select Year\"]/preceding-sibling::*[1]"
value: "2014"
The key is a xpath. I was using cmd.split("=") which is not correct. I think i can use regex to parse the string but don't know how. Please advice.
Thank you in advance!
using split will work for you .
2.1.1 :006 > cmd = '"//div/table/tbody/tr/td/label[text()=\"Select Year\"]/preceding-sibling::*[1]" = "2014"'
=> "\"//div/table/tbody/tr/td/label[text()=\\\"Select Year\\\"]/preceding-sibling::*[1]\" = \"2014\""
2.1.1 :007 > cmd.split(" = ")[0]
=> "\"//div/table/tbody/tr/td/label[text()=\\\"Select Year\\\"]/preceding-sibling::*[1]\""
2.1.1 :008 > cmd.split(" = ")[1]
=> "\"2014\""
save the first as key and the second as value.
Now, i got solution. See below:
my original string:
a = "\"//div/table/tbody/tr/td/label[text()=\\\"Select Year\\\"]/preceding-sibling::*[1]\" = \"2014\""
I'm using:
a.match('[\w\W]*\"[\s]*=')[0]
to get string:
"\"//div/table/tbody/tr/td/label[text()=\\\"Select Year\\\"]/preceding-sibling::*[1]\" ="
Then i can use substring to get the rest string.

How do you use variables in regex?

I am trying to pull from this string the photo ID : 30280 :
"--- !ruby/struct:PhotoJob \nimage_id: 30280\n"
I've seen this sort of thing done in regex before where you can look for a couple parameters that match like /nimage_id: \d/ and then return \d.
How can I return /d or the number 30280 from that string?
What's funny is that you have a Ruby Struct there, so you could do the following and let YAML take care of the parsing.
PhotoJob = Struct.new(:image_id)
job = YAML.load("--- !ruby/struct:PhotoJob \nimage_id: 30280\n")
job.image_id
=> 30280
use group matches "--- !ruby/struct:PhotoJob \nimage_id: 30280\n".scan(/image_id: (\d+)/)[0]
>> matches = "--- !ruby/struct:PhotoJob \nimage_id: 30280\n".match(/struct:(.*) .*image_id: (\d+)/m)
=> #<MatchData "struct:PhotoJob \nimage_id: 30280" 1:"PhotoJob" 2:"30280">
>> matches[1]
=> "PhotoJob"
>> matches[2]
=> "30280"
str = "--- !ruby/struct:PhotoJob \nimage_id: 30280\n"
image_id = str.scan(/\d+/)[0]
#=> "30280"
RE = '\nimage_id: (\d+)\n'
The group defined by parentheses around \d+ catches the number

ruby string escape when adding

In Ruby on Rails,I got a string number is made of 3 parts : prefix , counter , suffix
In model Setup:
def self.number
prefix = setup.receipt_prefix.blank? ? "" : setup.receipt_prefix.to_s
counter = setup.receipt_counter.blank? ? "" : setup.receipt_counter+1
suffix = setup.receipt_suffix.blank? ? "" : setup.receipt_suffix.to_s
each individual string shows fine:
puts prefix
=> \#_
puts counter
=>
1234
puts suffix
=>
#$#s
but when I add 3 string together, an addition back slash appear :
prefix + counter + suffix
=>
\\#_1234\#$#s
how can I escape "#" "\" when I add 3 string together ? like
=>
\#_1234#$#s
any Ruby or Rails's helper I can use in the model?
thx~~
The string will look different if you get the value versus print (puts) it out. See the following irb session.
>> a = "\\#_"
=> "\\#_"
>> puts a
\#_
=> nil
>> b = "1234"
=> "1234"
>> puts a + b
\#_1234
=> nil
>> a + b
=> "\\#_1234"
The actual string value has two backslashes in it. But only one shows up if you print the string.

Replace white space with AND in ruby

I have someone entering a form with some string input. What I need to do is replace any white space in the string with " AND " (no quotes). What's the best way to do this?
Also, how would I go about doing this if I wanted to remove all the whitespace in the string?
Thanks
to replace with and:
s = 'this has some whitespace'
s.gsub! /\s+/, ' AND '
=> "this AND has AND some AND whitespace"
to remove altogether:
s = 'this has some whitespace'
s.gsub! /\s+/, ''
=> "thishassomewhitespace"
Split and join is another technique:
s = " a b c "
s.split(' ').join(' AND ')
# => "a AND b AND c"
This has the advantage of ignoring leading and trailing whitespace that Peter's RE does not:
s = " a b c "
s.gsub /\s+/, ' AND '
# => " AND a AND b AND c AND "
Removing whitespace
s.split(' ').join('')
# or
s.delete(' ') # only deletes space chars
use gsub method for replacing whitespace
s = "ajeet soni"
=> "ajeet soni"
s.gsub(" "," AND ")
=> "ajeet AND soni"

Resources