How to delete specific characters from a string in Ruby?
Do as below using String#tr
:
"((String1))".tr('()', '')
# => "String1"
How to remove characters from String at specific index in Ruby
I think you want this:
>> string = 'Set{[8, 4, "a", 6, 1]}'
=> "Set{[8, 4, \"a\", 6, 1]}"
>> string.gsub('{[', '{').gsub(']}', '}')
=> "Set{8, 4, \"a\", 6, 1}"
If there is any danger that you might see the '{[' or ']}' pattern in the middle of the string and want to preserve it there, and if you are sure of the position relative to the start and end of the string each time, you could do this:
>> string = 'Set{[8, 4, "a", 6, 1]}'
>> chars = string.chars
>> chars.delete_at(4)
>> chars.delete_at(-2)
>> chars.join
=> "Set{8, 4, \"a\", 6, 1}"
Deleting all special characters from a string - ruby
You can do this
a.gsub!(/[^0-9A-Za-z]/, '')
How to Delete Strings that Start with Certain Characters in Ruby
Check if there's a whitespace (\s
) before or if it's the start of the string (^
).
(?:\s|^)@.*
Live Demo
If you only want to match word characters, then instead of using .
you could use \w
. Also note that *
would still match just "@"
, so you might want to use +
instead to match at least one character.
(?:\s|^)@\w+
Live Demo
Remove substring from the string
You can use the slice method:
a = "foobar"
a.slice! "foo"
=> "foo"
a
=> "bar"
there is a non '!' version as well. More info can be seen in the documentation about other versions as well:
http://www.ruby-doc.org/core/classes/String.html#method-i-slice-21
How to remove strings that end with a particular character in Ruby
You were so close.
email = email.gsub( /.*\.\s*$/ , "")
The difference lies in the fact that you didn't consider the relationship between string of reference and the regex tokens that describe the condition you wish to trigger. Here, you are trying to find a period (\.
) which is followed only by whitespace (\s
) or the end of the line ($
). I would read the regex above as "Any characters of any length followed by a period, followed by any amount of whitespace, followed by the end of the line."
As commenters pointed out, though, there's a simpler way: String#end_with?
.
Remove character from string if it starts with that character?
Why not just include the regex in the sub!
method?
string.sub!(/^1/, '')
how to delete specific characters in ruby?
Here is a non-Rails approach:
require 'cgi'
str = 'Company "Life"'
puts CGI.unescape_html(str).gsub(/"/, '').gsub(/\s+/, '-').downcase
# => company-life
And a pure regex solution:
puts str.gsub(/&\w+;/, '').gsub(/\s+/, '-').downcase
# => company-life
And if you are inside Rails(thanks to @nzifnab):
str.gsub(/&\w+;/, '').parameterize
How can remove some special character from string of array
The same way you map
over the collection, you can iterate it using a block:
["hardware", "computer", "network", "mobile devices"].map do |asset_type|
asset_type.delete(' ')
.delete('_')
.delete('.')
end
Your function could look something like:
def valid_sheet_names
asset_types = Company.find(@company_id).asset_types.pluck(:name).reject(&:nil?)
# I'd personally extract normalization to a specific function or module
asset_types.map do |asset_type|
asset_type.downcase
.delete(' ')
.delete('.')
.delete('_')
end
end
You can also use gsub
(which supports regular expressions). Though I found delete
more explicit when we want to... delete
Related Topics
Rails Generate Has_Many Association
Instance and Class Variables in Rails Controller
Rails Routing (Root :To => ...)
How to Log the Entire Trace Back of a Ruby Exception Using the Default Rails Logger
Ruby: What Does the Asterisk in "P *1..10" Mean
How to Add Two Weeks to Time.Now
Ruby Classes: Initialize Self VS. @Variable
How to Read Lines from File into Array
Rails: Creating a Custom Data Type/Creating a Shorthand
How to Debug/Display Request Sent Using Restclient
Rails: Serializing Objects in a Database
An Error Occurred While Installing Debugger-Linecache (1.1.1), and Bundler Cannot Continue
Rails: How to Write Tests for a Ruby Module
Comparing Times Only, Without Dates
Rails Activeadmin - Custom Association Select Box
MAC Os X Mountain Lion "Rails Is Not Currently Installed on This System."