Get substring after the first = symbol in Ruby
Not exactly .after
, but quite close to:
string.partition('=').last
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-partition
Get substring after the first = symbol in Ruby
Not exactly .after
, but quite close to:
string.partition('=').last
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-partition
How to extract part of the string which comes after given substring?
Suppose your url is saved in a variable called url
.
Then the following should return 1234
url.match(/subfolder\/(\d*)/)[1]
Explanation:
url.match(/ # call the match function which takes a regex
subfolder\/ # search for the first appearance of the string 'subfolder/'
# note: we must escape the `/` so we don't end the regex early
(\d*) # match any number of digits in a capture group,
/)[1] # close the regex and return the first capture group
Return string until matched string in Ruby
String splitting is definitely easier and more readable than trying to extract the portion of the string using a regex. For the latter case, you would need a capture group to get the first match. It will be the same as string splitting
string.split(/#|Apt/, 2).first
How to get a substring from the beginning till the end of a word that starts with a with a given substring - Ruby
Use Regexp:
a = 'Metrics testSomeMetrics gets initial metrics data'
b = 'test'
a.match(/^.*#{b}\w*/).to_s
Where:
^
— start of the string..*
— zero or more of any single character.#{b}
— your string.\w*
— zero or more of any word character.
UPDATE
Add \b
to get /^.*\b#{b}\w*/
so that it b
will be exactly a start of a new string.
Select all characters in a string until a specific character Ruby
You can avoid creating an unnecessary Array (like Array#split
) or using a Regex (like Array#gsub
) by using.
a = "2.452811139617034,42.10874821716908|3.132087902867818,42.028314077306646|-0.07934861041448178,41.647538468746916|-0.07948265046522918,41.64754863599606"
a[0,a.index('|')]
#=>"2.452811139617034,42.1087482171"
This means select characters at positions 0 up to the index of the first pipe (|
). Technically speaking it is start at position 0 and select the length of n where n is the index of the pipe character which works in this case because ruby uses 0 based indexing.
As @CarySwoveland astutely pointed out the string may not contain a pipe in which case my solution would need to change to
#to return entire string
a[0,a.index('|') || a.size]
# or
b = a.index(?|) ? a[0,b] : a
# or to return empty string
a[0,a.index('|').to_i]
# or to return nil
a[0,a.index(?|) || -1]
Ruby: How to get the first character of a string
You can use Ruby's open classes to make your code much more readable. For instance, this:
class String
def initial
self[0,1]
end
end
will allow you to use the initial
method on any string. So if you have the following variables:
last_name = "Smith"
first_name = "John"
Then you can get the initials very cleanly and readably:
puts first_name.initial # prints J
puts last_name.initial # prints S
The other method mentioned here doesn't work on Ruby 1.8 (not that you should be using 1.8 anymore anyway!--but when this answer was posted it was still quite common):
puts 'Smith'[0] # prints 83
Of course, if you're not doing it on a regular basis, then defining the method might be overkill, and you could just do it directly:
puts last_name[0,1]
Ruby - slice all characters till underscore in a string
"solution_10"[/(?<=_).*/]
#⇒ "10"
or simply just get digits until the end of the line:
"solution_10"[/\d+\z/]
#⇒ "10"
Related Topics
How to Introspect Things in Ruby
Can't Convert Fixnum to String During Rake Db:Create
Ruby Methods That Either Yield or Return Enumerator
Rails: Ensure Only One Boolean Field Is Set to True at a Time
Best Way to Combine Fragment and Object Caching for Memcached and Rails
How to Write Specs for Code That Depends on Environment Variables
Reading the First Line of a File in Ruby
Vcrproxy: Record Phantomjs Ajax Calls with Vcr Inside Capybara
What Is the Easiest Way I Can Create a 'Beep' Sound from a Ruby Program
Ruby: What Does the Asterisk in "P *1..10" Mean
How to Log the Entire Trace Back of a Ruby Exception Using the Default Rails Logger
Is Ruby a Scripting Language or an Interpreted Language
How to Access a Ruby Module Method
Specifying a Layout and a Template in a Standalone (Not Rails) Ruby App, Using Slim or Haml