
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: November 25, 2024
In this tutorial, we’ll demonstrate how to remove the first n digits from a number in Scala using basic string manipulation techniques.
To begin, we need to ensure our number can be manipulated as a string. This step is essential, as we will be removing characters based on their positions.
Here’s a straightforward approach:
def removeFirstNDigits(num: Int, n: Int): Int = {
num.toString.drop(n).toInt
}
In this method, we convert the integer to a string and use the String.drop() method to remove the first n characters. However, we should consider what happens if n exceeds the length of the number.
To account for potential issues, we can modify our function to return 0 if n is greater than or equal to the length of the number:
def removeFirstNDigitsLimit(num: Int, n: Int): Int = {
val numString = num.toString
if (n >= numString.length)
0
else
numString.drop(n).toInt
}
This version checks the length of the string representation of the number and safely handles the scenario where n is too large.n
Let’s look at some examples to see how our function behaves:
removeFirstNDigitsLimit(123456, 2) shouldBe 3456
removeFirstNDigitsLimit(123456, 5) shouldBe 6
removeFirstNDigitsLimit(123456, 6) shouldBe 0
These examples illustrate how our function effectively removes the specified number of digits.
An alternative approach could involve pattern matching to enhance readability and expressiveness:
def removeFirstNDigitsPatternMatching(num: Int, n: Int): Int = {
val numString = num.toString
numString match {
case str if n >= str.length => 0
case str => str.drop(n).toInt
}
}
This method utilizes pattern matching to succinctly handle the edge case, making the code more elegant while retaining its functionality.
When dealing with negative numbers, we need to account for the presence of the negative sign. In our previous examples, the negative sign (-
) is part of the number but does not get removed when using string manipulation. This means that if we naively apply drop(n)
to a negative number, the negative sign will still be preserved in the result, potentially leading to incorrect behavior.
To handle negative numbers correctly, we can add a check to separate the sign from the magnitude of the number. Here’s how we can modify our function to deal with negative numbers:
def removeFirstNDigitsNegative(num: Int, n: Int): Int = {
val numString = num.toString
if (num < 0) {
// For negative numbers, remove the first n digits but keep the negative sign
val absNumString = numString.drop(1) // Drop the negative sign
if (n >= absNumString.length) 0
else ("-" + absNumString.drop(n)).toInt
} else {
// For positive numbers, apply the same logic as before
if (n >= numString.length) 0
else numString.drop(n).toInt
}
}
Let’s see an example with this function:
removeFirstNDigitsNegative(-123456, 2) shouldBe -3456
In this article, we learned how to remove the first n digits from a number in Scala.
We explored different approaches, starting from a basic implementation to more robust solutions that handle edge cases. By using string manipulation and pattern matching, we effectively solved the problem while keeping the code clean and efficient.