[] 中的索引

a = "hello there"

a[1]                   #=> "e"

a[2, 3]                #=> "llo"

a[2..3]                #=> "ll"

a[-3, 2]               #=> "er"

a[7..-2]               #=> "her"

a[-4..-2]              #=> "her"

a[-2..-4]              #=> ""

a[12..-1]              #=> nil

a[/[aeiou](.)\1/]      #=> "ell"

a[/[aeiou](.)\1/, 0]   #=> "ell"

a[/[aeiou](.)\1/, 1]   #=> "l"

a[/[aeiou](.)\1/, 2]   #=> nil

a["lo"]                #=> "lo"

a["bye"]               #=> nil

索引不但支持 用范围

还支持 逗号分隔的两个参数(1st=首索引,2nd==长度)

支持 负索引

支持 正则

str[fixnum] = new_str

str[fixnum, fixnum] = new_str

str[range] = aString

str[regexp] = new_str

str[regexp, fixnum] = new_str

str[regexp, name] = new_str

str[other_str] = new_str

按字节处理bytes

按byte切片

byteslice(fixnum) → new_str or nil

byteslice(fixnum, fixnum) → new_str or nil

byteslice(range) → new_str or nil

bytesize → integer    Returns the length of str in bytes.

bytes {|fixnum| block } → str

bytes → an_enumerator

用正则替换

sub/gsub

"hello".gsub(/[aeiou]/, '*')                  #=> "h*ll*"

"hello".gsub(/([aeiou])/, '<\1>')             #=> "h<e>ll<o>"

"hello".gsub(/./) {|s| s.ord.to_s + ' '}      #=> "104 101 108 108 111 "

"hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}')  #=> "h{e}ll{o}"

'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*')    #=> "h3ll*"

match

'hello'.match('(.)\1')      #=> #<MatchData "ll" 1:"l">

'hello'.match('(.)\1')[0]   #=> "ll"

'hello'.match(/(.)\1/)[0]   #=> "ll"

'hello'.match('xx')         #=> nil

partition

partition(sep) → [head, sep, tail] click to toggle source

partition(regexp) → [head, match, tail]

scan

a = "cruel world"

a.scan(/\w+/)        #=> ["cruel", "world"]

a.scan(/.../)        #=> ["cru", "el ", "wor"]

a.scan(/(...)/)      #=> [["cru"], ["el "], ["wor"]]

a.scan(/(..)(..)/)   #=> [["cr", "ue"], ["l ", "wo"]]

字符串转symbol

intern → symbol

"Koala".intern         #=> :Koala

s = 'cat'.to_sym       #=> :cat

s == :cat              #=> true

s = '@cat'.to_sym      #=> :@cat

s == :@cat             #=> true

upto/downto/times/Range.each

String的更多相关文章

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  3. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  4. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  5. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  6. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

  10. BCL中String.Join的实现

    在开发中,有时候会遇到需要把一个List对象中的某个字段用一个分隔符拼成一个字符串的情况.比如在SQL语句的in条件中,我们通常需要把List<int>这样的对象转换为“1,2,3”这样的 ...

随机推荐

  1. ZeroMQ接口函数之 :zmq_bind - 绑定一个socket

    ZeroMQ 官方地址 : http://api.zeromq.org/4-0:zmq-bind zmq_bind(3) ZMQ Manual - ZMQ/3.2.5 Name zmq_bind -  ...

  2. javascript平时小例子⑦(鼠标跟随的div)

    <!doctype html><html> <head> <meta charset="utf-8"> <title>无 ...

  3. IOS 开发调用打电话,发短信

    1.调用 自带mail[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzl ...

  4. 使用ZIM桌面维基做笔记

    最近尝试了使用ZIM做笔记,感觉还不错 ubuntu下直接到软件中心即可安装,或者 sudo apt-get install zim windows下的到此下载http://www.glump.net ...

  5. linux系统安装(上)

    1.VMware虚拟机的软件应用 www.vmware.com 2.linux系统安装设置(分区为重点) 3.远程登录管理工具介绍centOS5.5 使用虚拟机的优点 1.不用分区 2.可以完成本机与 ...

  6. canvas绘制二次贝塞尔曲线----演示二次贝塞尔四个参数的作用

    canvas中绘制二次贝塞尔曲线的方法为ctx.quadraticCurveTo(x1,y1,x2,y2); 四个参数分别为两个控制点的坐标.开始点即当前canvas中目前的点,如果想从指定的点开始, ...

  7. Unity透明材质Batch

    NO Batch  ? 游戏场景中存在大量例子的时候,DrallCall的压力很大,但是遍历一遍之后发现,为啥一样的粒子特效竟然没有合并,why?经过很多测试后发现,如果把透明材质的修改为非半透明的, ...

  8. eclipse SE增加Web开发插件;安装配置Apache

    在eclipse SE版本点击"Help"--"Install New Software..."--"Work with"中输入" ...

  9. 浅谈java抽象类和接口

    第一次,写这个,没有把图片放上来,有兴趣的可以点击连接看原文 http://note.youdao.com/noteshare?id=aecbd52b9240f23c0954e8086b848a17 ...

  10. Log4J的入门简介学习【转】

    原文:http://blog.csdn.net/lengyuhong/article/details/5832799 简介: Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以 ...