fun main(args: Array<String>) {
var _array = arrayOf(1 , 2 , 3.14 , 'A', "item" , "a b c d", 4)
println("$_array[3]") // [Ljava.lang.Object;#1b6d3586[3]
println("${_array[3]}") // A
println(_array[3]) // A
println( _array[3] + " is _array's item") // ERROR
println( "" + _array[3] + " is _array's item") // A is _array's item
}
I am confused why the code above makes different output
println("$_array[3]") // [Ljava.lang.Object;#1b6d3586[3]
prints the _array object reference followed by [3], you use string interpolation only for the _array argument
println("${_array[3]}") // A
prints the 4th element of _array, you use string interpolation for the _array[3] argument
println(_array[3]) // A
prints the 4th element of _array (same as above)
println( _array[3].toString() + " is _array's item") // ERROR
it needs toString() to get printed because the elements of _array are of type Any and the + sign after it is inconclusive
it prints the 4th element of _array
println( "" + _array[3] + " is _array's item") // A is _array's item
it does not need toString() as it is preceded by an empty string and the + sign is interpreted by the compiler as string concatenation so it prints the 4th element of _array
When you use complex expression in string template, you have to wrap it inside curly braces. But that is optional for single variable.
So, this line
println("$_array[3]")
means same thing as
println(_array.toString() + "[3]")
Let's break it one by one:
println("$_array[3]")
Same as println("${_array}[3]") - [3] is just a string here, not interpolated
println("${_array[3]}")
Entire _array[3] is interpolated
println(_array[3])
Same as println(_array[3].toString())
println( _array[3] + " is _array's item") // ERROR
Your array is Array<Any>. plus() is not defined for (Any,String)
println( "" + _array[3] + " is _array's item") // A is _array's item
plus() is defined for pair (String,Any), and it returns a string
Related
validValueType.ValueTypeGroup
["\"is_enabled\": false", "\"value\":\"OUT\""]
failedRecord.record
{"email":"test#gmail.com","source":"web","value":"OUT","reate_date":"2022-09-29T03:42:09.976-05:00","is_undeliverable":false}
fun publishAlert(failedRecord: Record<String>) {
if (validValueType.ValueTypeGroup.contains(failedRecord.record)) {
// do stuff
} else {
// no match do other stuff
}
}
In the list above there are two strings I want to check for when this function receives a record.
The failedRecord.record string does contain what I want "value":"OUT" and it's also within the list above. So why is contains not working here? it keeps bouncing out to the else statement.
You can use any() in the list and pass a predicate:
{x -> searchString.contains(x)}
The searchString.contains() will search x as a substring inside searchString for each x representing the elements in the list
var list = listOf("\"is_enabled\": false", "\"value\":\"OUT\"")
println(list)
println(list::class.qualifiedName)
println() // empty newline
var searchString = "{\"email\":\"test#gmail.com\",\"source\":\"web\",\"value\":\"OUT\",\"create_date\":\"2022-09-29T03:42:09.976-05:00\",\"is_undeliverable\":false}";
println(searchString)
println(searchString::class.qualifiedName)
println() // empty newline
println(list.any{x -> searchString.contains(x)});
Output
["is_enabled": false, "value":"OUT"]
java.util.Arrays.ArrayList
{"email":"test#gmail.com","source":"web","value":"OUT","create_date":"2022-09-29T03:42:09.976-05:00","is_undeliverable":false}
kotlin.String
true
I rather have this ugly way of building a string from a list as:
val input = listOf("[A,B]", "[C,D]")
val builder = StringBuilder()
builder.append("Serialized('IDs((")
for (pt in input) {
builder.append(pt[0] + " " + pt[1])
builder.append(", ")
}
builder.append("))')")
The problem is that it adds a comma after the last element and if I want to avoid that I need to add another if check in the loop for the last element.
I wonder if there is a more concise way of doing this in kotlin?
EDIT
End result should be something like:
Serialized('IDs((A B,C D))')
In Kotlin you can use joinToString for this kind of use case (it deals with inserting the separator only between elements).
It is very versatile because it allows to specify a transform function for each element (in addition to the more classic separator, prefix, postfix). This makes it equivalent to mapping all elements to strings and then joining them together, but in one single call.
If input really is a List<List<String>> like you mention in the title and you assume in your loop, you can use:
input.joinToString(
prefix = "Serialized('IDs((",
postfix = "))')",
separator = ", ",
) { (x, y) -> "$x $y" }
Note that the syntax with (x, y) is a destructuring syntax that automatically gets the first and second element of the lists inside your list (parentheses are important).
If your input is in fact a List<String> as in listOf("[A,B]", "[C,D]") that you wrote at the top of your code, you can instead use:
input.joinToString(
prefix = "Serialized('IDs((",
postfix = "))')",
separator = ", ",
) { it.removeSurrounding("[", "]").replace(",", " ") }
val input = listOf("[A,B]", "[C,D]")
val result =
"Serialized('IDs((" +
input.joinToString(",") { it.removeSurrounding("[", "]").replace(",", " ") } +
"))')"
println(result) // Output: Serialized('IDs((A B,C D))')
Kotlin provides an extension function [joinToString][1] (in Iterable) for this type of purpose.
input.joinToString(",", "Serialized('IDs((", "))')")
This will correctly add the separator.
The problem:
Consider the following code:
val key: String? = "key"
val value: String? = "label"
val row = key + ": " + value
I would like the variable row to be null if any of the supplied inputs in the concatenation is null.
By default, any null String will be converted to "null" and the concatenation will proceed. In example:
val value = null
"Height: " + value + "mm" // Produces: "Height: nullmm"
I can skip the showing "null" in the results by using value ?: "", but this solves only a part of my problem:
val value = null
"Height: " + (value ?: "") + "mm" // Produces: "Height: mm"
My best solution so far:
I understand that writing a simple function like the one below would do the job, but I still expect that something like this already exists in the language:
fun Array<String?>.nullSafeConcat(): String? {
val result = StringBuilder()
this.forEach {
if(it == null) return null
result.append(it)
}
return result.toString()
}
The ask:
Is there a better way to do this?
Post Scriptum:
I cannot understand why would a null string be converted to "null" by default, as I cannot find any use case where this would be actually usable.
I think matt freake's answer is correct for the question, but I would avoid overriding the + operator for Strings - it can cause tons of unexpected issues.
Instead, I suggest you to slightly modify your nullSafeConcat helper function to be a standalone function that takes vararg instead of being an extension function. Something like this:
fun nullSafeConcat(vararg strings: String?): String? {
val result = StringBuilder()
strings.forEach {
if(it == null) return null
result.append(it)
}
return result.toString()
}
Then you can use it like:
val merged = nullSafeConcat("foo", null, "bar", "baz")
Notes:
You might want to handle the empty case (when varargs argument strings is empty) specifically, depending on the outcome you want.
Additionally, if you want this to work for at least 2 strings (so a concatenation is actually meaningful), you can use a signature like nullSafeConcat(first: String?, second: String?, vararg rest: String?) instead.
I'm not sure if this solves the problem, you can override the + operator on a nullable String to get close to what you want. For example:
private operator fun String?.plus(otherString: String?): String? = if (this==null || otherString ==null ) "null" else this + otherString
Then:
fun main() {
val s1: String? = "Hello"
val s2: String? = null
val s3: String? = "Bye"
println(s1 + s2)
println(s2 + s1)
println(s1 + s3)
}
prints:
null
null
HelloBye
The problem is it will only work with variables of String? not String which your ":" value is. So you'd need to do something like:
s1 + colon + s2
where colon was also of type String?
EDIT:
There are two dangers with this approach. Firstly If you don't make it private (or even if you do) there is a risk that existing or new code tries to append two String? values and gets your new behaviour, which they don't expect. Secondly, someone reading the code where you call it may be surprised by the behaviour if they don't spot that you've overridden the + operator.
How about this
listOfNotNull("foo", null, "bar", "baz").joinToString()
In kotlin, how to check if the input is alphabetic only.
Input could be anything, a String, Int or Double etc.
For example
val input = readLine()
if(check) {
doSomeTask
}
else doSomethingElse
You can have a look here, there are a lot of examples.
for example you can check via
fun isLetters(string: String): Boolean {
return string.all { it.isLetter() }
}
A good answer for checking if a String is entirely alphabetical was given by #HakobHakobyan: String.all { it.isLetter() }.
I will borrow his solution to target a second aspect of your question, that is
Input could be anything, a string, int or double etc.
Here's another method that checks Any input type:
fun isAplhabetical(input: Any): Boolean {
when (input) {
// if the input is a String, check all the Chars of it
is String -> return input.all { it.isLetter() }
// if input is a Char, just check that single Char
is Char -> return input.isLetter()
// otherwise, input doesn't contain any Char
else -> return false
}
}
and it can be used in an example main() like this:
fun main() {
val a = "Some non-numerical input"
val b = "45"
val c = "Some numbers, like 1, 2, 3, 4 and so on"
val d: Int = 42
val e: Double = 42.42
val f: Float = 43.4333f
val g = "This appears as entirely alphabetical" // but contains whitespaces
val h = "ThisIsEntirelyAlphabetical"
println("[$a] is" + (if (isAplhabetical(a)) "" else " not") + " (entirely) alphabetical")
println("[$b] is" + (if (isAplhabetical(b)) "" else " not") + " (entirely) alphabetical")
println("[$c] is" + (if (isAplhabetical(c)) "" else " not") + " (entirely) alphabetical")
println("[$d] is" + (if (isAplhabetical(d)) "" else " not") + " (entirely) alphabetical")
println("[$e] is" + (if (isAplhabetical(e)) "" else " not") + " (entirely) alphabetical")
println("[$f] is" + (if (isAplhabetical(f)) "" else " not") + " (entirely) alphabetical")
println("[$g] is" + (if (isAplhabetical(g)) "" else " not") + " (entirely) alphabetical")
println("[$h] is" + (if (isAplhabetical(h)) "" else " not") + " (entirely) alphabetical")
}
The output is
[Some non-numerical input] is not (entirely) alphabetical
[45] is not (entirely) alphabetical
[Some numbers, like 1, 2, 3, 4 and so on] is not (entirely) alphabetical
[42] is not (entirely) alphabetical
[42.42] is not (entirely) alphabetical
[43.4333] is not (entirely) alphabetical
[This appears as entirely alphabetical] is not (entirely) alphabetical
[ThisIsEntirelyAlphabetical] is (entirely) alphabetical
Only the last String is entirely alphabetical.
You can use a regex with the alphabet range:
fun alphabetCheck(input: String): Boolean {
val regex = Regex("[a-zA-Z]+?")
return regex.matches(input)
}
First convert your input to string by using toString():
val str = input.toString()
val matchesAlphabet = alphabetCheck(str)
You can check the ascii value of a character as in the example:
fun main(args: Array) {
val c = 'a'
val ascii = c.toInt()
println("The ASCII value of $c is: $ascii")
}
If you look at the ascii table, you can see that alphabetic characters are the one between the values 65 and 90 for capital letters. For small letters you have the interval 97 - 122.
If you want to build an arbitrary lookup (say characters that fit an encoding like base 64) you can do this kind of thing too:
val acceptable = ('a'..'z').plus('A'..'Z').plus("+-/~".asIterable())
So that's using ranges as a quick way of defining a... range of characters, and using a string to easily specify some individual ones (and turning it into an Iterable<Char> so plus can add them to the list.
val Char.isAcceptable get() = this in acceptable
"ab+5%".filter(Char::isAcceptable).let { print("VIPs: $it")}
>>>> VIPs: ab+
I have to validate an error message getting populated for 10 fields, 10 field names will be mentioned in the error message together.
Example :
Error message:10 fields got over lapped and 10 field names are ac, bc, dc..
Can you please help me to write the logic to validate the error message having all random strings.
Need to pass all strings values in to the creates string to compare the error message.
Map<String,String> m = arg1.asMap(String.class,String.class);
System.out.println("\nFilling form with below data\n");
for( String fieldname : m.keySet())
{
//Need to pass this field name to the error message and the error message may have multiple fieldnames//
System.out.println("Key -> " + fieldnames + " Value -> " + m.get(fieldnames));
}
}
You can get key and values as following and here is a good answer for how to use Map and EntrySet :
Map<String,String> map = arg1.asMap(String.class,String.class);
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
OUTPUT :
key1=value1
key2=value2
.
.
.
As an answer of your question for how to assert them :
List<String> fieldNames = new ArrayList<String>();
Map<String,String> map = arg1.asMap(String.class,String.class);
int i = 0;
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
fieldName.add(entry.getKey());
i++;
}
Assert.assertEquals(errorMessage, "Error message:" + i + "fields got over lapped and" + i + "field names are" + fieldName.toString() );