[] 中的索引

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. c++ 离散数学 群的相关判断及求解

    采用C/C++/其它语言编程,构造一个n阶群<G={a,b,c,…},*>,G的阶|G|满足:3<=|G|<=6 1.判断该群是否是循环群,若是,输出该群的某个生成元. 2.给 ...

  2. node.js中module.export与export的区别。

    对module.exports和exports的一些理解 可能是有史以来最简单通俗易懂的有关Module.exports和exports区别的文章了. exports = module.exports ...

  3. 3.Powershell编辑器

    工欲善其事,必先利其器.有个得心应手的工具会使你的学习事半功倍.使用什么工具来编辑Powershell指令比较方便呢?笔者前后使用过几个编辑器,有几个比较不错推荐给大家试用. Powershell I ...

  4. GIT 操作

    1. 查看某个文件某次提交修改的内容 git show commitid  a.txt  2. git rm 和 git rm --cached  当我们需要删除暂存区或分支上的文件, 同时工作区也不 ...

  5. eclipse如何快速查找某个类

    2. [Ct rl+Shift +T ]    查找工作空间(Workspace)构建路径中的可找到Java类文件,不要为找不到类而痛苦,而且可以使用“*”.“?”等通配符. 3. 当我们编写了很多的 ...

  6. 关于thinkphp开发的几种规范(仅限个人)

    一.只要设计到where查询语句,无论是增删改查 $cn['username'] = session('member.username'); $cn['itemid'] = $itemid; $ite ...

  7. ZTE交换路由设备配置的备份与恢复

    一.TFTP服务器搭建 使用用户计算机搭建TFTP服务器,交换路由设备作为TFTP客户端. 运行TFTPServer.exe,该程序所在的目录为TFTP的根目录. 请保证您的TFTP可以Ping通所要 ...

  8. mysql临时禁用触发器

    mysql支持设定session变量,并且有带入到触发器中使用的能力,故可以间接的设置触发器失效 思路是: 在执行前设定一个session变量,执行过程中判断该变量的值(没有设定该变量的值时该变量默认 ...

  9. Linux_函数使用手册(中、英),确实不错

    http://files.cnblogs.com/files/findumars/Linux_functions_ch_en.rar

  10. magento后台paypal设置

    如何在magento后台设置paypal呢? 这边把整理的简单跟大家分享一下. 1.system->config-paypel1.1 Merchant Country 设置国家1.2 Email ...