Sling Academy
Home/Kotlin/Padding Strings with Characters in Kotlin

Padding Strings with Characters in Kotlin

Last updated: November 30, 2024

In Kotlin, string manipulation is a common task, and sometimes you may need to pad strings with specific characters to ensure they meet a certain length. Padding a string means adding characters to one or both sides of the string. Kotlin provides an easy and concise way to pad strings.

Using padStart and padEnd Functions

Kotlin has built-in functions padStart and padEnd to achieve string padding handling. These functions allow you to specify the desired length of the string after padding and the character to use for padding.

Example of padStart

padStart adds characters to the beginning of the string until the specified length is reached.


fun main() {
    val original = "Kotlin"
    val padded = original.padStart(10, '*')
    println("Original: '"+original+"'")
    println("Padded: '")+padded+"'")
}

This would output:


Original: 'Kotlin'
Padded: '****Kotlin'

Example of padEnd

padEnd adds characters to the end of the string until the specified length is reached.


fun main() {
    val original = "Kotlin"
    val padded = original.padEnd(10, '-')
    println("Original: '")+original+"'")
    println("Padded: '")+padded+"'")
}

This would output:


Original: 'Kotlin'
Padded: 'Kotlin----'

Working with Numbers and Strings

If you're dealing with numeric strings or ID numbers where you need consistent formatting, padding with zeros can be quite helpful.


fun main() {
    val number = "42"
    val paddedNumber = number.padStart(5, '0')
    println("Original Number: ")+number+"")
    println("Padded Number: ")+paddedNumber+"")
}

This would output:


Original Number: 42
Padded Number: 00042

String Padding in Practical Use

Padded strings can be quite useful in formatting tasks, such as displaying data in tabular formats, creating text-based UI components, or when working with fixed-width file formats.

Consider a situation where you have a list of employee names and want to align them to the right when printing:


fun main() {
    val names = listOf("Alice", "Bob", "Christina", "Daniel")
    names.forEach { name ->
        println(name.padStart(10))
    }
}

This would output:


     Alice
      Bob
Christina
   Daniel

As seen, padStart helps in aligning the names neatly when printed.

Conclusion

String padding is a simple yet powerful feature in Kotlin encoded in padStart and padEnd methods. It is particularly useful when working with data formatting and presentation. Mastering these functions can simplify many tasks related to text manipulation and enhance your applications' professionalism and readability.

Next Article: Inserting Substrings at Specific Indexes in Kotlin

Previous Article: Removing Duplicate Characters in Strings in Kotlin

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin