ruby字符串相关方法
构造字符串字面量
方法一:
最简单的使用单引号或者双引号括起来的字符串,比如"hello"。
方法二:
使用%q配合分界符,%q代表单引号
str=%q!he/lo!
方法三:
使用%Q配合分界符,%Q代表双引号
str=%Q{he/lo}
方法四:
here document构建字符串,该方法比较适合用于多行字符串的创建。由<<和边界字符串作为开头,由边界字符串作为结尾,比如下列代码:
str = <<END_OF_STRING1
We are here now,
where are you?
END_OF_STRING1
puts str
输出结果为:
We are here now,
where are you?
较为复杂的是允许多个边界字符串对出现。
str = <<END_OF_STRING1,<<END_OF_STRING2
We are here now,
where are you?
END_OF_STRING1
I will leave now,
would you like to go with me?
END_OF_STRING2
puts str
输出结果为:
We are here now,
where are you?
I will leave now,
would you like to go with me?
连接字符串
+ 连接。ruby的+操作符不会将其右侧操作数自动转为字符串,你必须亲自动手:
planet_number=5
"hello planet #" + planet_number.to_s # to_s converts to str
在ruby中,采用字符串内插通常要比采用+操作符连接简单一些。在字符串内插时,对to_s的调用是自动进行的:
"hello planet ##{planet_number}"
字面量与copy-on-write技术
在Java中,如果两个String对象a和b的值都是"abcdef",如下:
String a="abcdef";
String b="abcdef";
那 么,JVM只会创建一个常量对象"abcdef",让a和b都指向它。但是在ruby中,采用了智能指针(熟悉c++的朋友清楚)的一个高级技术 copy-on-write,一开始也是共享同一个字符常量,但是一旦之后某个对象(比如b对象)进行了修改操作,则"abcdef"将产生一个副本,b 的修改操作在这个副本上进行。
更详细的讨论请参考http://developer.51cto.com/art/200811/98630.htm。
和Java的一些其他区别
Java的String每次执行修改操作,都不会改变自身,而是创建一个新的String对象,而Ruby每次的修改操作都会修改自身。
计算长度
puts "hello".length
访问字符和子字符串访问某个元素时返回的有由单个字符组成的字符串。
s="hello"
s[0] # "h"
s[s.length-1]
重点:
如果你试图访问一个超出了字符串末尾的字符,ruby不会抛出异常,只是简单的返回nil。
赋值:
赋值右侧可以是任意一个字符串,既可以包含多字符,也可以是空字符。
irb(main):067:0> s="hello"
=> "hello"
irb(main):068:0> s[-1]="abc"
=> "abc"
irb(main):069:0> s
=> "hellabc"
irb(main):070:0>
s[-s.length]等于s[1]
s[s.length]等于nil
获取子串:
方括号中使用有两个都好分割的操作数,第一个指定index(可以为负的),第二个指定长度值(必须非负)。
s = "hello"
s[0,2] # "he"
s[-1,1] # "o": returns a string, not the character code ?o
s[0,0] # "": a zero-length substring is always empty
s[0,10] # "hello": returns all the characters that are available
s[s.length,1] # "": there is an empty string immediately beyond the end
s[s.length+1,1] # nil: it is an error to read past that
s[0,-1] # nil: negative lengths don't make any sense
删除:
赋值非空字符串相当于删除。相应第,如果左侧的长度值为0,那么相当于插入:
s = "hello"
s[0,1] = "H" # Replace first letter with a capital letter
s[s.length,0] = " world" # Append by assigning beyond the end of the string 增加
s[5,0] = "," # Insert a comma, without deleting anything
s[5,6] = "" # Delete with no insertion; s == "Hellod"
Another way to extract, insert, delete, or replace a substring is by indexing a string
with a Range object. We’ll explain ranges in detail in §3.5 later. For our purposes here,
a Range is two integers separated by dots. When a Range is used to index a string, the
return value is the substring whose characters fall within the Range:
s = "hello"
s[2..3] # "ll": characters 2 and 3
s[-3..-1] # "llo": negative indexes work, too
s[0..0] # "h": this Range includes one character index
s[0...0] # "": this Range is empty
s[2..1] # "": this Range is also empty
s[7..10] # nil: this Range is outside the string bounds
s[-2..-1] = "p!" # Replacement: s becomes "help!"
s[0...0] = "Please " # Insertion: s becomes "Please help!"
s[6..10] = "" # Deletion: s becomes "Please!"
Don’t confuse string indexing with two comma-separated integers with this form that
uses a single Range object. Although both involve two integers, there is an important
difference: the form with the comma specifies an index and a length; the form that uses
a Range object specifies two indexes.
还可以用一个字符串来索引另一个字符串。
It is also possible to index a string with a string. When you do this, the return value is
the first substring of the target string that matches the index string, or nil, if no match
is found. This form of string indexing is really only useful on the lefthand side of an
assignment statement when you want to replace the matched string with some other
string:
s = "hello" # Start with the word "hello"
while(s["l"]) # While the string contains the substring "l"
s["l"] = "L"; # Replace first occurrence of "l" with "L"
end # Now we have "heLLo"
Finally, you can index a string using a regular expression. (Regular expression objects
are covered in §9.2.) The result is the first substring of the string that matches the
pattern, and again, this form of string indexing is most useful when used on the
lefthand side of an assignment:
s[/[aeiou]/] = '*' # Replace first vowel with an asterisk
对字符串进行迭代
更多:
http://blog.csdn.net/csfreebird/article/details/4646140
ruby字符串相关方法的更多相关文章
- ruby 字符串学习2
在一个ruby字符串中包含表但是或者变量.想使用不同的值替换表达式或者变量 1 类似java 或者python的printf-style方式 template = 'Oceania has alway ...
- 雷林鹏分享:Ruby 字符串(String)
Ruby 字符串(String) Ruby 中的 String 对象存储并操作一个或多个字节的任意序列,通常表示那些代表人类语言的字符. 最简单的字符串是括在单引号(单引号字符)内.在引号标记内的文本 ...
- Ruby字符串(1):String基本用法
String字符串 字符串由String类提供,除了直接使用单双引号或其它字面量创建字符串,也可以使用String.new()方法来创建. a = "hello" b = Stri ...
- Ruby字符串
在Ruby中的String对象持有和操纵的任意序列的一个或多个字节,通常表示人类语言的字符表示.简单的字符串文本括在单引号中,如 'This is a simple Ruby string liter ...
- ruby 字符串学习笔记1
1 从一种数据结构中构件字符串 hash = { key1: "val1", key2: "val2" } string = "" hash ...
- Ruby字符串的一些方法
最近因为公司需求开始看ruby,先从ruby的基本数据类型开始看 看到ruby的字符串类型string,发现ruby中的字符串单双引号是不一样的,这点和Python有那么点不一样 主要是我们对字符串进 ...
- ruby 字符串常用方法学习
引用链接:http://www.blogjava.net/nkjava/archive/2010/01/03/308088.html 1,切片:silce, [ ]-----------------[ ...
- Ruby字符串(2):String方法详细整理
String方法整理 官方手册 类方法 new new(str="") → new_str new(str="", encoding: enc) → new_s ...
- ruby字符串学习笔记5
1获取字符串某部分 s = "My kingdom for a string!" s.slice(3,7) # kingdom s[3,7] # kingdom s[/.ing/] ...
随机推荐
- LSJ_NHibernate第四章 MVC
前言: MVC现在已经成为web开发的一个主流趋势了,还没用过的小伙伴,你们已经落伍了,这里我推荐一篇学习博客 玩转Asp.net MVC 的八个扩展点 代码完全开源,下载地址:https://gi ...
- 转:四种方案解决ScrollView嵌套ListView问题
转载自:http://blog.sina.com.cn/s/blog_46798aa80101lxbk.html 原始的连接已经不知道是哪里了,项目中遇到了同样的问题,花了一下午都没有想到是嵌套引起的 ...
- web服务器压力测试工具
http_load 是运行在linux操作系统上的命令行测试工具, 用来对网站做压力测试. 下载地址:http://www.acme.com/software/http_load/http_load- ...
- dbforge studio for mysql 怎样破解
下载好dbforge studio压缩包有两个exe,dbforge.studio.for.mysql.6.0.315-loader.exe ,和dbforgemysql.exe,安装后目录在C:\P ...
- c编程:用户输入一个数值n,打印出出1到n之间的所有质数
#include <stdio.h> int func(int i ) { //定义一个变量temp=2,当主函数引入的数大于temp时进入for循环.当它在比自己小的数中找到一个能背整除 ...
- struts2基于Convention插件的约定映射使用
一.首先说明一点:所谓的基于Convention插件的约定优于配置的使用,并不是严格意义上的零配置,struts.xml文件并不能完全舍弃. 获得Convention插件功能,所必需的jar包有:|a ...
- Javascript 迭代法实现数组多条件排序
多条件排序可能有很多种思路,效率也各不相同,我的方法可能只适合自己用,毕竟目的是为了实现功能,所以采用了最笨的方法,不过效果还是很理想的,经过多次测试,6列1000行数据,平均排序时间大约是:28ms ...
- IOS 学习笔记 2015-04-15 控制器数据反向传值
// // FirstViewController.h // 控制器数据传递 // // Created by wangtouwang on 15/4/15. // Copyright (c) 201 ...
- 锋利的Jquery解惑系列(二)------插件开发大总结
申明:插件开发是实际项目就经常用到的,不过也是挺吃力的.笔者自己做项目时,看着我们老大写的jQuery一头桨糊,那叫个痛苦.后面果断买了本参考书以及浏览别人的博客,现在也算慢慢入门了.现在总结自己的一 ...
- extern “C”的作用
1.引言 C++语言的创建初衷是“a better C”,但是这并不意味着C++中类似C语言的全局变量和函数所采用的编译和连接方式与C语言完全相同.作为一种欲与C兼容的语言,C++保留了一部分过程 式 ...