Bases des chaînes
Créez et combinez des chaînes.
Bases des chaînes est une leçon Scala for Backend Engineering & Functional Programming gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Scala for Backend Engineering & Functional Programming, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Scala for Backend Engineering & Functional Programming comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
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 \".
Questions Fréquemment Posées
La leçon « Bases des chaînes » est-elle gratuite ?
Oui — le texte complet de « Bases des chaînes » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Scala for Backend Engineering & Functional Programming, passe à CoddyKit PRO. Le cours Scala for Backend Engineering & Functional Programming comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Bases des chaînes » ?
Créez et combinez des chaînes. Tu pratiques Scala for Backend Engineering & Functional Programming avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Scala for Backend Engineering & Functional Programming ?
Aucune expérience préalable n'est requise. Scala for Backend Engineering & Functional Programming sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.
Combien de temps prend la leçon « Bases des chaînes » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Scala for Backend Engineering & Functional Programming ?
Oui. Chaque leçon Scala for Backend Engineering & Functional Programming inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.