Top > Ruby [2.2.2] > split


String# split

p '1+1=1'.split("=")      # ["1+1", "2"]

p '<head><title>split</title></head>'.split(//)
                   # ["<", "h", "e", "a", "d", ">", 
                   #  "<", "t", "i", "t", "l", "e", ">", 
                   #  "s", "p", "l", "i", "t", 
                   #  "<", "/", "t", "i", "t", "l", "e", ">",
                   #  "<", "/", "h", "e", "a", "d", ">"]

p '<head><title>split</title></head>'.split(/<(\S+?)>/)
                   # ["", "head", "", "title", "split",
                   #  "/title", "", "/head"]

p '<head><title>split</title></head>'.split(/<(\S+?)>/,2)
                   # ["", "head", "<title>split</title></head>"]

正規表現のパワーは凄いですね。
splitも正規表現を使えば、使い道が倍増します。



Copyright © 2015 AchaPorutePiipo All Rights Reserved.