ruby字符串截取字符串_在Ruby中创建字符串

ruby字符串截取字符串

Ruby| 字符串创建 (Ruby | String creation)

There are various methods through which we can create string objects. We will understand them with the help of syntaxes and their supporting program codes in the rest of the content.

我们可以通过多种方法来创建字符串对象 。 在其余内容中,我们将借助语法及其支持的程序代码来理解它们。

方法1:new(str =“”) (Method 1: new(str = ""))

This method is a public class method defined and declared for String class in Ruby's library. This method works in a way that it returns a new String instance which is a duplicate of str String object. You will have to invoke this method with the help of a new keyword along with the String class.

此方法是在Ruby的库中为String类定义和声明的公共类方法。 此方法以返回新的String实例的方式工作,该实例是str String对象的副本。 您将不得不借助new关键字以及String类来调用此方法。

Syntax:

句法:

    String.new(str = "")

Example:

例:

=begin
  Ruby program to demonstrate String generation		
=end

puts "String.new(str) method implementation"

String.new(str="Hello there! You are at includehelp.com")

puts "The string object generated is: #{str}"

Output

输出量

String.new(str) method implementation
The string object generated is: Hello there! You are at includehelp.com

Explanation:

说明:

In the above code, you can observe that we are generating an instance of String class with the help of the above method. We have invoked the new method along with String class and a duplicate copy of str has been generated.

在上面的代码中,您可以观察到,借助于上述方法,我们正在生成String类的实例。 我们将新方法与String类一起调用,并且已生成str的副本。

方法2:new(str =“”,encoding:enc) (Method 2: new(str="",encoding:enc))

This method is a variant of the method which we have discussed in method 1. You can pass an optional encoding argument along with the method which specifies the encoding methodology used in the new String object. If you are not specifying the encoding parameter then by default ASCII 8 bit encoding is used.

此方法是我们在方法1中讨论的方法的一种变体。您可以传递一个可选的encoding参数以及指定新String对象中使用的编码方法的方法。 如果未指定编码参数,则默认使用ASCII 8位编码。

Syntax:

句法:

    String.new(str = "")
    str.encode(Encoding::enc)

Example:

例:

=begin
  Ruby program to demonstrate String generation		
=end

puts "String.new(str) method implementation"

String.new(str="Hello there! You are at includehelp.com")
puts "Encoding used is: #{str.encoding}"

puts "String.new(str) and encode method implementation"

String.new(str1="Hello there! You are at includehelp.com")
string=str1.encode(Encoding::ISO_8859_1)
puts "Encoding used is: #{string.encoding}"

Output

输出量

String.new(str) method implementation
Encoding used is: UTF-8
String.new(str) and encode method implementation
Encoding used is: ISO-8859-1

Explanation:

说明:

In the above code, you can observe that we can set the type of encoding we want to use in our string object with the help of the method discussed above. You can see that the default encoding is UTF-8 and we have set it to ISO_8859_1.

在上面的代码中,您可以观察到,借助于上面讨论的方法,我们可以设置要在字符串对象中使用的编码类型。 您会看到默认编码为UTF-8 ,我们将其设置为ISO_8859_1

方法3:new(str =“”,capacity) (Method 3: new(str="",capacity))

This method is a variant of the methods discussed above. You can set the capacity of Strings as well for specifying the size of the internal buffer. This improves performance when the String object is concatenated many times.

该方法是上述方法的一种变体。 您还可以设置字符串的容量以指定内部缓冲区的大小。 当String对象被多次连接时,这可以提高性能。

Syntax:

句法:

    String.new(str = "",capacity:size)

Example:

例:

=begin
  Ruby program to demonstrate String generation		
=end

puts "String.new(str,capacity) method implementation"

String.new(str="Hello there! You are at includehelp.com",capacity:120)
puts "Encoding used is: #{str.encoding} and str is #{str}"

Output

输出量

String.new(str,capacity) method implementation
Encoding used is: UTF-8 and str is Hello there! You are at includehelp.com

Explanation:

说明:

In the above code, you can observe that how capacity of the String can be set with the help of new method.

在上面的代码中,您可以观察到如何借助new方法设置String的容量。

翻译自: https://www.includehelp.com/ruby/string-creation.aspx

ruby字符串截取字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值