строковые функции

This commit is contained in:
badkaktus 2019-10-08 22:35:46 +03:00
parent 4690c15e21
commit afe89e6383
2 changed files with 22 additions and 22 deletions

View File

@ -42,7 +42,7 @@ WaitGroups
Panic Panic
Defer Defer
Функции коллекции (Collection Functions) Функции коллекции (Collection Functions)
String Functions Строковые функции (String Functions)
String Formatting String Formatting
Regular Expressions Regular Expressions
JSON JSON

View File

@ -1,6 +1,6 @@
// The standard library's `strings` package provides many // Стандартная библиотека пакета `strings` предоставляет
// useful string-related functions. Here are some examples // множество удобных функций для работы со строками. Вот
// to give you a sense of the package. // некоторые из них.
package main package main
@ -9,19 +9,18 @@ import (
s "strings" s "strings"
) )
// We alias `fmt.Println` to a shorter name as we'll use // Создаем алиас для функции `fmt.Println`, т.к. далее мы будем
// it a lot below. // часто вызывать эту функцию.
var p = fmt.Println var p = fmt.Println
func main() { func main() {
// Here's a sample of the functions available in // Данные функции доступны в пакете `strings`. Обратите внимание,
// `strings`. Since these are functions from the // что все эти функции из пакета, а не методы строковых объектов.
// package, not methods on the string object itself, // Это означает, что нам необходимо передать первым аргументом
// we need pass the string in question as the first // функции, строку, над которой мы производим операцию. Вы можете
// argument to the function. You can find more // найти больше строковых функций в [`официальной документации
// functions in the [`strings`](http://golang.org/pkg/strings/) // к пакету`](http://golang.org/pkg/strings/).
// package docs.
p("Contains: ", s.Contains("test", "es")) p("Contains: ", s.Contains("test", "es"))
p("Count: ", s.Count("test", "t")) p("Count: ", s.Count("test", "t"))
p("HasPrefix: ", s.HasPrefix("test", "te")) p("HasPrefix: ", s.HasPrefix("test", "te"))
@ -36,16 +35,17 @@ func main() {
p("ToUpper: ", s.ToUpper("test")) p("ToUpper: ", s.ToUpper("test"))
p() p()
// Not part of `strings`, but worth mentioning here, are // Примеры ниже не относятся к пакету `strings`, но о них
// the mechanisms for getting the length of a string in // стоит упомянуть - это механизмы для получения длины
// bytes and getting a byte by index. // строки и получение символа по индексу.
p("Len: ", len("hello")) p("Len: ", len("hello"))
p("Char:", "hello"[1]) p("Char:", "hello"[1])
} }
// Note that `len` and indexing above work at the byte level. // Обратите внимание, что `len` и индексация выше работают на
// Go uses UTF-8 encoded strings, so this is often useful // уровне байтов. Go использует строки в кодировке UTF-8, так
// as-is. If you're working with potentially multi-byte // что это часто полезно как есть. Если вы работаете с
// characters you'll want to use encoding-aware operations. // потенциально многобайтовыми символами, вам нужно использовать
// See [strings, bytes, runes and characters in Go](https://blog.golang.org/strings) // операции с кодировкой. Смотрите [строки, байты, руны и символы
// for more information. // в Go](https://blog.golang.org/strings) для получения дополнительной
// информации.