freeCodeCamp/guide/chinese/kotlin/strings/index.md

4.0 KiB
Raw Blame History

title localeTitle
Strings 字符串

字符串

字符串是编程语言中的基本数据类型。字符串由String类型表示。字符串是不可变的。 Kotlin有一个丰富的API来处理字符串。

基本用法

宣言

// Explicit type declaration 
 var firstName : String = "Elon" 
 
 // or Implicit type declaration and will still compile 
 val lastName = "Musk" 

另外,请注意val变量类型的用法,以下是它的行为方式

firstName = "Mark" // can be changed 
 lastName = "Zuckerberg" // cannot be changed 
 lastName = 12 // Error: type mismatch 

字符串连接

显示在代码片段中就像Java一样Int追加到 String将导致String输出

var str = "abc" + 1 
 println(str + "def") 

输出:

abc1def 

即使没有显式地将Int值1显式转换为String对象,结果输出仍然是String

具有多行的字符串

程序员可以使用三引号而不是双引号来声明具有多行的String变量

var str = """ 
        This is line 1 
        This is line 2 
        This is line 3 
        """ 
 println(str) 

输出:

        This is line 1 
        This is line 2 
        This is line 3 

或者使用.trimIndent()

使用trimIndent()还可以通过消除每行上多余的和不必要的缩进来提供干净的输出格式。检查下面的代码段:

var str = """ 
        This is line 1 
        This is line 2 
        This is line 3 
        """.trimIndent() 
 println(str) 

输出:

This is line 1 
 This is line 2 
 This is line 3 

访问字符串的字符

索引访问

程序员可以使用索引访问运算符访问字符串的元素(字符):

var str = "Example" 
 println(str[2]) 

输出:

a 

就像从数组中访问元素一样,您得到:

var str = "Example" 
 println(str[9]) // Error: index out of bounds 

通过String迭代

字符串的元素是可以通过索引操作访问的字符: s[i]

var str = "Example" 
 for (c in str) { 
    println(c) 
 } 

输出:

E 
 x 
 a 
 m 
 p 
 l 
 e 

字符串的不变性

就像Java一样您无法更改String各个元素

var str = "Example" 
 str[2] = "b" // Error 

重新分配字符串值

var str = "Example" 
 println(str) 
 str = "Example was changed" 
 println(str) 

输出:

Example 
 Example was changed 

字符串属性

确定字符串的长度

var str = "Example" 
 println(str.length) 

输出:

7 

字符串函数

这些是当前Kotlin版本中可用的一些常见String函数

相比于

将此对象与指定的对象进行比较以获得顺序。如果此对象等于指定的其他对象,则返回零;如果小于其他对象,则返回负数;如果它大于其他对象,则返回正数。

var str = "Example" 
 var str2 = "Example123" 
 var str3 = "Example12345" 
 println(str.compareTo(str2)) 
 println(str.compareTo(str3)) 
 println(str3.compareTo(str)) 
 println(str.compareTo("Example")) 

输出:

-3 
 -5 
 5 
 0 # Equal 

等于

指示String对象是否与另一个String对象完全相同

var str = "Example" 
 var str2 = "Example2" 
 println(str.equals("Example")) 
 println(str2.equals("Example")) 

输出:

true 
 false 

得到

返回此字符序列中指定索引处的字符。

```kotlin var str =“示例” 的printlnstr.get3

Output: 

贝壳 米

### toString 
 
 Returns a string representation of the object. 

科特林 println9.toString+ 10 println9 + 10

Output: 

贝壳 “910” 19 ```

资源