Files
freeCodeCamp/guide/russian/ruby/ruby-string-operations/index.md
2018-10-16 21:32:40 +05:30

51 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Ruby String Operations
localeTitle: Операции с Ruby String
---
Как конкатенацию, так и умножение можно выполнять по строкам.
## конкатенация:
* Строки можно объединить, используя любой из следующих способов:
* `+` оператор
* `<<` оператор
* `.concat` метод
```ruby
"Hello" + " World" + "!" #=> Hello World!
```
```ruby
"Hello" << " World!" #=> Hello World!
```
```ruby
string1 = "Hello"
string2 = " World!"
string1.concat(string2) #=> Hello World!
```
## Умножение:
* Строки могут быть умножены на целое значение с помощью оператора `*` . `ruby "Hello" * 3 #=> HelloHelloHello`
## Замена подстроки
* Мы можем искать подстроки или использовать Regex для поиска и замены символа в строке. `ruby "Hey mom, look at this string".sub('mom', 'dad') #=> Hey dad, look at this string`
## Сравнение:
* Строки можно сравнивать, возвращает -1, 0, +1 или ноль в зависимости от того, меньше или меньше строки, чем другая\_страница.
```ruby
"abcdef" <=> "abcde" #=> 1
"abcdef" <=> "abcdef" #=> 0
"abcdef" <=> "abcdefg" #=> -1
"abcdef" <=> "ABCDEF" #=> 1
```