class String
Public Instance Methods
Searches for the first occurrence of delimiter
, and returns the portion of the String
after that. If delimiter
is not found, returns nil. Equivalent to split(delimiter, 2).drop(1)[-1]
for non-empty delimiters.
@example
"http://www.example.com".after("://") # == "www.example.com" "http://www.example.com".after("?") # == nil "http://www.example.com".after("") # == "http://www.example.com"
@param delimiter [String] @return [String, nil]
# File lib/casual_support/string/after.rb, line 15 def after(delimiter) i = self.index(delimiter) i && self[i + delimiter.length, self.length] end
Searches for the last occurrence of delimiter
, and returns the portion of the String
after that. If delimiter
is not found, returns nil. Equivalent to split(delimiter, -1).drop(1)[-1]
for non-empty delimiters.
@example
"/path/to/file".after_last("/") # == "file" "/path/to/file".after_last(".") # == nil "/path/to/file".after_last("") # == ""
@param delimiter [String] @return [String, nil]
# File lib/casual_support/string/after_last.rb, line 15 def after_last(delimiter) i = self.rindex(delimiter) i && self[i + delimiter.length, self.length] end
Searches for the first occurrence of delimiter
, and returns the portion of the String
before that. If delimiter
is not found, returns a copy of the original String
. Equivalent to split(delimiter, 2)[0]
for non-empty delimiters.
@example
"http://www.example.com".before("://") # == "http" "http://www.example.com".before("?") # == "http://www.example.com" "http://www.example.com".before("") # == ""
@param delimiter [String] @return [String]
# File lib/casual_support/string/before.rb, line 15 def before(delimiter) self[0, self.index(delimiter) || self.length] end
Searches for the last occurrence of delimiter
, and returns the portion of the String
before that. If delimiter
is not found, returns a copy of the original String
. Equivalent to split(delimiter, -1)[0...-1].join(delimiter)
for existent delimiters.
@example
"/path/to/file".before_last("/") # == "/path/to" "/path/to/file".before_last(".") # == "/path/to/file" "/path/to/file".before_last("") # == "/path/to/file"
@param delimiter [String] @return [String]
# File lib/casual_support/string/before_last.rb, line 16 def before_last(delimiter) self[0, self.rindex(delimiter) || self.length] end
Returns the portion of the String
between the first occurrences of an open
delimiter and a close
delimiter. If either delimiter is not found, returns nil.
@example
"i <b><3</b> ruby".between("<b>", "</b>") # == "<3" "i <b><3<b> ruby".between("<b>", "</b>") # == nil
@param open [String] @param close [String] @return [String, nil]
# File lib/casual_support/string/between.rb, line 14 def between(open, close) i = self.index(open) if i i += open.length j = self.index(close, i) self[i, j - i] if j end end
Prepends affix
to the String
only if the String
does not already start with affix
. Otherwise returns a duplicate of the String
. Equivalent to +gsub(/^(?!affix)/, “affix”)+.
@example
"example.com".prefix("www.") # == "www.example.com" "www.example.com".prefix("www.") # == "www.example.com"
@param affix [String] @return [String]
# File lib/casual_support/string/prefix.rb, line 13 def prefix(affix) self.start_with?(affix) ? self.dup : "#{affix}#{self}" end
Appends affix
to the String
only if the String
does not already end with affix
. Otherwise returns a duplicate of the String
. Equivalent to +gsub(/(?<!affix)$/, “affix”)+.
@example
"example".suffix(".com") # == "example.com" "example.com".suffix(".com") # == "example.com"
@param affix [String] @return [String]
# File lib/casual_support/string/suffix.rb, line 13 def suffix(affix) self.end_with?(affix) ? self.dup : "#{self}#{affix}" end