0Pricing
Scala for Backend Engineering & Functional Programming · درس

مُدرِجات s وf وraw

ضمّن القيم في السلاسل النصية

مُدرِجات s وf وraw درس مجاني في Scala for Backend Engineering & Functional Programming على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Scala for Backend Engineering & Functional Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What Is Interpolation?

String interpolation lets you embed values directly inside a string literal.

Instead of joining pieces with +, you put a prefix letter before the quotes and reference variables inside. Scala has three built-in interpolators: s, f, and raw.

val name = "Ada"
println(s"Hello, $name!")

The s Interpolator

The s interpolator is the most common. Put s before the opening quote, then use $variable to insert a value.

It reads more clearly than concatenation with +.

val count = 3
println(s"You have $count messages")

Expressions with Braces

For anything more than a simple variable, wrap the expression in ${ }.

This lets you do arithmetic or call methods right inside the string.

val a = 4
val b = 5
println(s"Sum is ${a + b}")
println(s"Upper: ${"hi".toUpperCase}")

A Runnable s Example

Here is a complete program using the s interpolator to build a greeting.

Notice how the variables are inserted neatly into the sentence.

object Main extends App {
  val user = "Sam"
  val items = 2
  println(s"Hi $user, you bought $items items.")
}

Escaping a Dollar Sign

Inside an interpolated string, $ has special meaning. To print a literal dollar sign, write $$.

This is useful when formatting prices or currency.

val price = 9
println(s"Total: $$$price")

The f Interpolator

The f interpolator adds printf-style formatting. After a variable, add a format specifier like %d for integers or %s for strings.

It is great for controlling number precision and alignment.

val n = 42
println(f"Value: $n%d")
val label = "Pi"
println(f"$label%s")

Formatting Decimals

Use %f with a precision to control decimal places. For example %.2f shows two digits after the point.

This is ideal for money or measurements.

val pi = 3.14159
println(f"Pi is $pi%.2f")
val ratio = 0.5
println(f"Ratio: $ratio%.1f")

Width and Padding with f

The f interpolator can also pad numbers to a width. %5d right-aligns an integer in a field five characters wide.

This helps line up columns of output.

val x = 7
println(f"[$x%5d]")
println(f"[$x%-5d]")

The raw Interpolator

The raw interpolator works like s but does not process escape sequences.

So \n stays as the two characters backslash and n, instead of becoming a new line.

println(raw"Path: C:\new\test")
println(s"Path: C:\new\test")

Choosing the Right One

Use s for everyday value insertion. Use f when you need number formatting. Use raw when you want backslashes kept literally, like file paths or regex.

All three still support $variable and ${expression}.

val v = 1.0
println(s"s: $v")
println(f"f: $v%.2f")
println(raw"raw: $v\n")

Multiple Values Together

You can combine many variables and expressions in one interpolated string.

This keeps output code compact and easy to read compared to long chains of +.

object Main extends App {
  val item = "Coffee"
  val qty = 2
  val cost = 4.5
  println(f"$qty x $item = $$${qty * cost}%.2f")
}

Quick Check

Which interpolator keeps escape sequences literal?

Recap

You learned Scala's three interpolators. s inserts values with $var and ${expr}.

f adds printf-style formatting like %.2f and %5d. raw behaves like s but keeps escape sequences literal. Use $$ to print a real dollar sign.

الأسئلة الشائعة

هل درس «مُدرِجات s وf وraw» مجاني؟

نعم — نص درس «مُدرِجات s وf وraw» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.

ماذا ستتعلم في «مُدرِجات s وf وraw»؟

ضمّن القيم في السلاسل النصية تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟

لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «مُدرِجات s وf وraw»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟

نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أساسيات String
  2. مُدرِجات s وf وraw
  3. أساليب String الشائعة
  4. السلاسل متعددة الأسطر
← العودة إلى Scala for Backend Engineering & Functional Programming