Ruby学习笔记-String

简介: 字符串(String) 1.创建字符串        在Ruby中可以使用单引号和双引号两种方法来创建一个字符串。但是使用这两种方式创建特殊字符串时,效果有很大区别。        双引号创建字符串:Ruby会对字符串中的转义字符和特殊字符进行替换;        单引号创建字符串:不进行替...

字符串(String)

1.创建字符串

       Ruby中可以使用单引号和双引号两种方法来创建一个字符串。但是使用这两种方式创建特殊字符串时,效果有很大区别。

       双引号创建字符串:Ruby会对字符串中的转义字符和特殊字符进行替换;

       单引号创建字符串:不进行替换

示例:

puts 'This stirng \n delimited by single quotes'                  #\n:换行符

puts "This stirng \n delimited by double quotes"

       s=”Ruby”

puts 'Start to learn #{s}'                 

puts " Start to learn #{s}"

------------------------------ Result -------------------------------

     This stirng \n delimited by single quotes

This stirng

        delimited by double quotes

Start to learn #{s}

    Start to learn Ruby

 

2.%创建多行字符串

       如果希望一个字符串由多行文本字符组成的,可以使用%表示法来创建, 共有三种形式:

       %/ string / :其中”/”是分隔符,可以是斜线,括号等其它字符。”string”表示是一个多行文本字符串,此形式的功能类似于双引号创建的字符串。

       %Q/ string / :与%/ string / 用法相同。

       %q/ string / :此形式的功能类似于单引号创建的字符串。

示例:

       name="Kobe"

age = "23"

gender = "male"

 

       a=%q/

name :#{name}

age:#{age}

gender:#{gender}

/

b=%Q {

name :#{name}

age :#{age}

gender :#{gender}

}

      

       puts a,b

-----------------result--------------------

name:#{name}

age:#{age}

gender:#{gender}

 

name:Kobe

age:23

gender:male

 

3. Here Document

       这是Ruby提供的别外一种多行字符串的表示形式,其格式如下:

       <<标识符

              #string

       标识符

Note: 必须以两个”<”开头,标识符可以是任意字符,起始与结束的标识符要一致; 另外,在Here Document的语法中,还可以把”<<”后面的标识符用单引号或双引号括起来,用于确定不同的替换程度;

示例:

        name="Kobe"

age = "23"

gender = "male"

 

a=<<info

name:#{name}

age:#{age}

gender:#{gender}

info

puts a

--------------------result-----------------------

  name:Kobe

   age:23

   gender:male

 

4. 字符串的常用操作

1.合并字符串:

       + :原有字符串的值不变

       << :是在原有字符串的基础上添加另一个字符串,会修改原有字符串的值。

示例:

        a = "this is the first string!"

b = "---second"

c = a + b

puts c,a

d = a << b

puts d,a

-------------------result--------------------------

 this is the first string!---second

 this is the first string!

 this is the first string!---second

 this is the first string!---second

 

2.替换字符串:

     可以使用Ruby内置类中的replace方法进行字符串替换;当有一个字符串对象的内容被替换时,指定这个字符串的变量都会变化;

示例:

     a = "this is the first string!"

b = a

puts a,b

b.replace("hellow world")

puts a,b

-----------------result------------------------

 this is the first string!

 this is the first string!

 hellow world

 hellow world

 

3.改变字符串:

常用方法如下:

方法

说明

capitalize

将字符串首字母转换成大写

upcase

将字符串中所有字母转换成大写

downcase

将字符串中所有字母转换成小写

swapcase

将字符串大写字母转换成小写,小写字母转换成大写

strip

去掉字符串中的首尾空格

lstrip

去掉字符串中左边的空格

rstrip

去掉字符串中右边的空格

chop

删除字符串中的最后一个字符

chomp

删除字符串中的最后换行符

reverse

将字符串所有的单词倒过来,并将单词中的字符也倒过来

示例:

    a = "this is the first string!"

puts a.reverse

puts a

puts a.upcase

puts a

--------------result------------------

 !gnirts tsrif eht si siht

 this is the first string!           #原有字符串的值没有改变

 THIS IS THE FIRST STRING!

 this is the first string!

 

4.获取或替换字符和子字符串:

     Ruby可以使用 [] 读取字符串中的字符或子字符串,使用 []= 方法可以替换字符串中的字符和子字符串; [] 方法的参数是字符的索引,返回一个ASCII码值,可以使用chr方法把它转换为字符;

示例:

   a = "abcdefghijklmnopqrstuvwxyz"

   puts a[0]

   puts a[0].chr

   puts a[-1]                     #索引可为正数(从左开始,0开始)或负数(从右开始,-1开始)

   puts a[-1].chr

   puts a[2,5]                   #从索引为2的字符开始取5个字符

   puts a[0] = "z"

   puts a

   a[10,5] = "AAAAA"

   puts a

------------result---------------------------

97

a

122

z

cdefg

Q

Qbcdefghijklmnopqrstuvwxyz

QbcdefghijAAAAApqrstuvwxyz

 

5.比较两个字符串是否相等:

     1. 使用”==”或内置访方法 eql? 来比较两个字符串内容是否相等;

     2. 使用 equal? 方法比较两个对象是否相同;

示例:

if"one" == "one"  

    puts "equal"

    else

       puts "unequal"

    end    

if "one".eql?("one")

    puts "equal"

else

       puts "unequal"

end

if "one".equal?("one")

    puts "equal"

else

    puts "unequal"

end

--------------------result---------------------

equal

equ  al

unequal 

 

6. <=>:比较两个字符串大小:

     字符串比较大小时实际比较的是字母的ASCII码;

示例

puts "one" <=> "one"  

    puts "one" <=> "two"

    puts "one" <=> "apple"  

if "a" > "b"

         puts "a>b, true"

else

         puts "a>b, false"

end

---------------result------------------

0

-1

1

a>b, false

 

 

目录
相关文章
|
4月前
|
数据安全/隐私保护 Ruby 索引
|
4月前
|
存储 Ruby
|
4月前
|
存储 Ruby
|
6月前
|
存储 NoSQL Redis
Redis学习笔记-String数据类型及其节省空间优化
Redis学习笔记-String数据类型及其节省空间优化
67 0
|
6月前
|
存储 Go C语言
Go学习笔记-String底层实现原理
Go学习笔记-String底层实现原理
37 0
|
9月前
|
C++
C++学习笔记_05 string 2021-04-16
C++学习笔记_05 string 2021-04-16
|
API
java202302java学习笔记第十五天-string中api查找
java202302java学习笔记第十五天-string中api查找
40 0
java202302java学习笔记第十五天-string中api查找
|
存储 编译器 C++
C++学习笔记(十一)——String类的模拟实现(二)
C++学习笔记(十一)——String类的模拟实现
|
编译器 C++
C++学习笔记(十一)——String类的模拟实现(一)
C++学习笔记(十一)——String类的模拟实现
C++学习笔记(十一)——String类的模拟实现(一)
|
存储 人工智能 C++
C++学习笔记(十二)——String类练习题(下)
C++学习笔记(十二)——String类练习题(下)
C++学习笔记(十二)——String类练习题(下)