Ruby String Functions

EditRocket provides the following information on String functions in the Ruby source code builder.

block_scanf(fstr,&b) -

capitalize! - Modifies str by converting the first character to uppercase and the remainder to lowercase. Returns nil if no changes are made.

capitalize - Returns a copy of str with the first character converted to uppercase and the remainder to lowercase.

casecmp(other_str) - Case-insensitive version of String#<=>.

center(integer, padstr) - If integer is greater than the length of str, returns a new String of length integer with str centered and padded with padstr; otherwise, returns str.

chomp!(separator=$/) - Modifies str in place as described for String#chomp, returning str, or nil if no modifications were made.

chomp(separator=$/) - Returns a new String with the given record separator removed from the end of str (if present). If $/ has not been changed from the default Ruby record separator, then chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n).

chop!() -

chop - Returns a new String with the last character removed. If the string ends with \r\n, both characters are removed. Applying chop to an empty string returns an empty string. String#chomp is often a safer alternative, as it leaves the string unchanged if it doesn't end in a record separator.

concat(fixnum) - Append---Concatenates the given object to str. If the object is a Fixnum between 0 and 255, it is converted to a character before concatenation.

count([other_str]+) - Each other_str parameter defines a set of characters to count. The intersection of these sets defines the characters to count in str. Any other_str that starts with a caret (^) is negated. The sequence c1--c2 means all characters between c1 and c2.

crypt(other_str) - Applies a one-way cryptographic hash to str by invoking the standard library function crypt. The argument is the salt string, which should be two characters long, each character drawn from [a-zA-Z0-9./].

delete!([other_str]+>) - Performs a delete operation in place, returning str, or nil if str was not modified.

delete([other_str]+) - Returns a copy of str with all characters in the intersection of its arguments deleted. Uses the same rules for building the set of characters as String#count.

downcase! - Downcases the contents of str, returning nil if no changes were made.

downcase - Returns a copy of str with all uppercase letters replaced with their lowercase counterparts. The operation is locale insensitive---only characters ``A'' to ``Z'' are affected.

dump - Produces a version of str with all nonprinting characters replaced by \nnn notation and all special characters escaped.

each(separator=$/) - Splits str using the supplied parameter as the record separator ($/ by default), passing each substring in turn to the supplied block. If a zero-length record separator is supplied, the string is split on \n characters, except that multiple successive newlines are appended together.

each_byte - Passes each byte in str to the given block.

each_char() -

each_line(separator=$/) - Splits str using the supplied parameter as the record separator ($/ by default), passing each substring in turn to the supplied block. If a zero-length record separator is supplied, the string is split on \n characters, except that multiple successive newlines are appended together.

empty? - Returns true if str has a length of zero.

end_regexp() -

eql?(other) - Two strings are equal if the have the same length and content.

gsub!(pattern, replacement) - Performs the substitutions of String#gsub in place, returning str, or nil if no substitutions were performed.

gsub(pattern, replacement) - Returns a copy of str with all occurrences of pattern replaced with either replacement or the value of the block. The pattern will typically be a Regexp; if it is a String then no regular expression metacharacters will be interpreted (that is /\d/ will match a digit, but '\d' will match a backslash followed by a 'd').

hash - Return a hash based on the string's length and content.

hex - Treats leading characters from str as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number. Zero is returned on error.

include? - Returns true if str contains the given string or character.

index(substring [, offset]) - Returns the index of the first occurrence of the given substring, character (fixnum), or pattern (regexp) in str. Returns nil if not found. If the second parameter is present, it specifies the position in the string to begin the search.

initialize_copy str.replace(other_str) - Replaces the contents and taintedness of str with the corresponding values in other_str.

insert(index, other_str) - Inserts other_str before the character at the given index, modifying str. Negative indices count from the end of the string, and insert after the given character. The intent is insert aString so that it starts at the given index.

inspect - Returns a printable version of str, with special characters escaped.

intern - Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.

is_binary_data?() -

is_complex_yaml?() -

iseuc - Returns whether self's encoding is EUC-JP or not.

issjis - Returns whether self's encoding is Shift_JIS or not.

isutf8 - Returns whether self's encoding is UTF-8 or not.

jcount(str) -

jlength() -

jsize() - "Alias for #jlength"

kconv String#kconv(out_code, in_code = Kconv::AUTO) - Convert self to out_code. out_code and in_code are given as constants of Kconv.

length - Returns the length of str.

ljust(integer, padstr=' ') - If integer is greater than the length of str, returns a new String of length integer with str left justified and padded with padstr; otherwise, returns str.

lstrip! - Removes leading whitespace from str, returning nil if no change was made. See also String#rstrip! and String#strip!.

lstrip - Returns a copy of str with leading whitespace removed. See also String#rstrip and String#strip.

match(pattern) - Converts pattern to a Regexp (if it isn't already one), then invokes its match method on str.

mbchar?() -

new(str="") - Returns a new string object containing a copy of str.

next! - Equivalent to String#succ, but modifies the receiver in place.

next - Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case. Incrementing nonalphanumerics uses the underlying character set's collating sequence.

oct - Treats leading characters of str as a string of octal digits (with an optional sign) and returns the corresponding number. Returns 0 if the conversion fails.

quote() -

replace(other_str) - Replaces the contents and taintedness of str with the corresponding values in other_str.

reverse! - Reverses str in place.

reverse - Returns a new string with the characters from str in reverse order.

rindex(substring [, fixnum]) - Returns the index of the last occurrence of the given substring, character (fixnum), or pattern (regexp) in str. Returns nil if not found. If the second parameter is present, it specifies the position in the string to end the search---characters beyond this point will not be considered.

rjust(integer, padstr=' ') - If integer is greater than the length of str, returns a new String of length integer with str right justified and padded with padstr; otherwise, returns str.

rstrip! - Removes trailing whitespace from str, returning nil if no change was made. See also String#lstrip! and String#strip!.

rstrip - Returns a copy of str with trailing whitespace removed. See also String#lstrip and String#strip.

scan(pattern) - Both forms iterate through str, matching the pattern (which may be a Regexp or a String). For each match, a result is generated and either added to the result array or passed to the block. If the pattern contains no groups, each individual result consists of the matched string, $&. If the pattern contains groups, each individual result is itself an array containing one entry per group.

scanf(fstr,&b) -

size - Returns the length of str.

slice!(fixnum) - Deletes the specified portion from str, and returns the portion deleted. The forms that take a Fixnum will raise an IndexError if the value is out of range; the Range form will raise a RangeError, and the Regexp and String forms will silently ignore the assignment.

slice(fixnum) - Element Reference---If passed a single Fixnum, returns the code of the character at that position. If passed two Fixnum objects, returns a substring starting at the offset given by the first, and a length given by the second. If given a range, a substring containing characters at offsets given by the range is returned. In all three cases, if an offset is negative, it is counted from the end of str. Returns nil if the initial offset falls outside the string, the length is negative, or the beginning of the range is greater than the end.

split(pattern=$;, [limit]) - Divides str into substrings based on a delimiter, returning an array of these substrings.

squeeze!(del=nil) -

squeeze([other_str]*) - Builds a set of characters from the other_str parameter(s) using the procedure described for String#count. Returns a new string where runs of the same character that occur in this set are replaced by a single character. If no arguments are given, all runs of identical characters are replaced by a single character.

strip! - Removes leading and trailing whitespace from str. Returns nil if str was not altered.

strip - Returns a copy of str with leading and trailing whitespace removed.

sub!(pattern, replacement) - Performs the substitutions of String#sub in place, returning str, or nil if no substitutions were performed.

sub(pattern, replacement) - Returns a copy of str with the first occurrence of pattern replaced with either replacement or the value of the block. The pattern will typically be a Regexp; if it is a String then no regular expression metacharacters will be interpreted (that is /\d/ will match a digit, but '\d' will match a backslash followed by a 'd').

succ! - Equivalent to String#succ, but modifies the receiver in place.

succ - Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case. Incrementing nonalphanumerics uses the underlying character set's collating sequence.

sum(n=16) - Returns a basic n-bit checksum of the characters in str, where n is the optional Fixnum parameter, defaulting to 16. The result is simply the sum of the binary value of each character in str modulo 2n - 1. This is not a particularly good checksum.

swapcase! - Equivalent to String#swapcase, but modifies the receiver in place, returning str, or nil if no changes were made.

swapcase - Returns a copy of str with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase.

to_f - Returns the result of interpreting leading characters in str as a floating point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0.0 is returned. This method never raises an exception.

to_i(base=10) - Returns the result of interpreting leading characters in str as an integer base base (2, 8, 10, or 16). Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0 is returned. This method never raises an exception.

to_s - Returns the receiver.

to_str - Returns the receiver.

to_sym - Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.

to_yaml( opts = {} ) -

toeuc - Convert self to EUC-JP

tojis - Convert self to ISO-2022-JP

tosjis - Convert self to Shift_JIS

toutf16 - Convert self to UTF-16

toutf8 - Convert self to UTF-8

tr!(from, to) -

tr(from, to) -

tr_s!(from, to) -

tr_s(from_str, to_str) - Processes a copy of str as described under String#tr, then removes duplicate characters in regions that were affected by the translation.

unpack(format) - Decodes str (which may contain binary data) according to the format string, returning an array of each value extracted. The format string consists of a sequence of single-character directives, summarized in the table at the end of this entry. Each directive may be followed by a number, indicating the number of times to repeat with this directive. An asterisk (``*'') will use up all remaining elements. The directives sSiIlL may each be followed by an underscore (``_'') to use the underlying platform's native size for the specified type; otherwise, it uses a platform-independent consistent size. Spaces are ignored in the format string. See also Array#pack.

upcase! - Upcases the contents of str, returning nil if no changes were made.

upcase - Returns a copy of str with all lowercase letters replaced with their uppercase counterparts. The operation is locale insensitive---only characters ``a'' to ``z'' are affected.

upto(other_str) - Iterates through successive values, starting at str and ending at other_str inclusive, passing each value in turn to the block. The String#succ method is used to generate each value.

yaml_new( klass, tag, val ) -