-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1551 from yadavan88/collapse-spaces
Collapse multiple spaces in a string
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...s-2/src/main/scala/com/baeldung/scala/strings/removemultispace/RemoveMultipleSpaces.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.baeldung.scala.strings.removemultispace | ||
|
||
object RemoveMultipleSpaces { | ||
def usingReplaceAll(str: String): String = { | ||
str.trim.replaceAll("\\s+", " ") | ||
} | ||
|
||
def usingSplit(str: String): String = { | ||
str.trim.split("\\s+").mkString(" ") | ||
} | ||
|
||
def usingZip(str: String): String = { | ||
if (str.trim.isEmpty) { | ||
str.trim | ||
} else { | ||
val zipped = str.trim.zip(str.trim.tail) | ||
str.trim.head + zipped.collect { | ||
case (a, b) if !(a == ' ' && b == ' ') => b | ||
}.mkString | ||
} | ||
} | ||
|
||
def usingStringBuilder(str: String): String = { | ||
val sb = new StringBuilder | ||
var lastCharWasSpace = false | ||
|
||
for (c <- str.trim) { | ||
if (c != ' ' || !lastCharWasSpace) sb.append(c) | ||
lastCharWasSpace = c == ' ' | ||
} | ||
sb.toString | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...test/scala/com/baeldung/scala/strings/removemultispace/RemoveMultipleSpacesUnitTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.baeldung.scala.strings.removemultispace | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.prop.TableDrivenPropertyChecks | ||
|
||
class RemoveMultipleSpacesUnitTest | ||
extends AnyFlatSpec | ||
with Matchers | ||
with TableDrivenPropertyChecks { | ||
|
||
private val table = Table( | ||
("input string", "expected"), | ||
(" too many spaces ", "too many spaces"), | ||
(" ", ""), | ||
("a", "a"), | ||
("a ", "a"), | ||
("", "") | ||
) | ||
|
||
private val functions = Seq( | ||
("usingReplaceAll", RemoveMultipleSpaces.usingReplaceAll), | ||
("usingSplit", RemoveMultipleSpaces.usingSplit), | ||
("usingZip", RemoveMultipleSpaces.usingZip), | ||
("usingStringBuilder", RemoveMultipleSpaces.usingStringBuilder) | ||
) | ||
it should "remove multiple spaces with a single space in the string" in { | ||
forAll(table) { (input, expected) => | ||
functions.map { (name, fn) => | ||
withClue( | ||
s"Failed for the input string ${input} in the function ${name}" | ||
) { | ||
fn(input) shouldBe expected | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |