`
hbxiao135
  • 浏览: 107762 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ruby_学习笔记:字符串处理函数

阅读更多

1.返回字符串的长度

str.length => integer

 

2.判断字符串中是否包含另一个串

str.include? other_str => true or false

"hello".include? "lo" #=> true

"hello".include? "ol" #=> false

"hello".include? ?h #=> true

 

3.字符串插入:

str.insert(index, other_str) => str

"abcd".insert(0, 'X') #=> "Xabcd"

"abcd".insert(3, 'X') #=> "abcXd"

"abcd".insert(4, 'X') #=> "abcdX"

"abcd".insert(-3, 'X') #=> "abXcd"

"abcd".insert(-1, 'X') #=> "abcdX"

 

4.字符串分隔,默认分隔符为空格

str.split(pattern=$;, [limit]) => anArray

" 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,,"]

 

5.字符串替换

str.gsub(pattern, replacement) => new_str

str.gsub(pattern) {|match| block } => new_str

"hello".gsub(/[aeiou]/, '*') #=> "h*ll*" #将元音替换成*号

"hello".gsub(/([aeiou])/, '') #=> "h<e>ll<o>" #将元音加上尖括号,\1表示保留原有字符???

"hello".gsub(/./) {|s| s[0].to_s + ' '} #=> "104 101 108 108 111 " 字符串替换二:

str.replace(other_str) => str

s = "hello" #=>"hello"

s.replace "world" #=>"world"

 

6.字符串删除:

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

"hello".delete "l","lo" #=> "heo"

"hello".delete "lo" #=> "he"

"hello".delete "aeiou", "^e" #=> "hell"

"hello".delete "ej-m" #=> "ho"

 

7.去掉前和后的空格

str.lstrip => new_str

" hello ".lstrip #=> "hello "

"hello".lstrip #=> "hello"

 

8.字符串匹配 str.match(pattern) => matchdata or nil

 

9.字符串反转

str.reverse => new_str

"stressed".reverse #=> "desserts"

 

10.去掉重复的字符

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

"yellow moon".squeeze #=> "yelow mon" #默认去掉串中所有重复的字符

" now is the".squeeze(" ") #=>" now is the" #去掉串中重复的空格

"putters shoot balls".squeeze("m-z") #=> "puters shot balls" #去掉指定范围内的重复字符

 

11.转化成数字

str.to_i=&gt; str "12345".to_i #=> 12345

chomp和chop的区别: chomp:去掉字符串末尾的\n或\r chop:去掉字符串末尾的最后一个字符,不管是\n\r还是普通字符 "hello".chomp #=> "hello"

"hello\n".chomp #=> "hello"

"hello\r\n".chomp #=> "hello"

"hello\n\r".chomp #=> "hello\n"

"hello\r".chomp #=>"hello"

"hello".chomp("llo") #=>"he"

"string\r\n".chop #=> "string"

"string\n\r".chop #=> "string\n"

"string\n".chop #=> "string"

"string".chop #=> "strin"

"x".chop.chop #=> ""</o></e>

分享到:
评论

相关推荐

    ruby_full:RoR_Full_29 :: Ruby_reports

    ruby_full:RoR_Full_29 :: Ruby_reports

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    demo_web_app:这是howitzer_example的演示Web应用程序

    The available options are: [:ruby, :ruby_18,:ruby_19, :ruby_20, :mri, :mri_18, :mri_19, :mri_20, :rbx, :jruby, :mswin,:mingw, :mingw_18, :mingw_19, :mingw_20]然后更新捆绑器: gem update bundler在...

    share_some_love:与我们的好友Ruby分享爱心!

    与我们的好友Ruby分享一些爱! 对所有花时间创建和维护我们使用的开源软件的仁慈的人表示感谢! 问题 您是否曾经想过有时要说谢谢您有多重要? 回复好心的话是多么高兴! 尤其是当某些东西对您来说是一种爱好时,...

    ruby_cld2:用于 Google CLD2 的 Ruby 包装器

    包括宝石 gem 'ruby_cld2' , :git =&gt; 'git@github.com:craig-day/ruby_cld2.git' 湾在给定的字符串上调用该函数。 require 'cld2'#...CLD2 . detect_language ( "Hola mis amigos!" )=&gt; { :name =&gt; "SPANISH" , :...

    ruby的二进制字符串与hex互转,二进制字符串与整数互转的工具函数

    本资源是ruby代码,提供了一系列封装好的函数,用于快速进行转换,一个函数搞定,包括如下转换,二进制字符串与hex字符串的互转。二进制字符串与整数互转,包括uint8,uin16,uint32, 以及本地字节序和网络字节序两种...

    payoneer_ruby_sdk:PayoneerRubySDK

    Payoneer Ruby SDK Payoneer Ruby SDK。 安装 将此行添加到您的应用程序的Gemfile中: gem 'payoneer_ruby_sdk' 然后执行: $ bundle 或将其自己安装为: $ gem install payoneer_ruby_sdk 配置 Payoneer . ...

    vite_ruby::high_voltage:Ruby中的Vite.js,为您JavaScript体验带来欢乐

    是将前端工具作为Ruby进行编程,纯属喜悦! :smiling_face_with_heart-eyes: 此回购包含的库将使您可以轻松地将Vite集成到您喜欢的Ruby框架中,例如或 。 为什么要Vite? :thinking_face: Vite,这意味着开发...

    ruby_terminal_games:一些用Ruby编写的基本游戏,可以玩得开心

    $ gem install ruby_terminal_games 用法 $ ruby_terminal_games Usage: ruby_terminal_games COMMAND [ARGS] The most common commands are: list Get the list of games play Have fun ! about What is the ...

    Ruby-rubybuild编译和安装Ruby

    ruby-build - 编译和安装Ruby

    Ruby中常用的字符串处理函数使用实例

    1.返回字符串的长度 代码如下: str.length =&gt; integer 2.判断字符串中是否包含另一个串 代码如下: str.include? other_str =&gt; true or false “hello”.include? “lo” #=&gt; true “hello”.include? “ol” #=&gt;...

    ruby-changelog:RubyCHANGELOG

    Ruby版本changelog 介绍 在上查看此项目的完整版本 本页面最后更新于2021-04-09通过rake md task Ruby更新日志 Ruby 3.0 实施: :gear: MJIT改进 :NEW_button: Ractor-像并发抽象的Actor模型 :test_tube: :NEW_...

    Ruby_on_Rails笔记

    此文档我学习Ruby on Rails技术的综合 ,有读书笔记、心得自创教程一些问题解决经验统分门别类集技术的综合 ,有读书笔记、心得自创教程一些问题解决经验统分门别类集技术的综合 ,有读书笔记、心得自创教程一些问题...

    jquery_string_helper:一些字符串助手方法的 Jquery 插件

    我们使用相同的 Ruby on Rails Active 支持字符串辅助方法来简化字符串操作。 #A Jquery Plugins For Some String Helper methods like Camelize, Underscore, Dasherize, Classify, Ordinalize, ForeignKey 所有...

    ruby_version:Ruby版 | 比RUBY_VERSION好

    Ruby 版本 提供RubyVersion以简化在程序中检查正确 Ruby 版本的过程。安装 在您的命令行上: $ gem install ruby_version 在Ruby中: require 'ruby_version'用法 # Output RUBY_VERSIONRubyVersion . to_s# Check ...

    understand_ruby_memory:试图了解Ruby内存:full_moon_face:

    了解Ruby内存使用情况 :thinking_face: 我最初发表这篇文章是为了补充我对Ruby记忆的疑问(在MRI中)。 我没有CS学位,但了解Ruby如何使用内存并非易事。 我对这个主题充满热情。 该项目现在是Wiki。 随时贡献自己...

    king_placeholder:解析字符串中的占位符

    这个 gem 对用户在电子邮件、文本和导出模板中提供的字符串进行简单的 [占位符] 替换。 占位符在每个类中声明,然后任何包含[占位符] 的字符串都可以在模型范围内解析。 解析是由一个简单的状态机完成的,使用...

    Ruby_On_Rails笔记

    这个是我从百度文库里找的rails笔记,原文作者用清晰的语言代领初学者怎样快速入门Rails

    rgeo:Ruby的地理空间数据库

    RGeo RGeo是Ruby的地理空间数据库。 :warning_selector: :warning_selector: :warning_selector: :warning_selector: :warning_selector: :warning_selector: :warning_selector: :warning_selector: :warning_...

    ruby_s7plc:用于Simatic S7-300 PLC数据交换的Ruby库

    ruby_s7plc 用于西门子Simatic S7-300 PLC数据交换的Ruby库。快速开始 require 'simatic' Simatic :: Plc . exchange ( '192.168.0.1' ) do | plc | plc . write ( 'db1.dbw2' =&gt; 0xffff ) res = plc . read ( 'db1....

Global site tag (gtag.js) - Google Analytics