タグ

stringとsubstitutionに関するnabinnoのブックマーク (2)

  • String#gsub (Ruby 3.4 リファレンスマニュアル)

    gsub(pattern, replace) -> String[permalink][rdoc][edit] 文字列中で pattern にマッチする部分全てを文字列 replace で置き換えた文字列を生成して返します。 置換文字列 replace 中の \& と \0 はマッチした部分文字列に、 \1 ... \9 は n 番目の括弧の内容に置き換えられます。置換文字列内では \`、\'、\+ も使えます。これらは $`、$'、$+ に対応します。 [PARAM] pattern: 置き換える文字列のパターンを表す文字列か正規表現。文字列を指定した場合は全く同じ文字列にだけマッチする [PARAM] replace: pattern で指定した文字列と置き換える文字列 例 p 'abcdefg'.gsub(/def/, '!!') # => "abc!!g" p 'abcabc'.g

    nabinno
    nabinno 2014/09/10
    puts s.gsub(/[<>&"]/, "<" => "&lt;", ">" => "&gt;", "&" => "&amp;", '"' => "&quot;")
  • Class: String (Ruby 2.1.2)

    A String object holds and manipulates an arbitrary sequence of bytes, typically representing characters. String objects may be created using String::new or as literals. Because of aliasing issues, users of strings should be aware of the methods that modify the contents of a String object. Typically, methods with names ending in “!'' modify their receiver, while those without a “!'' return a new St

  • 1