字符串处理函数
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')
-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])/, '<\1>')         #=> "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=> str
".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"
复制代码
转载自:http://blog.163.com/ma95221@126/blog/static/2482210220100159515220/ 

ruby 字符串的更多相关文章

  1. ruby 字符串学习2

    在一个ruby字符串中包含表但是或者变量.想使用不同的值替换表达式或者变量 1 类似java 或者python的printf-style方式 template = 'Oceania has alway ...

  2. 雷林鹏分享:Ruby 字符串(String)

    Ruby 字符串(String) Ruby 中的 String 对象存储并操作一个或多个字节的任意序列,通常表示那些代表人类语言的字符. 最简单的字符串是括在单引号(单引号字符)内.在引号标记内的文本 ...

  3. Ruby字符串(1):String基本用法

    String字符串 字符串由String类提供,除了直接使用单双引号或其它字面量创建字符串,也可以使用String.new()方法来创建. a = "hello" b = Stri ...

  4. Ruby字符串

    在Ruby中的String对象持有和操纵的任意序列的一个或多个字节,通常表示人类语言的字符表示.简单的字符串文本括在单引号中,如 'This is a simple Ruby string liter ...

  5. ruby 字符串学习笔记1

    1 从一种数据结构中构件字符串 hash = { key1: "val1", key2: "val2" } string = "" hash ...

  6. ruby字符串相关方法

    构造字符串字面量 方法一:最简单的使用单引号或者双引号括起来的字符串,比如"hello". 方法二:使用%q配合分界符,%q代表单引号str=%q!he/lo! 方法三:使用%Q配 ...

  7. Ruby字符串的一些方法

    最近因为公司需求开始看ruby,先从ruby的基本数据类型开始看 看到ruby的字符串类型string,发现ruby中的字符串单双引号是不一样的,这点和Python有那么点不一样 主要是我们对字符串进 ...

  8. ruby 字符串常用方法学习

    引用链接:http://www.blogjava.net/nkjava/archive/2010/01/03/308088.html 1,切片:silce, [ ]-----------------[ ...

  9. Ruby字符串(2):String方法详细整理

    String方法整理 官方手册 类方法 new new(str="") → new_str new(str="", encoding: enc) → new_s ...

  10. ruby字符串学习笔记5

    1获取字符串某部分 s = "My kingdom for a string!" s.slice(3,7) # kingdom s[3,7] # kingdom s[/.ing/] ...

随机推荐

  1. 【Java】List转化为数组

    List转化为数组的两种方式: 第一种: List<String> list = new ArrayList<>(); String [] arr = list.toArray ...

  2. 一次lenovo a390t刷机体验

    今天一朋友说自己的联想a390t手机有时候打着打着电话就没声音了,手机好像死机了一样,以前用着挺好的没什么毛病. 因为以前用刷机精灵刷过几个android手机,感觉挺简单的,只要找好对应的rom点击两 ...

  3. VS2010 + C#4.0使用 async + await

    方法一: 安装官方出的Microsoft.Bcl.Async包 最新发布日期为 2014/4/12,版本1.0.168 (不支持VS2010) 1.解决方案-右键-管理解决方案的NuGet程序包 2. ...

  4. linux下内存

    MMU由一个或一组芯片组成.其功能是把逻辑地址映射为物理地址,进行地址转换(MMU是CPU的一部分) 机器指令仍然用逻辑地址指定一个操作数的地址或一条指令的地址 每个逻辑地址都由一个段选择符(16位) ...

  5. 《linux 内核全然剖析》 mktime.c

    tm结构体的定义在time.h里面 struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_y ...

  6. 搭建rocketMq环境

    大体流程按照文章https://blog.csdn.net/wangmx1993328/article/details/81536168逐步搭建,下面列出踩过的一些坑 1,自己的阿里云服务器端口没开放 ...

  7. iOS开发中地图与定位

    不管是QQ还是微信的移动client都少不了定位功能,之前在微信demo中没有加入定位功能,今天就写个定位的小demo来了解一下定位和地图的东西. 地图和定位看上去是挺高大上一东西.其有使用方法比Ta ...

  8. jenkins构建一个python项目

    Jenkins发布后端python代码 “系统管理”“管理插件”“已安装” 检查是否有“Git plugin”和“Publish Over SSH”两个插件,如果没有,则需点击“可选插件”,找到它 ...

  9. Oracle创建自增字段和修改方法-ORACLE SEQUENCE的简单介绍

    http://blog.csdn.net/zhoufoxcn/article/details/1762351先假设有这么一个表: create table S_Depart  (    DepartI ...

  10. Windows 10正式版历代记:Version 1709、Build 16299都是什么鬼?

    Windows 10免费用!创意者更新秋季版激活秘籍 2017年10月中下旬,微软面向正式版用户推送了Windows 10创意者更新秋季版.这是自发布以来,Windows 10的第五个大版本. 在这篇 ...