Ruby Class : String
ÃÑ ÆäÀÌÁö ¼ö : 3224

Àüü ÇÔ¼ö/¿ë¾î»çÀü
Facebook Joinc ±×·ì   Joinc QA »çÀÌÆ®
ÇöÀçÀ§Ä¡ : ¸Þ´º¾ó>30>String



joinc´Â Firefox¿Í chrome¿¡¼­ Å×½ºÆ® Çß½À´Ï´Ù. IE¿¡¼­´Â Å×À̺íÀÌ ±úÁö°Å³ª À̹ÌÁö°¡ º¸ÀÌÁö ¾ÊÀ» ¼ö ÀÖ½À´Ï´Ù. ƯÈ÷ ±¸±Û DocsÀ̹ÌÁöÀÇ °æ¿ì ¿¢¹Úó¸®µÉ ¼ö ÀÖ½À´Ï´Ù.

String

index (substring [,offset]) -> fixnum or nil

index (regexp [,offset]) -> fixnum or nil

¹®ÀÚ¿­¿¡¼­ substring¸¦ ã´Â´Ù. Á¤±ÔÇ¥Çö½ÄÀ» »ç¿ëÇÒ ¼öµµ ÀÖ´Ù. ¹ß°ßÇÏ¸é ¹ß°ßÇÑ À§Ä¡ÀÇ index °ªÀ» ¹ÝȯÇÑ´Ù. ¹ß°ßÇÏÁö ¸øÇß´Ù¸é nilÀ» ¹ÝȯÇÑ´Ù.
"hello".index('e')             #=> 1 
"hello".index('lo')            #=> 3 
"hello".index('a')             #=> nil 
"hello".index(?e)              #=> 1 
"hello".index(/[aeiou]/, -3)   #=> 4 
 

split (pattern=$;, limit) -> anArray

StringÀ» ±¸ºÐÀÚ¸¦ ±âÁØÀ¸·Î ³ª´«´Ù. °á°ú°ªÀº ±¸ºÐÀÚ·Î ºÐ¸®µÈ ¹®ÀÚ¿­ÀÇ ¹è¿­ÀÌ´Ù.

¸¸¾à pattern¾øÀÌ split¸¦ È£ÃâÇϸé, °ø¹é¹®ÀÚ¸¦ ±¸ºÐÀÚ·Î ¼³Á¤ÇÑ´Ù. patternÀº (Regexp)Á¤±ÔÇ¥Çö½ÄÀ» »ç¿ëÇÒ ¼ö ÀÖ´Ù.

¸¸¾à limit¸¦ »ç¿ëÇϸé, limit °³¼ö¸¸Å­ ¸¸ split¸¦ ÁøÇàÇÑ´Ù.

" now's  the time".split        #=> ["now's", "the", "time"] 
" now's  the time".split(' ')   #=> ["now's", "the", "time"] 
" now's  the time".split(/ /)   #=> ["", "now's", "", "the", "time"] 
"1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"] 
"hello".split(//)               #=> ["h", "e", "l", "l", "o"] 
"hello".split(//, 3)            #=> ["h", "e", "llo"] 
"hi mom".split(%r{\s*})         #=> ["h", "i", "m", "o", "m"] 
 
"mellow yellow".split("ello")   #=> ["m", "w y", "w"] 
"1,2,,3,4,,".split(',')         #=> ["1", "2", "", "3", "4"] 
"1,2,,3,4,,".split(',', 4)      #=> ["1", "2", "", "3,4,,"] 
"1,2,,3,4,,".split(',', -4)     #=> ["1", "2", "", "3", "4", "", ""] 
 

sub (pattern, replacement) -> new_str

sub (pattern, hash) -> new_str

sub (pattern){|match| block} -> new_str

str¿¡¼­ ù¹øÂ° ¹ß»ýÇÑ patternÀ» µÎ¹øÂ° ¸Å°³º¯¼ö·Î ġȯÇÑ´Ù. patternÀº Á¤±ÔÇ¥Çö½ÄÀ¸·Î Ç¥ÇöÇÒ ¼ö ÀÖ´Ù.
puts "hello".sub(/[aeiou]/, '*') 
puts "hello".sub(/([aeiou])/, '<\1>') 
puts "service.a.com".sub("com", "co.kr") 
 
EmailÀ» ±âÀÔÇϸé, ´ñ±ÛÀÌ ¸ÞÀÏ·Î Àü´ÞµË´Ï´Ù.