How to strip leading and trailing quote from string, in Ruby
You could also use the chomp
function, but it unfortunately only works in the end of the string, assuming there was a reverse chomp, you could:
'"foo,bar"'.rchomp('"').chomp('"')
Implementing rchomp
is straightforward:
class String
def rchomp(sep = $/)
self.start_with?(sep) ? self[sep.size..-1] : self
end
end
Note that you could also do it inline, with the slightly less efficient version:
'"foo,bar"'.chomp('"').reverse.chomp('"').reverse
EDIT: Since Ruby 2.5, rchomp(x)
is available under the name delete_prefix
, and chomp(x)
is available as delete_suffix
, meaning that you can use
'"foo,bar"'.delete_prefix('"').delete_suffix('"')
Removing leading and trailing double quotes in string in rails
You can also use .tr
method.
str = "\"hai"
str = str.tr('\"', '')
##OR
str.tr!('\"', '')
## OUTPUT
"hai"
Remove leading and trailing quotes from string if both are present
You can use
str.gsub(/\A"+(.*?)"+\Z/m, '\1')
The pattern will match a string starting with one or more "
, then it can have any characters, any amount of them and then one or more double quotes at the end of the string. The whole string without the leading and trailing quotes will be inserted into the replacement result with the \1
backreference.
See IDEONE demo
To only trim out the first and last double qoute, you may use
str.gsub(/\A"(.*)"\Z/m, '\1')
how Remove double quotes from a string ruby on rails?
str = 'hello " 5," world'
str.gsub!('"', '')
puts str #hello 5, world
How to remove leading and trailing spaces at comma and single quotes from string
# read as string
csv = File.read file.strip, "r:bom|utf-8"
# parse and separate header and the rest
head, *rest = csv.split($/).map { |row| row.split(/["']*\s*,\s*['"]*/) }
# produce the hash requested
rest.map { |row| head.zip(row).to_h }
#⇒ [
# [0] {
# "Date" => "Mar 22",
# "Line" => "heartbeat",
# "MatchedLine" => "kernel: heartbeat event \\n\"'",
# "SN" => "C0001",
# "TIME" => "02:50:25"
# },
# [1] {
# "Date" => "Mar 22",
# "Line" => "Wait max",
# "MatchedLine" => "kernel: Wait max 12 seconds and start polling eCM \\n\"'",
# "SN" => "C0002",
# "TIME" => "02:50:25"
# }
# ]
Remove trailing spaces from each line in a string in ruby
str.split("\n").map(&:rstrip).join("\n")
Its probably doable with regex but I prefer this way.
how to remove the quote from string?
This is assuming your strings will always be of the "msgid..." format shown above and your inteded output was 'msgid "text here"':
>> str.gsub(/(msgid )"{1,}(.*) "{1,}(.*)"/, '\1"\2 \3"')
=> "msgid "We couldn't set up that account, sorry. Please try again or contact an admin (link is above).""
>> puts str.gsub(/(msgid )"{1,}(.*) "{1,}(.*)"/, '\1"\2 \3"')
msgid "We couldn't set up that account, sorry. Please try again or contact an admin (link is above)."
Related Topics
Error Install Rubyracer with Error "Invalid Gem: Package Is Corrupt"
Differencebetween Ruby's 'Open-Uri' and 'Net:Http' Gems
Ruby on Rails Multiple Http Request at the Same Time
Ruby.Metaprogramming. Class_Eval
How to Override [] Brackets in Ruby
Why Don't Numbers Support .Dup
Removing or Overriding an Activerecord Validation Added by a Superclass or Mixin
Why Does Rails 4.2 + Responders Keeps Telling Me to Add Responders to the Gemfile
How to Solve the 'Object Doesn't Support #Inspect' Error
Rails 3. Simple_Format Do Not Wrap Result in Paragraph Tags
Splitting String into Pair of Characters in Ruby
Testing Routes with Subdomain Constraints Using Rspec
Does Ruby Have Any Number Formatting Classes