what is this "it <= ' '" in trim string function mean here
trim function in kotlin allows a predicate so trim in java code (removes the white spaces) is the same as .trim { it <= ' ' }
You can use .trim()
in kotlin too
It is comparing the ASCII of each char which ' ' (space). If the ASCII of the character is less than or equal to that of the ASCII of ' ' (space), it is removed.
If you look at the ASCII table, the symbols below space ( ASCII 32) are non-printable control characters. So all the printable characters other than space are above the ASCII of space (which is 32). So if the characters whose ASCII is lesser than or equal to that of ' ' (space) are removed, we will get the remaining String without leading and trailing whitespace.
This is how the Java's trim()
works. Nevertheless, you can just use trim()
also in Kotlin:
titleEt.text.toString().trim()
Java's String#trim()
removes all codepoints between '\u0000'
(NUL) and '\u0020'
(SPACE) from the start and end of the string.
Kotlin's CharSequence.trim()
removes only leading and trailing whitespace by default (characters matching Char.isWhitespace
, which is Character#isWhitespace(char)
). For the same behavior as Java, the IDE generated a predicate that matches the same characters that Java would have trimmed.
These characters include ASCII whitespace, but also include control characters.
'\u0000' ␀ ('\0')
'\u0001' ␁
'\u0002' ␂
'\u0003' ␃
'\u0004' ␄
'\u0005' ␅
'\u0006' ␆
'\u0007' ␇ ('\a')
'\u0008' ␈ ('\b')
'\u0009' ␉ ('\t')
'\u000A' ␊ ('\n')
'\u000B' ␋ ('\v')
'\u000C' ␌ ('\f')
'\u000D' ␍ ('\r')
'\u000E' ␎
'\u000F' ␏
'\u0010' ␐
'\u0011' ␑
'\u0012' ␒
'\u0013' ␓
'\u0014' ␔
'\u0015' ␕
'\u0016' ␖
'\u0017' ␗
'\u0018' ␘
'\u0019' ␙
'\u001A' ␚
'\u001B' ␛
'\u001C' ␜
'\u001D' ␝
'\u001E' ␞
'\u001F' ␟
'\u0020' ␠ (' ')