はじめに
PHPの標準関数をRubyで書いてみました。
簡単に書けそうなものだけパズル感覚で書いてます。
文字列操作編です。
引数と返り値の型はテーブルで記載しました。
str_contains($haystack, $needle)
haystack | String |
needle | String |
return | TrueClass | FalseClass |
haystack.include?(needle)
str_ends_with($haystack, $needle)
haystack | String |
needle | String |
return | TrueClass | FalseClass |
haystack.end_with?(needle)
str_getcsv ($string, $separator, $enclosure, $escape)
string | String |
separator | String |
enclosure | String |
escape | 標準CSVライブラリでは指定不可 (2回続けて入力してエスケープ) |
return | Array<String> |
require "csv"
CSV.parse_line(string, col_sep: separator, quote_char: enclosure)
str_ireplace($search, $replace, $subject, &$count)
search | String |
replace | String |
subject | String |
count | Integer |
return | String |
count = 0
subject.gsub(Regexp.new(Regexp.escape(search), Regexp::IGNORECASE)) {count += 1; replace}
str_pad($string, $length, $pad_string, $pad_type)
string | String |
length | Integer |
pad_string | String |
pad_type | STR_PAD_RIGHT |
return | String |
string.ljust(length, pad_string)
string | String |
length | Integer |
pad_string | String |
pad_type | STR_PAD_LEFT |
return | String |
string.rjust(length, pad_string)
string | String |
length | Integer |
pad_string | String |
pad_type | STR_PAD_BOTH |
return | String |
string.center(length, pad_string)
str_repeat($string, $times)
string | String |
times | Integer |
return | String |
string * times
str_replace($search, $replace, $subject, &$count)
search | String |
replace | String |
subject | String |
count | Integer |
return | String |
count = 0
subject.gsub(Regexp.new(Regexp.escape(search))) {count += 1; replace}
str_rot13($string)
string | String |
return | String |
string.tr("a-zA-Z", "n-za-mN-ZA-M")
str_shuffle($string)
string | String |
return | String |
string.chars.shuffle.join
str_split($string, $length)
string | String |
length | Integer ※1以上 |
return | String |
string.each_char.each_slice(length).map(&:join)
str_starts_with($haystack, $needle)
haystack | String |
needle | String |
return | TrueClass | FalseClass |
haystack.start_with?(needle)
str_word_count($string, $format, $characters)
string | String |
format | 0 |
characters | String |
return | Integer |
string.match(/^[-']*(.*?)-*$/)[1].scan(/[-'a-zA-Z]+/).count
string | String |
format | 1 |
characters | String |
return | Array<String> |
string.match(/^[-']*(.*?)-*$/)[1].scan(/[-'a-zA-Z]+/)
strcasecmp($str1, $str2)
str1 | String |
str2 | String |
return | Integer |
str1.casecmp(str2)
※返却値は-1, 0, 1のいずれかなので注意
strcmp($str1, $str2)
str1 | String |
str2 | String |
return | Integer |
str1 <=> str2
※返却値は-1, 0, 1のいずれかなので注意
strcspn($string, $characters, $offset, $length)
string | String |
characters | String |
offset | Integer |
length | Integer ※0以上 |
return | Integer |
string = string[offset, length]
string.chars.each_with_index.find { |c, _| characters.include?(c) }&.at(1) || string.length
stripos($haystack, $needle, $offset)
haystack | String |
needle | String |
offset | Integer |
return | String | FalseClass |
haystack.downcase.index(needle.downcase, offset)
stristr($haystack, $needle, $before_needle)
haystack | String |
needle | String |
before_needle | FalseClass |
return | String | FalseClass |
haystack.chars.each_with_index.find { |c, _| c.casecmp?(needle) }.then { |_, i| i ? haystack[i..] : false }
haystack | String |
needle | String |
before_needle | TrueClass |
return | String | FalseClass |
haystack.chars.each_with_index.find { |c, _| c.casecmp?(needle) }.then { |_, i| i ? haystack[0, i] : false }
strlen($string)
string | String |
return | Integer |
string.length
strncasecmp($str1, $str2, $length)
str1 | String |
str2 | String |
length | Integer |
return | Integer |
str1[0, length].casecmp(str2[0, length])
※返却値は-1, 0, 1のいずれかなので注意
strncmp($str1, $str2, $length)
str1 | String |
str2 | String |
length | Integer |
return | Integer |
str1[0, length] <=> str2[0, length]
※返却値は-1, 0, 1のいずれかなので注意
strpbrk($string, $characters)
string | String |
characters | String |
return | String | FalseClass |
string.chars.each_with_index.find { |c, _| characters.include?(c) }.then { |_, i| i ? string[i..] : false }
strpos($haystack, $needle, $offset)
haystack | String |
needle | String |
offset | Integer |
return | String | FalseClass |
haystack.index(needle, offset)
strrchr($haystack, $needle)
haystack | String |
needle | String |
return | String | FalseClass |
haystack.chars.reverse.each_with_index.find { |c, _| c == needle.chr }.then { |_, i| i ? haystack[-i-1..] : false }
strrev($string)
string | String |
return | String |
string.reverse
strripos($haystack, $needle, $offset)
haystack | String |
needle | String |
offset | Integer ※0以上 |
return | String | FalseClass |
haystack[offset..].downcase.rindex(needle.downcase)&.+(offset) || false
haystack | String |
needle | String |
offset | Integer ※0未満 |
return | String | FalseClass |
haystack.downcase.rindex(needle.downcase, offset) || false
strrpos($haystack, $needle, $offset)
haystack | String |
needle | String |
offset | Integer ※0以上 |
return | String | FalseClass |
haystack[offset..].rindex(needle)&.+(offset) || false
haystack | String |
needle | String |
offset | Integer ※0未満 |
return | String | FalseClass |
haystack.rindex(needle, offset) || false
strspn($string, $characters, $offset, $length)
string | String |
characters | String |
offset | Integer |
length | Integer ※0以上 |
return | Integer |
string = string[offset, length]
string.chars.each_with_index.find { |c, _| !characters.include?(c) }&.at(1) || string.length
strstr($haystack, $needle, $before_needle)
haystack | String |
needle | String |
before_needle | FalseClass |
return | String | FalseClass |
haystack.chars.each_with_index.find { |c, _| c == needle }.then { |_, i| i ? haystack[i..] : false }
haystack | String |
needle | String |
before_needle | TrueClass |
return | String | FalseClass |
haystack.chars.each_with_index.find { |c, _| c == needle }.then { |_, i| i ? haystack[0, i] : false }
strtolower($string)
string | String |
return | String |
string.downcase
strtoupper($string)
string | String |
return | String |
string.upcase
strtr($string, $from, $to)
string | String |
from | String |
to | String |
return | String |
limit = [from, to].map(&:length).min
string.tr(from[0, limit], to[0, limit])
strtr($string, $replace_pairs)
string | String |
replace_pairs | Hash<String, String> |
return | String |
string.gsub(Regexp.new(replace_pairs.keys.map { |s| Regexp.escape(s) }.join('|'))) { |m| replace_pairs[m] }
参考文献
Class array (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Array.html
Class CSV (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/CSV.html
Class Regexp (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Regexp.html
Class string (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/String.html
Module enumerable (Ruby 3.0.0 リファレンスマニュアル). (n.d.). docs.ruby-lang.org. Retrieved March 30, 2021, from https://docs.ruby-lang.org/ja/latest/class/Enumerable.html
Str_contains. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-contains.php
Str_ends_with. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-ends-with.php
Str_getcsv. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-getcsv.php
Str_ireplace. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-ireplace.php
Str_pad. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-pad.php
Str_repeat. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-repeat.php
Str_replace. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-replace.php
Str_rot13. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-rot13.php
Str_shuffle. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-shuffle.php
Str_split. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-split.php
Str_starts_with. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-starts-with.php
Str_word_count. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.str-word-count.php
Strcasecmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcasecmp.php
Strcmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcmp.php
Strcspn. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strcspn.php
Stripos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.stripos.php
Stristr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.stristr.php
Strlen. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strlen.php
Strncasecmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strncasecmp.php
Strncmp. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strncmp.php
Strpbrk. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strpbrk.php
Strpos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strpos.php
Strrchr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrchr.php
Strrev. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrev.php
Strripos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strripos.php
Strrpos. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strrpos.php
Strspn. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strspn.php
Strstr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strstr.php
Strtolower. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtolower.php
Strtoupper. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtoupper.php
Strtr. (n.d.). PHP: Hypertext Preprocessor. Retrieved March 30, 2021, from https://www.php.net/manual/ja/function.strtr.php