ruby
Ruby弦 (Ruby Strings)
We know that Strings are the sequence of characters which are used to represent some texts; they may also contain some numbers, spaces, and symbols. For example, "Mangoes" and "There are 3 mangoes!" both are strings. Generally, for identification purpose strings are enclosed in quotation marks.
我们知道, 字符串是用于表示某些文本的字符序列。 它们可能还包含一些数字,空格和符号。 例如,“芒果”和“有3个芒果!” 都是字符串。 通常,出于标识目的,字符串用引号引起来。
Let us understand, what strings are meant in Ruby?
让我们了解一下,Ruby中的字符串是什么意思 ?
Strings are considered as objects in Ruby. You will frequently use strings while programming. You will use them to communicate with users by displaying messages in the form of messages on the Interface.
字符串在Ruby中被视为对象 。 编程时会经常使用字符串。 您将通过在界面上以消息形式显示消息来使用它们与用户进行通信。
弦形成 (String formation)
In Ruby, strings can be formed using single quotes ' ' or double quotes " ". As far as you are consistent, both the cases are kind of same. You will find the difference during string interpolation. The string can be created in the following way:
在Ruby中,可以使用单引号''或双引号“”形成字符串。 据您一致,这两种情况都是相同的。 您将在字符串插值期间发现差异。 可以通过以下方式创建字符串:
'I love Includehelp'
#or
"I love the tutorials of Includehelp"
Communicating through Strings:
通过字符串进行通信:
You can print a string with the help of print and puts statement in the following way:
您可以通过以下方式在print和puts语句的帮助下打印字符串 :
# Using the print statement:
print "Hello there!"
print "Welcome to Includehelp."
print "Go through the tutorials of your choice!"
# Using puts statement:
puts # to print a new line only
puts "Hello there!"
puts "Welcome to Includehelp."
puts "Go through the tutorials of your choice!"
Output
输出量
Hello there!Welcome to Includehelp.Go through the tutorials of your choice!
Hello there!
Welcome to Includehelp.
Go through the tutorials of your choice!
You can note that with puts statement, the strings are getting printed in different lines but this is not the case of print statement.
您可能会注意到,使用puts语句时 ,字符串将以不同的行进行打印,但print语句并非如此。
字符串容器 (String containers)
We often need strings to be stored in some space or memory, we use variables for that purpose. Strings can be stored in the variables using assignment operator = and single quotes ' ' or double quotes " " in the following manner:
我们经常需要将字符串存储在某个空间或内存中,为此我们使用了变量。 可以使用赋值运算符=和单引号''或双引号“”通过以下方式将字符串存储在变量中:
# assigning string to a variable
variable_string = 'Hi there!'
# printing string
puts variable_string
Output
输出量
Hi there!
It enhances the code reusability as we don't have to type same string multiple times, just call the variable whenever you need the string.
它提高了代码的可重用性,因为我们不必多次键入相同的字符串,只需在需要字符串时调用变量即可。
字符串串联 (String concatenation)
Concatenation simply means joining the two strings together. Two or more than two strings can be concatenated by using + operator in the following way:
串联只是意味着将两个字符串连接在一起。 可以通过以下方式使用+运算符来连接两个或两个以上的字符串:
# concatenating two strings
result_1 = "Include"+"help"
# assigning two strings in variables
string_1 = "Hello"
string_2 = "World!"
# concatenating string variables with space in between
result_2 = string_1 + " " + string_2
# printing values/strings (i.e. results)
puts result_1
puts result_2
Output
输出量
Includehelp
Hello World!
Now let us check whether we can concatenate an integer variable with string variable or not with the help of following code:
现在让我们检查是否可以在以下代码的帮助下将整数变量与字符串变量连接起来:
Example:
例:
# string
string1="Roll no: "
# integer
roll=27
# concatenating and assigning result to f_str
# it will return an error
f_str=string1+roll
# printing the result
puts f_str
Output
输出量
main.rb:7:in '+': can't convert Fixnum into String (TypeError)
from main.rb:7:in '<main>'
The compiler will reflect an error as there will be no implicit, conversion of fixNum to string. The objective can be achieved by applying .to_s() method like
编译器将反映一个错误,因为不会将fixNum隐式转换为string 。 该目标可以通过应用.to_s()方法来实现,例如
# string
string1="Roll no: "
# integer
roll=27
# concatenating and assigning result to f_str
# converting fixNum to string using .to_s() method
f_str=string1+roll.to_s
# printing the result
puts f_str
Output
输出量
Roll no: 27
The .to_s method will convert any data type variable into string type.
.to_s方法会将任何数据类型变量转换为字符串类型。
字符串插值 (String Interpolation)
Concatenation makes the output hard to debug and complex. String Interpolation provides an easy way to the purpose by embedding variables into the strings with the help of # and curly braces {} in the following way:
串联使输出难以调试且非常复杂。 字符串插值法通过在#和大括号{}的帮助下将变量嵌入到字符串中来提供一种简便的方法,其方式如下:
# assigning strings to the variables
str1 = "Hi there!"
str2 = "Includehelp.com"
# printing the value of the variables
puts "#{str1} This is #{str2}"
Output
输出量
Hi there! This is Includehelp.com
Implicit typecasting of variables into strings can also be achieved using String Interpolation, consider the given example
还可以使用String Interpolation实现将变量隐式转换为字符串,请考虑给定的示例
# assigning string and an integre values
# to the variables
str1 = "Hi there!"
marks = 89
# Implicit typecasting using String Interpolation
puts "#{str1} You got #{marks} marks"
Output
输出量
Hi there! You got 89 marks
You will notice that here we don't have to put any .to_s method for conversion purpose.
您会注意到,这里我们不必放置任何.to_s方法来进行转换。
ruby