字符串基础
创建并组合字符串。
字符串基础 是 CoddyKit 上的免费 Scala for Backend Engineering & Functional Programming 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Scala for Backend Engineering & Functional Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Scala for Backend Engineering & Functional Programming 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is a String?
A String in Scala is a sequence of characters, like text you read or type.
You write a literal string by wrapping text in double quotes. Strings are one of the most common types you will work with in backend code.
val greeting = "Hello, Scala!"
println(greeting)Declaring String Values
Use val for a string that never changes, and var if you need to reassign it.
Scala infers the type String automatically, but you can also write it explicitly.
val name: String = "Ada"
var mood = "happy"
mood = "excited"
println(name)
println(mood)A Runnable Greeting
Let's put a string inside a complete program. An object extending App runs the code in its body.
This prints a friendly message to the console.
object Main extends App {
val message = "Welcome to Scala strings!"
println(message)
}Strings Are Immutable
A Scala String is immutable: once created, its characters never change.
Methods that seem to modify a string actually return a brand new string, leaving the original untouched.
val s = "abc"
val upper = s.toUpperCase
println(s)
println(upper)String Length
The length method tells you how many characters a string contains.
Spaces and punctuation count too, since they are characters.
val word = "backend"
println(word.length)
val phrase = "a b"
println(phrase.length)Joining Strings with +
You can join, or concatenate, strings using the + operator.
This is handy for building messages from several pieces.
val first = "Func"
val second = "tional"
val result = first + second
println(result)Mixing Strings and Numbers
When you use + between a string and a number, Scala converts the number to text and joins them.
This makes it easy to build readable output.
val count = 3
val line = "You have " + count + " tasks"
println(line)Accessing a Character
Strings are indexed starting at 0. Use parentheses with an index to get a single Char.
The first character is at index 0, the second at index 1, and so on.
val s = "Scala"
println(s(0))
println(s(1))
println(s(4))Comparing Strings
Use == to check if two strings have the same characters. In Scala this compares values, not memory addresses.
Comparison is case sensitive, so "Hi" is not equal to "hi".
println("hello" == "hello")
println("Hi" == "hi")Empty and Blank Strings
An empty string "" has length 0. A string of only spaces is not empty, but it is blank.
Use isEmpty to test for an empty string.
val empty = ""
val spaces = " "
println(empty.isEmpty)
println(spaces.isEmpty)
println(spaces.length)Escape Characters
Some characters need an escape with a backslash. Use \n for a new line, \t for a tab, and \" for a literal quote.
These let you include special symbols inside a normal string.
val text = "Line one\nLine two"
println(text)
val quote = "She said \"hi\""
println(quote)Quick Check
Test your understanding of basic Scala strings.
Recap
You learned that Scala strings are immutable sequences of characters created with double quotes.
You can find their length, join them with +, access characters by index from 0, compare them with ==, and use escapes like \n and \".
常见问题解答
「字符串基础」课时是免费的吗?
是的 — 「字符串基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Scala for Backend Engineering & Functional Programming 课程的其余内容,请升级到 CoddyKit PRO。 Scala for Backend Engineering & Functional Programming 课程共包含 4 节课。
「字符串基础」这节课中我会学到什么?
创建并组合字符串。 你通过在浏览器中直接运行的动手代码来练习 Scala for Backend Engineering & Functional Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Scala for Backend Engineering & Functional Programming 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Scala for Backend Engineering & Functional Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「字符串基础」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Scala for Backend Engineering & Functional Programming 课中编写并运行代码吗?
能。每节 Scala for Backend Engineering & Functional Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。