Ruby: string processing functions

Source: Internet
Author: User
Tags chop

String processing functions
1. Return the length of the string

str.length => integer

 

str.include? other_str => true .include?    .include?    .include? ?h     

 

3. String insertion:

str.insert(index, other_str) =>.insert(0, )    .insert(3, )    .insert(4, )    .insert(-3, -3, )   .insert(-1, )   

 

4. String separation. The default Delimiter is space.

str.split(pattern=$;, [limit]) =>.split        .split(%r{,\s*}) .split(//)               .split(//, 3)            .split(%r{\s*})         .split()   .split()         .split(, 4)      

5. String replacement

str.gsub(pattern, replacement) =>|match| block } =>.gsub(/[aeiou]/, )              .gsub(/([aeiou])/, )         .gsub(/./) {|s| s[0].to_s +  }   

str.replace(other_str) =>=          s.replace    

 

str.delete([other_str]+) =>.delete ,        .delete             .delete ,    .delete           

 

7. Remove leading and trailing Spaces

str.lstrip =>.lstrip   .lstrip       

 

8. String Matching

str.match(pattern) => matchdata  nil

 

9. String Inversion

str.reverse =>.reverse   

 

10. Remove duplicate characters

str.squeeze([other_str]*) =>.squeeze                  .squeeze( )         .squeeze()   

 

11. Convert to numbers

str.to_i=>.to_i             

 

Differences between chomp and chop:
Chomp: Remove \ n or \ r at the end of the string.
Chop: removes the last character at the end of the string, whether it is \ n \ r or a common character.

.chomp            .chomp          .chomp        .chomp        .chomp          .chomp()     .chop   .chop   .chop     .chop       

Reprinted from: http://blog.163.com/ma95221@126/blog/static/2482210220100159515220/

 

Author: Civil Engineering (http://www.cnblogs.com/hongfei)

Address: http://www.cnblogs.com/hongfei/p/3688198.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.