2013年3月24日日曜日

文字列操作の比較表 - Ruby, PHP, Objective-C

Ruby (String) PHP (String 非オブジェクト) Objective-C (NSString)
s = "abc" $s = "abc" s = @"abc"
s = x + y $s = $x . $y s = [x stringByAppendingString:y]
s == x $s == $x [s isEqualToString:x]
s % [x, y]
sprintf(s, x, y)
sprintf($s, $x, $y) [NSString stringWithFormat:s, x, y]
[x, y, z].join(d) implode($d, $a)
join($d, $a)
[a componentsJoinedByString:d] //NSArray
s.capitalize ucfirst($s)
ucwords($s)
[s capitalizedString]
s.capitalize!
s.center(w) str_pad($s, $w, STR_PAD_BOTH)
s.chomp rtrim(s, "\n")
s.chomp!
s.chop

s.chop!

s.clear #1.9
[ms setString:@""]
s.concat(x) $s .= $x [ms appendString:x]
s = [s stringByAppendingString:x]
s.count(x) preg_matchpreg_match_all
s.crypt(x) crypt($s, $x) C で
s.delete(x) str_replace($x, '', $s)
preg_replace($x, '', $s)
OgreKitで?
s.downcase strtolower($s) [s lowercaseString]
s.downcase!
s.each_byte {|x| ... } for($i=0; $x=$s[$i]; $i++){ ... } [s cString] →後はお好きに
s.each_line {|x| ... } implode して foreach? [s componentsSeparatedByString:@"\n"]→for{}
s.empty? strlen($s)==0 [s length]==0
s.end_with(x) #1.9

s.gsub(f, t) str_replace($f, $t, $s)
preg_replace($f, $t, $s)
OgreKitで?
s.gsub!(x, y)
s.hex
s.to_i(16)


s.include?(x) strpos($s, $x)!==FALSE
s.index(x) strpos($s, $x) //単純な文字列のみ
s.insert(i, x)

s.length
s.size
strlen($s) [s length]
s.ljust(w) str_pad($s, $w, STR_PAD_RIGHT)
s.lstrip ltrim(s)
s.lstrip!
s.match(x)
x.match(s)
preg_match($x, $s) OgreKitで?
s.next!
s.succ!
$s++ //けっこう賢い
s.oct
s.to_i(8)


s.partition(x) #1.9

s.replace(x) $s=$x [ms setString:x]
s.reverse

s.reverse!

s.rindex(x) strrpos($s, $x) //単純な文字列のみ
s.rjust(w) str_pad($s, $w, STR_PAD_LEFT)
s.rpartition(x) #1.9

s.rstrip rtrim(s)
s.rstrip!
s.scan(x) { ... }

s[i]
s.slice(i)
$s[i]
substr($s, $i, 1)
[s substringWithRange:NSMakeRange(i, 1)]
s[i..-1]
s.slice(i..-1)
substr($s, $i) [s substringFromIndex:i]
s[i, l]
s.slice(i, l)
substr($s, $i, $l) [s substringWithRange:NSMakeRange(i, l)]
s[i..j]
s.slice(i..j)
substr($s, $i, $j-$i+1) [s substringWithRange:NSMakeRange(i, j-i+1)]
s[i...j]
s.slice(i...j)
substr($s, $i, $j-$i) [s substringWithRange:NSMakeRange(i, j-i)]
s.split(d) explode($d, $s) //単純分割
preg_split($d, $s)
[s componentsSeparatedByString:d] //単純分割
s.start_with(x) #1.9

s.strip trim(s) [s stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet] ]
s.strip!
s.sub(x, y)

s.sub!(x, y)

s.swapcase

s.to_f floatval($s) [s floatValue]
s.to_i intval($s) [s intValue]
s.tr!(x, y) str_replace($x, $y, $s)
preg_replace($x, $y, $s)

s.tr_s!(x, y)

s.unpack(x) unserialize(s) [s propertyList]
[s propertyListFromStringsFileFormat]
s.upcase strtoupper(s) [s uppercaseString]
s.upcase!
Regexp.escape(s)
Regexp.quote(s)
preg_quote($s)
quotemeta($s)

0 件のコメント: