参考链接: Python字符串串联Concatenation

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"

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"

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")

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 字符串

[转载] kotlin 字符串_Kotlin基本类型字符串相关推荐

  1. kotlin中判断字符串_Kotlin程序查找字符串中字符的频率

    kotlin中判断字符串 Given a string and a character, we have to find the frequency of the character in the s ...

  2. kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符

    kotlin中判断字符串 Given a string and a character, we have to remove all occurrences of the character in g ...

  3. kotlin 字符串_Kotlin程序确定字符串是否具有所有唯一字符

    kotlin 字符串 Given a string, we have to check whether it has all unique characters or not. 给定一个字符串,我们必 ...

  4. python序列类型包括字符串_python序列类型字符串列表元组

    字符串 序列:序号 列 012345678 索引 序列类型,就是带有索引的数据类型:字符串/列表/元祖 name = 'wuhao dsb' # 012345678 name3 = name[0] n ...

  5. 深度讲解TS:这样学TS,迟早进大厂【15】:字符串字面量类型

    博主是一个专注于前端开发的程序猿~ 曾经主做于vue,react,小程序,uniapp,RN等各大框架~ 现在主攻web安全,数据加密,项目架构,性能优化~ 技术之路,任道重远.未来属于努力奋斗的我们 ...

  6. java中字符串的创建_【转载】 Java中String类型的两种创建方式

    本文转载自 https://www.cnblogs.com/fguozhu/articles/2661055.html Java中String是一个特殊的包装类数据有两种创建形式: String s ...

  7. json字符串和字典类型的相互转换(转载)

    转自:http://www.cnblogs.com/YUTOUYUWEI/p/5585863.html 在开发过程中,有时候需要将json字符串转为字典类型,反之亦然,通常采用.Net的开源类库New ...

  8. 疯狂kotlin讲义连载之Kotlin的基础类型--字符串

    Kotlin使用String类型字符串.字符串表示一个有序的字符集合,例如在前面代码中看到的"fkit.org"."crazyit.org"等代表字符串,Str ...

  9. Kotlin笔记15——字符串转数字类型

    前言 在使用Java编程语言开发的,我们会经常遇到字符串转数字的需求.那么在Kotlin中是怎么实现的呢?接下来跟大家分享一下. 字符串转数字 首先我们必须保证字符串是数字类型,不能出现a3这种数字与 ...

最新文章

  1. ISME:水库蓝藻影响真核浮游生物的群落演替和物种共存
  2. SQLPLUS SPOOL命令使用详解
  3. 90%的开发都不太考虑这个,但只要出问题直接公司完蛋!
  4. rman备份恢复总结
  5. storm消息可靠机制(ack)的原理和使用
  6. 一拍即合、一见钟情之后,智慧城市的“福利”来啦……
  7. 一文看懂网络上采样层中的 align_corners
  8. 如何正确的停掉线程?这里面大有门道!
  9. Oracle视图、自定义函数、存储过程、触发器
  10. 微软欲打造开发者联盟!
  11. tomcat测试程序的安装
  12. 免费开源PDF复制格式整理神器
  13. 使用电脑热点和Fiddler对Android app进行抓包
  14. ‘gbk‘ codec can‘t decode byte 0xae in position 199: illegal multibyte sequen 问题解决
  15. mysql group by 用法解析
  16. Named Route index has a default child route. When navigating to this named route to name index
  17. 控制 input 输入框不能输入中文,即不能在输入框中使用输入法
  18. Android应用优化之流畅度优化实操
  19. sql server 2008 r2服务器无法启动,怎么办
  20. 头脑王者对战游戏html5源码,头脑王者分析笔记及微信小程序解包源码

热门文章

  1. java 数据路id增长策略_基于数据库实现ID自动生成策略
  2. 【codevs3119】高精度开根号(二分答案)
  3. 【操作系统】Mac环境配置
  4. 胡学纲数据结构c语言PDF,数据结构精品课程参考书目
  5. linux向用户发送消息,Linux终端中向记录的用户发送消息
  6. java 访问器_网络之美 JavaScript中Get和Set访问器的实现代码
  7. wifi安装linux分区,centos7配置wifi驱动
  8. read time out解决方法_quot;read-on-writequot; in InnoDB
  9. 图像过滤,so easy~~
  10. 通信网中的数据报子网和虚电路子网