Ruby 字符串基础知识
在Ruby中, 字符串 是一个或多个字符的序列。它可以由数字、字母或符号组成。在这里,字符串是对象,与其他语言不同的是,字符串是 可变的 ,也就是说,字符串可以被就地改变,而不是创建新的字符串。字符串的对象持有并操作一个任意的字节序列,该字节通常代表一个字符序列。
创建字符串: 要创建字符串,只需将字符序列放在双引号或单引号中。另外,用户可以将字符串存储到一些变量中。在Ruby中, 不需要指定变量的数据类型 。
例子
# Ruby program to demonstrate
# the creation of strings
# using single quotes
puts 'Ruby String using single quotes'
# using double quotes
puts "Ruby String using double quotes"
# storing string into variables
str1 = "GFG"
str2 = 'Geeks'
# displaying string
puts str1
puts str2
输出
Ruby String using single quotes
Ruby String using double quotes
GFG
Geeks
注意: 使用单引号和双引号的唯一区别是,双引号会 对 变量 进行插值 ,而单引号不能插值。
例子
# Ruby program to demonstrate the difference
# while using single and double quotes to
# create strings
# storing string into variables
str1 = "GFG"
str2 = 'Geeks'
# using single quotes
puts 'Cannot Interpolate str1: #{str1}'
# using double quotes
puts "Interpolating str2: #{str2}"
输出
Cannot Interpolate str1: #{str1}
Interpolating str2: Geeks
字符串是对象: 正如你所知,Ruby是一种面向对象的语言,所以Ruby中的字符串是对象。基本上,一个对象是一个数据和方法的组合,它可以增强通信属性。
例子
# Ruby program to illustrate that
# string are objects in Ruby
#!/usr/bin/ruby
# using double quotes
str = "GeeksforGeeks"
puts str
# using new method to create string
# object and assigning value to it
str2 = String.new "GeeksforGeeks"
puts str2
输出
GeeksforGeeks
GeeksforGeeks
访问字符串元素: 用户可以通过使用方括号[]访问字符串元素。在方括号[]中,用户可以传递字符串、范围或索引。
语法
name_of_string_variable[arguments]
例子
# Ruby program to illustrate the
# accessing of string
#!/usr/bin/ruby
# storing string in variable
str = "GeeksforGeeks Sudo Placements"
# accessing the specified substring
puts str["Geeks"]
puts str['for']
# passing index as an argument which returns
# the specified character
puts str[3]
# passing the negative index as an argument which
# returns the specified character from the
# last of the string
puts str[-3]
# passing Two arguments which are separated
# by a comma that returns characters starting
# from the 1st index and the 2nd index is the
# number of characters
puts str[14, 10]
# using range operators in passed arguments
puts str[14 .. 17]
输出
Geeks
for
k
n
Sudo Place
Sudo
创建多行字符串: 在Ruby中,用户可以很容易地创建多行字符串,而在其他编程语言中,创建多行字符串需要花费很多精力。在Ruby中,有三种创建多行字符串的方法,如下所示。
- 使用双引号( “”) 这是最简单的创建多行字符串的方法,只需将字符串放在双引号之间。在双引号之间,用户可以添加换行符,以此类推。
- 使用 (%/ /) 创建多行字符串只需将字符串放在%/和/之间。
- **使用 ( << STRING STRING) **要创建多行字符串,只需将字符串放在<< STRING和STRING之间。这里STRING应该是大写字母。
例子
# Ruby program to illustrate the
# multiline strings
#!/usr/bin/ruby
# Using Double Quotes
puts "In Ruby, a user can create the multiline
strings easily where in other programming
languages creating multiline strings
requires a lot of efforts"
puts ""
# Using %/ /
puts %/ In Ruby, a user can create the multiline
strings easily where into other programming
languages creating multiline strings
requires a lot of efforts/
puts ""
# Using <<STRING STRING
puts <<STRING
In Ruby, a user can create the multiline
strings easily where into other programming
languages creating multiline strings
requires a lot of efforts
STRING
输出
In Ruby, a user can create the multiline
strings easily where in other programming
languages creating multiline strings
requires a lot of efforts
In Ruby, a user can create the multiline
strings easily where into other programming
languages creating multiline strings
requires a lot of efforts
In Ruby, a user can create the multiline
strings easily where into other programming
languages creating multiline strings
requires a lot of efforts
字符串复制: 有时候,用户可能需要多次重复某种字符串。所以要在Ruby中进行字符串的复制,可以使用(*)
操作符。这个操作符前面是要复制的字符串,后面是要复制的次数。
语法
string_variable_or_string * number_of_times
例子
# Ruby program to illustrate the
# replication of strings
#!/usr/bin/ruby
# string to be replicate
str = "GeeksForGeeks\n"
# using * operator
puts str * 7
输出
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks
GeeksForGeeks