What this "it <= ''" in the string line function means here

The presence of java code to trim the string

String title = titleEt.getText().toString().trim();

when the cover is for kotlin, I thought it should be kotlin code to crop the leading space and the ending space.

val title = titleEt.text.toString().trim()

but the IDE generates this code

val title = titleEt.text.toString().trim { it <= ' ' }

What is it {it <= ''} here, is there any char less than '??

+4
source share
4 answers

Java String#trim()removes all code points between '\u0000'(NUL) and '\u0020'(SPACE) from the beginning and end of a line.

Kotlin CharSequence.trim() ( Char.isWhitespace, Character#isWhitespace(char)). , Java, IDE , , Java.

ASCII, .

'\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' ␠ (' ')
+6

kotlin , java- ( ) , .trim { it <= ' ' } .trim() kotlin

+3

String.trim , predicate. , , . , ' ' ().

java.lang.String#trim.

+1

ASCII char, '' (). ASCII ASCII '' (), .

If you look at the ASCII table , the characters below the space (ASCII 32) are non-printable control characters. Thus, all printable characters, except for the space, are located above ASCII space (which is 32). Therefore, if characters whose ASCII are less than or equal to the value '' (space) are deleted, we get the remaining line without leading and trailing spaces.

This is how Java works trim(). However, you can just use trim()also in Kotlin:

titleEt.text.toString().trim()
0
source

Source: https://habr.com/ru/post/1683582/


All Articles