String object learning notes in Ruby, rubystring

Source: Internet
Author: User

String object learning notes in Ruby, rubystring

1. String object definition

You can use "" And ''to define String objects. For simple strings, you are advised to use'' to define them. The efficiency is higher than "". The difference between "" And ''is that, the string in ''is the final form after definition. Even if the \ n line break is used, it is output as is, and" "is more like an expression, the parser processes the special characters and then outputs them. The following sample code is as follows:Copy codeThe Code is as follows:

I = 100.
S1 = 'the value of I variable is # {I }! \ N' # The value of I variable is # {I }! \ N [no output variable I value, and no line feed]
S2 = "The value of I variable is # {I }! \ N "# The value of I variable is 100! [Output the value of the I variable and wrap the line]

Another way to define a String is to define a formatted String, as shown in the following sample code:

The Code is as follows:
S3 = <OK _STR
The value
Of I variable
Is # {I}
OK _STR

Output:

The Code is as follows:
The value
Of I variable
Is 100

For special strings, strings defined by ''are directly sent to the operating system for execution as system commands. The following is an example of code:

The Code is as follows:
Puts 'Ruby-V' # output: ruby 1.9.3p392 (revision 39386) [x86_64-linux]
Puts 'rails-V' # output: rails 3.2.8

2. String object operation

There are many methods to operate the String object in Ruby. For details, refer to the API documentation provided by Ruby:

The Code is as follows:
X = 'ho! '
Y = 'Hello from'
Z = 'hello'

Puts x * 30 # output: Ho! Ho! Ho!
Puts y + self. to_s # output: Hello from main
Puts z <''<'World! '# Output: Hello world!

Puts x. object_id # output: 19196800
Puts (x * 3). object_id # output: 19196600

Puts y. object_id # output: 19196780
Puts (y + self. to_s). object_id # output: 19196520

Puts z. object_id # output: 19196760
Puts (z <''<'World! '). Object_id # output: 19196760

The code above shows that <is a String append operation, returns the original object, + and * operations, and returns the new object.

3. You can use an array to manipulate characters in a string.

The Code is as follows:
M = 'taobao'
# Change the second character a to o
M [1] = 'O'
Puts m
# Truncate a substring between 4th and 6th characters
Puts m [3 .. 5]


In ruby, what is the function of connecting objects with &? What is the returned result?

& = And

In Java?

You need to understand when to allocate heap memory and stack memory.
String a = "aaa ";
In this way, java first searches for the "aaa" string in the memory. If there is one, it returns the aaa address to it.
If not, create
String a = new String ("aaa ");
Whether or not there is "aaa" in the memory"
All open up a new memory to save it
You can use the following methods to verify
String a = "aaa ";
String B = "aaa ";
String c = new String ("aaa ");
System. out. println (a = B );
System. out. println (a = c );
The result should be:
True
False
As a java programmer, this question has a high probability of writing questions.
Remember it! O (distinct _ distinct) O ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.