Ruby: Split string at character, counting from the right side
String#rpartition
does just that:
name, match, suffix = name.rpartition('.')
It was introduced in Ruby 1.8.7, so if running an earlier version you can use require 'backports/1.8.7/string/rpartition'
for that to work.
Rails split by last delimiter
try
'hello_to_you'.split /\_(?=[^_]*$)/
Split a string into a string and an integer
Use a positive lookbehind assertion based regex in string.split
.
> "10480ABCD".split(/(?<=\d)(?=[A-Za-z])/)
=> ["10480", "ABCD"]
(?<=\d)
Positive lookbehind which asserts that the match must be preceded by a digit character.(?=[A-Za-z])
which asserts that the match must be followed by an alphabet. So the above regex would match the boundary which exists between a digit and an alphabet. Splitting your input according to the matched boundary will give you the desired output.
OR
Use string.scan
> "10480ABCD".scan(/\d+|[A-Za-z]+/)
=> ["10480", "ABCD"]
Split method in a Do loop to separate string values in an array by whitespace
Anything like this?
array.each { |name| puts name.split.sample }
Split string at end of line (Ruby)
string_array = string.split(/\n/)
should do the job.
How to split a string in x equal pieces in ruby
How about this?
str.chars.each_slice(2).map(&:join)
Separate single characters from an string using .split
only_words_and_spaces = /[^a-zA-Z\s]/
"Hello friends+-!".gsub(only_words_and_spaces, '').split('')
=> ["H", "e", "l", "l", "o", " ", "f", "r", "i", "e", "n", "d", "s"]
Splitting a symbol, in the same manner as one would split a string
Since Ruby 1.9 some string's features are added to the Symbol
class but not this much.The best you can do, I think is:
:symbol_with_underscores.to_s.split('_').map(&:to_sym)
You could turn this into a Symbol
method:
class Symbol
def split(separator)
to_s.split(separator).map(&:to_sym)
end
end
:symbol_with_underscores.split('_')
# => [:symbol, :with, :underscores]
Creating a method that given a string (fname lname) returns the two
How about this:
def explode_name(str)
!str.nil? && str.respond_to?(:split) ? ((2 - (a = str.split(' ', 2)).length).times { a << nil };a) : [nil,nil]
end
explode_name "Mr James Bond" #=> ["Mr", "James Bond"]
explode_name "Mr Bond" #=> ["Mr", "Bond"]
explode_name "Mr" #=> ["Mr", nil]
explode_name "" #=> [nil, nil]
explode_name nil #=> [nil, nil]
explode_name 6 #=> [nil, nil]
A comparison for programming's sake
Related Topics
Rails Form Validation Conditional Bypass
Is There a Way in Ruby 1.9 to Remove Invalid Byte Sequences from Strings
Elasticsearch & Tire: Using Mapping and To_Indexed_JSON
Ruby Gets/Puts Only for Strings
Calling Sinatra from Within Sinatra
Invalid Gemspec -Illformed Requirement ["#<Yaml::Syck::Defaultkey:0Xb5F9C990> 3.2.0"]
Ruby Mocha: Is There an Equivalent to Rspec-Mocks' #And_Call_Original
How to Know If Today's Date Is in a Date Range
Ruby Metaprogramming: Dynamic Instance Variable Names
Ruby: What Is the Easiest Method to Update Hash Values
How to Ftp in Ruby Without First Saving the Text File
Rails - Pass Id Parameter on a Link_To
Detect Key Press (Non-Blocking) W/O Getc/Gets in Ruby
Ruby Block to String Instead of Executing
Disable Sprockets Asset Caching in Development
Problem with Quantifiers and Look-Behind