Go语言中使用字符串的简介

A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Made up of Unicode, strings are immutable sequences, meaning they are unchanging.

字符串是一个或多个字符(字母,数字,符号)的序列,可以是常数或变量。 字符串由Unicode组成 ,是不可变的序列,这意味着它们是不变的。

Because text is such a common form of data that we use in everyday life, the string data type is a very important building block of programming.

由于文本是我们在日常生活中使用的一种常见数据形式,因此字符串数据类型是编程中非常重要的组成部分。

This Go tutorial will go over how to create and print strings, how to concatenate and replicate strings, and how to store strings in variables.

该Go教程将介绍如何创建和打印字符串,如何连接和复制字符串以及如何将字符串存储在变量中。

字符串文字 (String Literals)

In Go, strings exist within either back quotes ` (sometimes referred to as back ticks) or double quotes ". Depending on which quotes you use, the string will have different characteristics.

在Go中,字符串存在于反引号` (有时称为反引号)或双引号" 。根据使用的引号,字符串将具有不同的特征。

Using back quotes, as in `bar`, will create a raw string literal. In a raw string literal, any character may appear between quotes, with the exception of back quotes. Here’s an example of a raw string literal:

使用反引号,如` bar ` ,将创建一个原始字符串字面量。 在原始字符串文字中,除反引号外,任何字符都可以出现在引号之间。 这是原始字符串文字的示例:

`Say "hello" to Go!`

Backslashes have no special meaning inside of raw string literals. For instance, \n will appear as the actual characters, backslash \ and letter n. Unlike interpreted string literals, in which \n would insert an actual new line.

反斜杠在原始字符串文字中没有特殊含义。 例如, \n将显示为实际字符,反斜杠\和字母n 。 与解释的字符串文字不同, \n将在其中插入实际的新行。

Raw string literals may also be used to create multi-line strings:

原始字符串文字也可以用于创建多行字符串:

`Go is expressive, concise, clean, and efficient.
Its concurrency mechanisms make it easy to write programs
that get the most out of multi-core and networked machines,
while its novel type system enables flexible and modular
program construction. Go compiles quickly to machine code
yet has the convenience of garbage collection and the power
of run-time reflection. It's a fast, statically typed,
compiled language that feels like a dynamically typed,
interpreted language.`

Interpreted string literals are character sequences between double quotes, as in "bar". Within the quotes, any character may appear with the exception of newline and unescaped double quotes.

解释的字符串文字是双引号之间的字符序列,例如"bar" 。 在引号内,除换行符和未转义的双引号外,任何字符都可以出现。

"Say \"hello\" to Go!"

You will almost always use interpreted string literals because they allow for escape characters within them.

您几乎总是使用解释的字符串文字,因为它们允许在其中使用转义字符。

Now that you understand how strings are formatted in Go, let’s take a look at how you can print strings in programs.

现在您已经了解了如何在Go中格式化字符串,下面让我们看一下如何在程序中打印字符串。

打印字符串 (Printing Strings)

You can print out strings by using the fmt package from the system library and calling the Println() function:

您可以通过使用系统库中的fmt包并调用Println()函数来打印出字符串:

fmt.Println("Let's print out this string.")

   
   
Output
Let's print out this string.

You have to import system packages when you use them, so a simple program to print out a string would look like this:

使用它们时,必须import系统软件包,因此一个简单的程序可以打印出一个字符串,如下所示:

package main

import "fmt"

func main() {
    fmt.Println("Let's print out this string.")
}

字符串串联 (String Concatenation)

Concatenation means joining strings together, end-to-end, to create a new string. You can concatenate strings with the + operator. Keep in mind that when you work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.

串联意味着将字符串首尾相连 ,以创建新字符串。 您可以使用+运算符连接字符串。 请记住,当您使用数字时, +将是加法运算符,但是当与字符串一起使用时,它是一个联接运算符。

Let’s combine the string literals "Sammy" and "Shark" together with concatenation through a fmt.Println() statement:

让我们通过fmt.Println()语句将字符串文字"Sammy""Shark"与串联组合在一起:

fmt.Println("Sammy" + "Shark")

   
   
Output
SammyShark

If you would like a whitespace between the two strings, you can simply include the whitespace within a string. In this example, add the whitespace within the quotes after Sammy:

如果您想在两个字符串之间使用空格,则只需在字符串中包含空格。 在此示例中,在Sammy之后的引号内添加空格:

fmt.Println("Sammy " + "Shark")

   
   
Output
Sammy Shark

The + operator can not be used between two different data types. As an example, you can’t concatenate strings and integers together. If you were to try to write the following:

不能在两种不同的数据类型之间使用+运算符。 例如,您不能将字符串和整数连接在一起。 如果要尝试编写以下内容:

fmt.Println("Sammy" + 27)

You will receive the following errors:

您将收到以下错误:


   
   
Output
cannot convert "Sammy" (type untyped string) to type int invalid operation: "Sammy" + 27 (mismatched types string and int)

If you wanted to create the string "Sammy27", you could do so by putting the number 27 in quotes ("27") so that it is no longer an integer but is instead a string. Converting numbers to strings for concatenation can be useful when dealing with zip codes or phone numbers. For example, you wouldn’t want to perform addition between a country code and an area code, but you do want them to stay together.

如果要创建字符串"Sammy27" ,则可以通过将数字27放在引号( "27" )中来实现,从而不再是整数,而是字符串。 在处理邮政编码或电话号码时,将数字转换为字符串以进行连接可能很有用。 例如,您不想在国家代码和区号之间执行加法运算,但是您希望它们保持在一起。

When you combine two or more strings through concatenation, you are creating a new string that you can use throughout your program.

当您通过串联组合两个或多个字符串时,您将创建一个可以在整个程序中使用的新字符串。

在变量中存储字符串 (Storing Strings in Variables)

Variables are symbols that you can use to store data in a program. You can think of them as an empty box that you fill with some data or value. Strings are data, so you can use them to fill up a variable. Declaring strings as variables can make it easier to work with strings throughout your Go programs.

变量是可用于在程序中存储数据的符号。 您可以将它们视为一个填充了一些数据或值的空盒子。 字符串是数据,因此您可以使用它们来填充变量。 将字符串声明为变量可以更轻松地在整个Go程序中使用字符串。

To store a string inside a variable, simply assign a variable to a string. In this case, declare s as your variable:

要将字符串存储在变量中,只需将变量分配给字符串。 在这种情况下,将s声明为您的变量:

s := "Sammy likes declaring strings."

Note: If you’re familiar with other programming languages, you may have written the variable as sammy. Go, however, favors shorter variable names. Choosing s for the variable name in this case would be considered more appropriate for the style in which Go is written.

注意:如果您熟悉其他编程语言,则可能已将变量编写为sammy 。 但是,Go倾向于使用较短的变量名。 在这种情况下,为变量名称选择s将被认为更适合于编写Go的样式。

Now that you have the variable s set to that particular string, you can print the variable like so:

现在您已将变量s设置为该特定字符串,您可以像下面那样打印该变量:

fmt.Println(s)

You will then receive the following output:

然后,您将收到以下输出:


   
   
Output
Sammy likes declaring strings.

By using variables to stand in for strings, you do not have to retype a string each time you want to use it, making it more simple for you to work with and manipulate strings within your programs.

通过使用变量来代替字符串,您不必每次都想使用一个字符串来重新键入它,从而使您在程序中使用和操作字符串变得更加简单。

结论 (Conclusion)

This tutorial went over the basics of working with the string data type in the Go programming language. Creating and printing strings, concatenating and replicating strings, and storing strings in variables will provide you with the fundamentals to use strings in your Go programs.

本教程介绍了使用Go编程语言处理字符串数据类型的基础知识。 创建和打印字符串,串联和复制字符串以及将字符串存储在变量中将为您提供在Go程序中使用字符串的基础。

翻译自: https://www.digitalocean.com/community/tutorials/an-introduction-to-working-with-strings-in-go

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值