How to extract separate capitals and numbers from String - kotlin

I have 2 strings 67 Paris St. Abbeville and 344 Paris Main Hgw and I need to extract separately numbers so Ill have something var onlyNumbers = "67,344" and separate capitals like var capitals = "Paris St. Abbeville,Paris Main Hgw"
How to filter capitals separate from numbers in those strings?

I suppose you have this
val address1 = "67 Paris St. Abbeville"
val address2 = "344 Paris Main Hgw"
Simple version
If you want a string with only numbers and one without, you can use :
val address1Digits = address1.filter(Char::isDigit) // Variant 1
val address2Digits = address2.filter { it.isDigit() } // Variant 2
val address1WithoutDigits = address1.filterNot(Char::isDigit).trim() // Variant 1
val address2WithoutDigits = address2.filterNot { it.isDigit() }.trim() // Variant 2
Then you can join the result in a comma-separated string :
val address1Digits = address1.filter(Char::isDigit) // Variant 1
val address2Digits = address2.filter { it.isDigit() } // Variant 2
val onlyNumbers = "$address1Digits,$address2Digits" // "67,344"
val capitals = "$address1WithoutDigits,$address2WithoutDigits" // "Paris St. Abbeville,Paris Main Hgw"
"Complex" version
This will do "123abc456" -> "123456" and "abc"
But if what you want an array with all the different numbers (for example "123abc456" -> ["123", "456"]) you can use :
val address1Digits = address1.split("\\D+".toRegex()).filter { it.isNotEmpty() }
val address2Digits = address2.split("\\D+".toRegex()).filter { it.isNotEmpty() }
val address1WithoutDigits = address1.split("[^\\D]+".toRegex()).map(String::trim).filter { it.isNotEmpty() } // Variant 1
val address2WithoutDigits = address1.split("[^\\D]+".toRegex()).map { it.trim() }.filter { it.isNotEmpty() } // Variant 2
Again you can join results
val onlyNumbers = (address1Digits+address2Digits).joinToString(",") // "67,344"
val capitals = (address1WithoutDigits+address2WithoutDigits).joinToString(",") // "Paris St. Abbeville,Paris Main Hgw"

Related

How do I initialize a nxm List<List<String>> matrix in Kotlin

new bee in Kotlin here, apologize for the simple question but how do I initialize a matrix of strings? I need this:
val board: List<List<String>>
I looked at this sample for integers and did the following:
val row = 4
val col = 3
var matrix: Array<IntArray> = Array(row) { IntArray(col) }
Then I tried to replace Int by String but it won't build:
val board: List<List<String>> = Array(row) { StringArray(col) }
thank you.
If you're trying to initialize a 2D String array you can do it like this:
fun main() {
val height = 5
val width = 5
val stringArray = Array(height) { Array(width) {""} }
}
no need to make board of type List<List<String>>.
To test the code, we can initialize the array instead with any character and print it out:
fun main() {
val height = 5
val width = 5
val stringArray = Array(height) { Array(width) {"a"} }
for (i in stringArray) {
for (j in i) {
print(j)
}
println()
}
}
which results in:
aaaaa
aaaaa
aaaaa
aaaaa
aaaaa

Remove character or word from string in kotlin

Hey I want to remove character/word or sentence from string.
For example
val string = "Hey 123"
or
val string = "Hey How are you 123"
or
val string = "Hey!! How are you 123"
and output
string = 123
If you only want the digits:
val result = string.filter { it.isDigit() }
Alternatively if you want to omit letters (and maybe also whitespace):
val result = string.filter { !it.isLetter() }
val result = string.filter { !it.isLetter() && !it.isWhitespace() }

Setter not assigning value in Kotlin

I am trying to do a temperature program, which outputs the lowest temperature from three cities provided. If the temperature of one of three cities is above + 57 or below -92 all three cities will have set default values which are (+5 Moscow, +20 Hanoi , 30 for Dubai)
However providing those numbers 20,100,35 in readLine doesn't work.
This is how City class looks like:
class City(val name: String) {
var degrees: Int = 0
set(value) {
field =
if (value > 57 || -92 > value) {
when (this.name) {
"Dubai" -> 30
"Moscow" -> 5
"Hanoi" -> 20
else -> 0
}
} else {
value
}
}}
And in my main I have:
val first = readLine()!!.toInt()
val second = readLine()!!.toInt()
val third = readLine()!!.toInt()
val firstCity = City("Dubai")
val secondCity = City("Moscow")
val thirdCity = City("Hanoi")
firstCity.degrees = first
secondCity.degrees = second
thirdCity.degrees = third
println(first)
println(second)
println(third)
What's wrong in the setter? Why does the second doesn't set the default values?
Works as expected to me https://pl.kotl.in/otINdg8E3:
class City(val name: String) {
var degrees: Int = 0
set(value) {
field =
if (value > 57 || -92 > value) {
when (this.name) {
"Dubai" -> 30
"Moscow" -> 5
"Hanoi" -> 20
else -> 0
}
} else {
value
}
}}
fun main() {
val firstCity = City("Dubai")
val secondCity = City("Moscow")
val thirdCity = City("Hanoi")
firstCity.degrees = 100
secondCity.degrees = -100
thirdCity.degrees = 6
println(firstCity.degrees) // prints 30
println(secondCity.degrees) // prints 5
println(thirdCity.degrees) // prints 6
}

Different Property setter for three same class objects in Kotlin

What I am trying to implement are three different temperature values depending on the city name.
The following class:
class City(val name: String) {
var degrees: Int = 0
set(value) {
when(this.name){
("Dubai") -> 30
"Moscow" -> 5
"Hanoi" -> 20
}
field = value
}}
And main func:
fun main() {
val firstCity = City("Dubai")
val secondCity = City("Moscow")
val thirdCity = City("Hanoi")
println(firstCity.degrees) // 0
}
Why is it set to default value 0? For Dubai it should have been 30.
The degrees are initialized with 0 and never changed due to no invocation of the setter, which lacks a value for cities that are not expected (maybe that's why you initialized the degrees?).
You could do what you want way shorter:
class City(val name: String) {
var degrees: Int = when(name) {
"Dubai" -> 30
"Moscow" -> 5
"Hanoi" -> 20
else -> 0 // default value for unpredictable cities
}
}
fun main() {
val firstCity = City("Dubai")
val secondCity = City("Moscow")
val thirdCity = City("Hanoi")
println(firstCity.degrees)
}
This will output 30

type-safe builder example kotlin

I want to have following person object in Kotlin :
var p = person {
age = 22
gender = "male"
name {
first = "Ali"
last = "Rezaei"
}
}
I have following code to build it :
data class Person(var age: Int? = null, var gender: String? = null
, var name : Name? = null) {
}
fun name(init: Name.() -> Unit): Name {
val n = Name()
n.init()
return n
}
data class Name(var first: String? = null, var last : String? = null)
fun person(init: Person.() -> Unit): Person {
val p = Person()
p.init()
return p
}
But when I print it, the result is following :
Person(age=22, gender="male", name=null)
What is wrong with my code?
You could make name an extension function on Person that assigns the Name to the Person instead of returning it:
fun Person.name(init: Name.() -> Unit) {
val n = Name()
n.init()
this.name = n
}
You could even consider a more concise syntax for the same, like this:
fun Person.name(init: Name.() -> Unit) {
this.name = Name().apply(init)
}
Shameless plug for my repository discussing DSL design and containing examples.
You need to assign to name. This ended up working for me...
var p = person {
age = 22
gender = "male"
name = name {
first = "Ali"
last = "Rezaei"
}
}