当前位置:网站首页>[kotlin] the third day
[kotlin] the third day
2022-07-04 23:22:00 【No. 101 of the top 100 most handsome faces in the Asia Pacific 】
Here is the reference
️ Author: intellectuals
️ Personal blog : Old nine CSDN Blog
Personal famous saying : Uncontrollable things Optimistic face
Series column :
Here is the reference
List of articles
- Nullability characteristics (?)
- Security call operators
- Use let Security call for
- Non empty assertion operators (!!)
- Use if Judge null value
- Null merge operator
- Exception handling and custom exception
- Prerequisite function
- substring function
- split operation
- replace Complete encryption and decryption operation
- == and === Comparison operation
Nullability characteristics (?)
fun main() {
var name:String = "lmp"
// This default is a non nullable type
//name = null
println(name)
// When making a statement : Specify as nullable type ( Type followed by a question mark )
var name2 : String ?
name2 = null
println(name2)
}
Security call operators
fun main() {
var name :String? = "zhangsan"
name = null
// Change the first letter to capital
// If name yes null, The code after the question mark is not executed , No null pointer exception will be thrown
val r = name?.capitalize()
println(r)
}
Use let Security call for
package com.lmp.s1
fun main() {
var name:String? = null
// var name = "lmp"
name = ""
var r = name?.let {
//it == name In itself
// If name It's empty , Do not execute after the question mark
if(it.isBlank())// If name yes ”“( No content )
{
"default"
}else{
"{
$it}"
}
}
println(r)
}
Non empty assertion operators (!!)
fun main() {
var name :String? = null
//!! Assertion
// No matter name Is it right? null, All implemented
var r = name!!.capitalize()
println(r)
}
Use if Judge null value
fun main() {
var name:String? = null
if(name != null){
var r = name.capitalize()
println(r)
}else{
println("name is null")
}
}
Null merge operator
fun main() {
var info : String ? = " Bruce Lee "
info = null
//?: If the front one is empty , Just output the following things
//xxx ?: " So you are null" If xx be equal to null, Will execute ?: The back area
println(info ?: " So you are null" )
//let function + Null merge operator
println(info ?.let{
"【$it】"} ?:" So you are null")
}
Exception handling and custom exception
fun main() {
try{
var info :String ?= null
checkException(info)
println(info!!.length)
}catch(e:Exception){
println("$e")
}
}
fun checkException(info: String?) {
info ?: throw CustomException()
}
class CustomException : IllegalArgumentException(" Your code is too loose ")
Prerequisite function
fun main() {
val value1 :String ?= null
val value2 :Boolean = false
// Throw abnormal function
//checkNotNull(value1)
// Throw abnormal function
//requireNotNull(value1)
// by false And throw exceptions
require(value2)
}
substring function
const val INFO = "lmp is Success Result"
fun main() {
val indexOf = INFO.indexOf("i")
println(INFO.substring(0, indexOf))
println(INFO.substring(0 until indexOf))
}
split operation
fun main() {
val jsonTest = "Derry,lmp"
val list = jsonTest.split(",")
// Deconstruction operations
val (v1,v2) = list
println("$v1.$v2")
}
replace Complete encryption and decryption operation
fun main() {
val sourcePwd = "lmp"
println(" The original code is $sourcePwd")
// Encryption operation : Is to replace characters with numbers
val newPwd = sourcePwd.replace(Regex("[lm]")){
when(it.value){
"l" -> "9"
"m" -> "8"
"p" -> "7"
else->it.value
}
}
println(" The encrypted password is $newPwd")
// Decryption operation
val sourcePwdNew = newPwd.replace(Regex("[987]")){
when(it.value) {
"9" -> "l"
"8" -> "m"
"7" -> "p"
else -> it.value
}
}
println(" The decrypted password is $sourcePwdNew")
}
== and === Comparison operation
fun main() {
//== value Comparison of contents amount to java Of equals
//=== Comparison of references
val name1 = "lmp"
val name2 = "lmp"
val name3 = "ww"
println(name1.equals(name2))
println(name1 == name2)
println(name1 === name2) // true, Because it refers to the string constant pool
println(name1 == name3)
val name4 = "lmp".capitalize()
println(name1 == name4)
println(name1 === name4)
}

————————————————————————
It's not easy to code words , Everyone's support is my driving force to stick to it
Copyright notice : This paper is about CSDN Blogger 「 The top 100 most handsome faces in the Asia Pacific region 101 name 」 The original article of
边栏推荐
- OSEK standard ISO_ 17356 summary introduction
- Redis introduction complete tutorial: Collection details
- Notepad++ -- editing skills
- 推荐收藏:跨云数据仓库(data warehouse)环境搭建,这货特别干!
- 解决无法通过ssh服务远程连接虚拟机
- SHP data making 3dfiles white film
- 【二叉树】节点与其祖先之间的最大差值
- Wechat official account solves the cache problem of entering from the customized menu
- [crawler] jsonpath for data extraction
- SPH中的粒子初始排列问题(两张图解决)
猜你喜欢

初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)

OSEK标准ISO_17356汇总介绍

【二叉树】节点与其祖先之间的最大差值

【kotlin】第三天

PMP证书续证流程

The initial trial is the cross device model upgrade version of Ruijie switch (taking rg-s2952g-e as an example)

PS style JS webpage graffiti board plug-in

位运算符讲解

phpcms付费阅读功能支付宝支付

colResizable. JS auto adjust table width plug-in
随机推荐
QT addition calculator (simple case)
Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
Ffmpeg quick clip
45岁教授,她投出2个超级独角兽
[binary tree] the maximum difference between a node and its ancestor
头文件重复定义问题解决“C1014错误“
Redis:Redis消息的发布与订阅(了解)
Basic use and upgrade of Android native database
【taichi】用最少的修改将太极的pbf2d(基于位置的流体模拟)改为pbf3d
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
A mining of edu certificate station
D3.js+Three. JS data visualization 3D Earth JS special effect
PS style JS webpage graffiti board plug-in
Actual combat simulation │ JWT login authentication
企业里Win10 开启BitLocker锁定磁盘,如何备份系统,当系统出现问题又如何恢复,快速恢复又兼顾系统安全(远程设备篇)
OSEK standard ISO_ 17356 summary introduction
微信公众号解决从自定义菜单进入的缓存问题
高配笔记本使用CAD搬砖时卡死解决记录
香港珠宝大亨,22亿“抄底”佐丹奴
Header file duplicate definition problem solving "c1014 error“