[] 中的索引

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. <二>JDBC_通过ResultSet执行查询操作

    一.ResultSet: 结果集. 封装了使用 JDBC 进行查询的结果.  1. 调用 Statement 对象的 executeQuery(sql) 可以得到结果集. 2. ResultSet 返 ...

  2. css3动态旋转魔方练习

    图片素材: html部分: <!DOCTYPE html><html><head> <meta charset="utf-8" /> ...

  3. JavaScript、tabel切换完整版—自动切换—鼠标移入停止-移开运行

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 基于Jquery的页面过渡效果(原创)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  5. Ecshop:后台添加新功能栏目以及管理权限设置

    一.添加菜单项 打开 /admin/includes/inc_menu.php文件(后台框架左边菜单),在最后添加一行如下: $modules['17_other_menu']['sns_list'] ...

  6. WPF中UserControl和DataTemplate

    最新更新: http://denghejun.github.io 前言 前言总是留给我说一些无关主题的言论,WPF作为全新Microsoft桌面或web应用程序显示技术框架, 从08年开始,一直到现在 ...

  7. antlr.collections.AST.getLine()I异常

        antlr.collections.AST.getLine()I异常 Struts+hibernate+spring项目经常遇到问题 因为Struts自带的antlr-2.7.2.jar,比H ...

  8. 使用 PHP 过滤器(Filter)进行严格表单验证

    PHP 过滤器(Filter)用于验证和过滤来自非安全来源的数据,比如用户的输入,使用过滤器扩展可以使数据过滤更轻松快捷.要求的 PHP 版本是 PHP 5 >= 5.2.0,PHP 7 和 F ...

  9. web应用程序开发原理

    企业应用计算的演变为1.主机/哑终端的集中计算模式: 2.客户机/服务器计算模式:3.浏览器    /服务器计算模式.其中,1具有部署方面的优势,但在一台计算机中进行全部的处理,应用程序难于维护,难于 ...

  10. scala的面向对象编程

    1.scala的简单编程 2.构造方法 辅助构造函数是在主构造函数没有的情况下,执行的构造函数. 3.object的介绍 4.半生类和半生对象 5.半生的案例程序(半生类可以调用半生) 6.apply ...