NIM string to seq[string] - nim-lang

Can anyone please assist I'm lost?
I have a string converted to string that I need to convert to seq[string]
simple example given below:
vat string1 = "this is a string"
var cmd: seq[string]
How do I get the string1 to be converted or cast into my seq[string] format e.g.
#["this", "is", "a", "string"]

import strutils
var string1 = "this is a string"
echo string1.split(' ')
this display
#["this", "is", "a", "string"]

Related

Kotlin Regex to Extract after and before a specific character

In Kotlin, i need to find the string value after and before of specific character,
For example,
Item1. myResultValue: someother string
So in the above, I need to get the value after ". " and before ":" SO my output should be "myResultValue". If it doesn't match it can return empty or null value.
Please help to write to Regex for this.
For your specific case you can use regex:
val text = "Item1. myResultValue: someother string"
val pattern = Pattern.compile(".*\\.(.*):.*")
val matcher = pattern.matcher(text)
if (matcher.matches()) {
println(matcher.group(1))
}
If you want to build something smarter that defines the start and end pattern you can do it like this:
val startSearch = "\\."
val endSearch = ":"
val text = "Item1. myResultValue: someother string"
val pattern = Pattern.compile(".*$startSearch(.*)$endSearch.*")
val matcher = pattern.matcher(text)
if (matcher.matches()) {
println(matcher.group(1))
}

Can you use a custom string interpolation character in Scala?

Scala's string interpolation is pretty straight forward to use:
val user = "Bob"
val message = s"Hello, $user"
//Hello, Bob
For string blocks containing lots of financial data, these normally need double escaping, however (which for large blocks, such as test data, might get long):
val user = "Mary"
val message = s"Balance owing for $user is $100"
//error: invalid string interpolation $1, expected: $$, $identifier or ${expression}
//error: unclosed string literal
val message = s"Balance owing for $user is $$100"
//Balance owing for Mary is $100
Is it possible to use a different interpolation character to avoid the double escape?
val characterA = "Randolph"
val characterB = "Mortimer"
val family = "Duke"
val film = "Trading Places"
val outcome = "wagered"
val message = at"Your property was #{outcome} for $1 by brothers #{characterA} and #{characterB} #{family}"
//Your property was wagered for $1 by brothers Randolph and Mortimer Duke
Is it possible to use a different interpolation character to avoid the double escape?
$ is part of Scala syntax, however it is at least possible to define custom string interpolation along the lines
scala> implicit class DollarSignInterpolation(val sc: StringContext) {
| def usd(args: Any*): String =
| sc.s(args:_*).replace("USD", "$")
| }
class DollarSignInterpolation
scala> val user = "Mary"
val user: String = Mary
scala> usd"""Balance owing for $user is USD100"""
val res0: String = Balance owing for Mary is $100
You can always use the format method:
val message = "Your property was %s for 1$ by brothers %s and %s %s"
println(message.format("actual outcome", "actual characterA", "actual characterB", "actual family"))

How do I split one string into two strings in Kotlin with "."?

Let's say I have this string:
val mainString: String = "AAA.BBB"
And now I define two children strings:
val firstString: String = ""
val secondString: String = ""
What code should I write to make firstString equals to "AAA", and secondString equals to "BBB"?
The below code works for any amount of strings separated by delimiters
val texto = "111.222.333"
val vet = texto.split(".")
for (st in vet) println(st)
It prints
111
222
333

How do I remove a substring/character from a string in Scala?

I am writing a program in which I need to filter a string. So I have a map of characters, and I want the string to filter out all characters that are not in the map. Is there a way for me to do this?
Let's say we have the string and map:
str = "ABCDABCDABCDABCDABCD"
Map('A' -> "A", 'D' -> "D")
Then I want the string to be filtered down to:
str = "BCBCBCBCBC"
Also, if I find a given substring in the string, is there a way I can replace that with a different substring?
So for example, if we have the string:
"The number ten is even"
Could we replace that with:
"The number 10 is even"
To filter the String with the map is just a filter command:
val str = "ABCDABCDABCDABCDABCD"
val m = Map('A' -> "A", 'D' -> "D")
str.filterNot(elem => m.contains(elem))
A more functional alternative as recommended in comments
str.filterNot(m.contains)
Output
scala> str.filterNot(elem => m.contains(elem))
res3: String = BCBCBCBCBC
To replace elements in the String:
string.replace("ten", "10")
Output
scala> val s = "The number ten is even"
s: String = The number ten is even
scala> s.replace("ten", "10")
res4: String = The number 10 is even

How to iterate over a list of Strings and concatenate them in Kotlin?

I have a list of strings such as:
listOf("1", "2", "3", "4", "+", "3")
And I want to concatenate so that I only get the numbers: "1234". I first attempted using a for loop which worked.
However I was wondering if Kotlin had a way of one lining the whole thing using a nice one line like:
val myList = listOf("1", "2", "3", "4", "+", "3")
someConcatenationFunction(myList) // returns "1234"
The solution I have found is this (to be put like in a separate file):
fun List<String>.concat() = this.joinToString("") { it }.takeWhile { it.isDigit() }
So basically, what it does is:
joinToString("") : JoinToString joins the content of a list to a String, the "" specifies that you don't want any separators in the concatenated String.
{ it }.takeWhile { it.isDigit() } : means that from the concatenated list, I only want the Chars that are digits. takeWhile will stop at first non digit.
And here you go! Now you can simply do:
listOf("1", "2", "3", "4", "+", "3").concat() // returns "1234"
Just using function joinToString() conact all the list items applicable for both Char and String.
below is the eg.
val list = listOf("1", "2", "3", "4", "+", "5")
val separator = ","
val string = list.joinToString(separator)
println(string) //output: 1,2,3,4,+,5
Other eg.
val list = listOf("My", "Name", "is", "Alise")
val separator = " "
val string = list.joinToString(separator)
println(string) //outeput: My Name is Alisse
You don't clearly said so but I assume you want to concat integers as long as possible (I.e. stop once an invalid string is encountered. The most straighforward way to do this:
val data = listOf("1", "2", "3", "4", "+", "3")
val concat = data.takeWhile { it.toIntOrNull() != null }.joinToString("")

Resources