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

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
Defer
Функции коллекции (Collection Functions)
String Functions
Строковые функции (String Functions)
String Formatting
Regular Expressions
JSON

View File

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