红宝石 |字符串基础
在 Ruby 中,字符串是一个或多个字符的序列。它可能由数字、字母或符号组成。这里的字符串是对象,除了其他语言之外,字符串是可变的,即字符串可以就地更改而不是创建新的字符串。 String 的对象保存和操作通常表示字符序列的任意字节序列。
创建字符串:要创建字符串,只需将字符序列放在双引号或单引号中。此外,用户可以将字符串存储到某个变量中。在 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 <
输出:
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