[] 中的索引

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. bigint数据类型

    尽管int依然是SQL Server 2000中最主要的整数数据类型,但是SQL Server 2000还是新增加了整数数据类型bigint,它应用于整数超过int数据范围的场合. int数据类型所表 ...

  2. NOSQL场景梳理

    Redis 场景:缓存,Session,消息发布订阅,产品属性分析,订单购买等强事务,计数等   Memcached 场景:读密集,写一般的缓存,Session   MongoDB 场景:数据显示,查 ...

  3. c# 反射事件

    被反射类中: public delegate void CompeletedHandler(); public static event CompeletedHandler AnalysisCompe ...

  4. 从DataGridView导出Excel

    从DataGridView导出Excel的两种情况,不多说,直接记录代码(新建类,直接引用传入参数). using System; using System.Collections.Generic; ...

  5. 无聊拆中国银行密码器和农业银行U盾

    原始状态 不知从何下手,直接斜口钳暴力剪开 开始露出电路板了,继续拆 拆完是这样的,屏幕没有焊接,直接靠外壳压上去的 背面图 相对而言,农行的就很好拆 后盖很好撬开 前面就是按键,没什么,屏是1286 ...

  6. SVM实践

    在Ubuntu上使用libsvm(附上官网链接以及安装方法)进行SVM的实践: 1.代码演示:(来自一段文本分类的代码) # encoding=utf8 __author__ = 'wang' # s ...

  7. mysql中影响myisam引擎写入性能的三项设置

    一.LOW_PRIORITY1.对于myisam默认是写操作优先,读操作滞后.通过该项更改,可以使读操作优先,写操作在有空闲的时候再写入.但该项可能在理论上造成,写被永远阻塞. SQL语句中使用示例: ...

  8. 自己生成nginx的https证书

    #自己生成ssl证书 这里说下Linux 系统怎么通过openssl命令生成 证书. 首先执行如下命令生成一个key openssl genrsa -des3 -out ssl.key 1024 然后 ...

  9. Apache增加Basic Auth

    在.htaccess文件中增加 AuthUserFile /var/www/htpasswd/test.htpasswd AuthName EnterPassword AuthType Basic r ...

  10. [daily][toolchain] 我的日常软件集

    序: 工欲善其事,必先利其器.针对各种各样的工作,我同样也在寻找各种各样的工具.记录以备忘,因为真的会忘... 已用的,在用的,就不记了.只记下新用的,其他的有时间慢慢补充进来. 文档编辑: Mark ...