Bootstrap

kotlin 字符串_Kotlin基本类型字符串

kotlin 字符串

Kotlin has five basic data types. In a previous post, we discussed the first basic type in Kotlin, numbers. This time, the discussion will be related to strings.

Kotlin具有五种基本数据类型。 在上一篇文章中,我们讨论了Kotlin中的第一种基本类型,即number 。 这次,讨论将与字符串有关。

In Kotlin, like in most programming languages, strings are groups of characters that form what we normally use as words, even though this is not necessarily true, as strings can have a group of characters that don’t make any sense and still be the same type.

在Kotlin中,就像在大多数编程语言中一样,字符串是构成我们通常用作单词的字符组,尽管这不一定是正确的,因为字符串可能具有一组没有任何意义的字符,但仍然是相同的类型。

The syntax for strings is easy, just wrap your characters between double quotes and you will have a string:

字符串的语法很简单,只需将字符括在双引号中,您将获得一个字符串:

val string = "Hello, world!"

字符串的属性 (Properties of Strings)

1.它们是不可变的 (1. They are immutable)

In programming, when we talk about “immutability”, we refer to the inability to change an object. Take, for example, the following String:

在编程中,当我们谈论“不变性”时,是指无法更改对象。 以下面的字符串为例:

var myDescription = "I am a programmer"

There is no way to alter that String, we don’t have a function where you can tell the code: “After the sixth character of the variable, insert the word great.” If you wanted to change the string you would need to reassign it.

没有办法更改String,我们没有一个可以告诉代码的函数:“在变量的第六个字符之后,插入great 。” 如果要更改字符串,则需要重新分配它。

myDescription = "I am a great programmer"
Image for post

Important note: It is a common beginner confusion to think that the above statement is a mutation — it is not.

重要说明:初学者通常会感到困惑,认为以上陈述是一种变异,事实并非如此。

What happens with the above line is that we are “reassigning” the variable, which means we are destroying the previous value it had and assigning it a new one, which could be similar or completely different.

上一行发生的事情是,我们正在“重新分配”变量,这意味着我们正在销毁它具有的先前值,并为其分配一个新值,该值可能相似或完全不同。

2.字符串可以通过索引进行迭代和访问 (2. Strings can be iterated and accessed via indexing)

Strings in Kotlin behave just like an array would, you can access them through a zero-index logic:

Kotlin中的字符串的行为就像数组一样,您可以通过零索引逻辑来访问它们:

var anotherString = "I like programming in Kotlin" anotherString[3] // This would give the character 'i' as a result

Also, you could check each character with a loop:

另外,您可以使用循环检查每个字符:

for (letter in anotherString) { 
println(letter)
}

Pro tip: Iterating through strings in the above fashion is very useful in algorithmic problems, which are very common in coding interviews.

专家提示:以上述方式遍历字符串在算法问题中非常有用,而算法问题在编码采访中很常见。

3.字符串可以串联 (3. Strings can be concatenated)

This is not a recommended practice, but it is a feature of Kotlin Strings. If you use the + operator, you can concatenate, which is the same as “joining” two Strings together.

不建议这样做,但这是Kotlin Strings的功能。 如果使用+运算符,则可以连接,这与将两个字符串“连接”在一起相同。

var name = "Evana" 
var lastName = "Margain" var fullName = name + " " + lastname
// result: "Evana Margain"
Image for post

Now you should be thinking: “This looks very useful, why is it not recommended?”

现在您应该在思考:“这看起来非常有用,为什么不建议这样做?”

There are other ways to achieve the same thing. Actually, in programming, most tasks can be achieved through different methods. The recommended method is called String templates.

还有其他方法可以实现相同的目的。 实际上,在编程中,大多数任务可以通过不同的方法来完成。 推荐的方法称为String模板

4.字符串模板 (4. String templates)

If you have ever used the word template in another context, you know that a template is a base format for something, in this case, a String. Let’s take a look at an example of a String template:

如果您曾经在其他上下文中使用过单词模板 ,则知道模板是某种东西(在这种情况下为字符串)的基本格式。 让我们看一下String模板的示例:

val language = "Kotlin" println("My favorite language is $language") val anotherLanguage = "Swift" println("My favorite language is $anotherLanguage")
Image for post

Can you guess the results of the above code? The first line would print “My favorite language is Kotlin,” while the second one would print “My favorite language is Swift”.

您能猜出上面代码的结果吗? 第一行将显示“我最喜欢的语言是Kotlin”,第二行将显示“我最喜欢的语言是Swift”。

If you observe carefully, the way to insert a variable into the string is by adding a dollar sign before the variable name. Easy, isn’t it?

如果仔细观察,将变量插入字符串的方法是在变量名称前添加美元符号。 很简单,不是吗?

If you want to add one variable to another, remember you should always prefer String templates over concatenation.

如果要将一个变量添加到另一个变量,请记住,始终应优先使用String模板,而不要使用串联模板。

5.字符串文字:转义的字符串 (5. String literals: escaped Strings)

When you are using Strings, sometimes you have to use characters that may confuse the compiler, for example:

使用字符串时,有时必须使用可能会使编译器感到困惑的字符,例如:

val famousQuote = ""Be yourself; everyone else is already taken." ― Oscar Wilde"

If the computer tries to interpret the code above, it will think you are closing your String, while in reality, you want to put a double quote as part as your String.

如果计算机试图解释上面的代码,它会认为您正在关闭String,而实际上,您想将双引号作为String的一部分。

The above code is not valid, but making it work is easy, just add a backslash behind the character.

上面的代码无效,但是使其工作很容易,只需在字符后面添加反斜杠即可。

val famousQuote = "\"Be yourself; everyone else is already taken.\" ― Oscar Wilde"

Now the compiler will know that you are trying to add a double quote, and will search for the closing quote that doesn’t have a backslash before it.

现在,编译器将知道您正在尝试添加双引号,并将搜索在其前没有反斜杠的结束引号。

According to the Kotlin documentation, the available escape sequences are: \t, \b, \n, \r, \', \", \\, and \$. But you don't have to learn this, there is an even easier method, take a look at the next part.

根据Kotlin文档 ,可用的转义序列为: \t\b\n\r\'\"\\\$ ,但是您不必学习,甚至更简单的方法,请看下一部分。

6.字符串文字:原始字符串 (6. String literals: raw Strings)

Another way you can include characters that would normally be a part of code is through raw Strings. Let’s look at an example:

可以包含通常会成为代码一部分的字符的另一种方法是通过原始字符串。 让我们看一个例子:

""" 
"Be yourself; everyone else is already taken." ― Oscar Wilde
"""

As you can see in the above code, raw strings begin with three quotes and end with another three, just make sure you put the quotes on a line of their own.

如您在上面的代码中看到的,原始字符串以三个引号开头,再以另外三个引号结尾,只需确保将引号放在它们自己的一行上即可。

Now you can put any character between them. If you add a break, it will also be respected. This is a feature not many programming languages have, take advantage of it! Sometimes, trying to escape many characters gets annoying and frustrating.

现在,您可以在它们之间放置任何字符。 如果您添加休息时间,则也将受到尊重。 这是许多编程语言所没有的功能,请利用它! 有时,试图逃避许多角色会变得烦人和沮丧。

Congratulations! This is all you need to know about Strings in Kotlin! As you can see, Kotlin is a very easy language, but “with great power comes great responsibility,” so don’t give up on your Kotlin learning path!

恭喜你! 这就是您需要了解的关于Kotlin中Strings的所有信息! 如您所见,Kotlin是一种非常简单的语言,但是“功能强大,责任重大”,所以不要放弃Kotlin的学习道路!

Until next time!

直到下一次!

翻译自: https://medium.com/better-programming/kotlin-basic-types-strings-acd37b7121e8

kotlin 字符串

;