当前位置:网站首页>【kotlin】第三天
【kotlin】第三天
2022-07-04 23:02:00 【亚太地区百大最帅面孔第101名】
这里是引用
️ Author: 老九
️ 个人博客:老九的CSDN博客
个人名言:不可控之事 乐观面对
系列专栏:
这里是引用
文章目录
可空性特点(?)
fun main() {
var name:String = "lmp"
//这个默认是不可空类型
//name = null
println(name)
//声明时:指定为可空类型(类型后面加一个问号)
var name2 : String ?
name2 = null
println(name2)
}
安全调用操作符
fun main() {
var name :String? = "zhangsan"
name = null
//把第一个字母改成大写
//如果name是null,问号后面的代码就不执行,就不会引发空指针异常
val r = name?.capitalize()
println(r)
}
使用let的安全调用
package com.lmp.s1
fun main() {
var name:String? = null
// var name = "lmp"
name = ""
var r = name?.let {
//it == name 本身
//如果name为空,问号后面不执行
if(it.isBlank())//如果name是”“(没有内容)
{
"default"
}else{
"{
$it}"
}
}
println(r)
}
非空断言操作符(!!)
fun main() {
var name :String? = null
//!! 断言
//不管name是不是null,都执行
var r = name!!.capitalize()
println(r)
}
使用if判断null值
fun main() {
var name:String? = null
if(name != null){
var r = name.capitalize()
println(r)
}else{
println("name is null")
}
}
空合并操作符
fun main() {
var info : String ? = "李小龙"
info = null
//?:如果前面的是空,就输出后面的东西
//xxx ?: "原来你是null" 如果xx等于null,就会执行?:后面的区域
println(info ?: "原来你是null" )
//let函数+空合并操作符
println(info ?.let{
"【$it】"} ?:"原来你是null")
}
异常处理与自定义异常
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("你的代码太不严谨了")
先决条件函数
fun main() {
val value1 :String ?= null
val value2 :Boolean = false
//抛异常的函数
//checkNotNull(value1)
//抛异常的函数
//requireNotNull(value1)
//为false也抛异常
require(value2)
}
substring函数
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操作
fun main() {
val jsonTest = "Derry,lmp"
val list = jsonTest.split(",")
//解构操作
val (v1,v2) = list
println("$v1.$v2")
}
replace完成加密解密操作
fun main() {
val sourcePwd = "lmp"
println("原始密码是$sourcePwd")
//加密操作:就是把字符替换成数字
val newPwd = sourcePwd.replace(Regex("[lm]")){
when(it.value){
"l" -> "9"
"m" -> "8"
"p" -> "7"
else->it.value
}
}
println("加密后的密码是$newPwd")
//解密操作
val sourcePwdNew = newPwd.replace(Regex("[987]")){
when(it.value) {
"9" -> "l"
"8" -> "m"
"7" -> "p"
else -> it.value
}
}
println("解密后的密码是$sourcePwdNew")
}
== 和 ===比较操作
fun main() {
//== 值 内容的比较 相当于java的equals
//=== 引用的比较
val name1 = "lmp"
val name2 = "lmp"
val name3 = "ww"
println(name1.equals(name2))
println(name1 == name2)
println(name1 === name2) // true,因为引用的是字符串常量池那个字符串
println(name1 == name3)
val name4 = "lmp".capitalize()
println(name1 == name4)
println(name1 === name4)
}

————————————————————————
码字不易,大家的支持就是我坚持下去的动力
版权声明:本文为CSDN博主「亚太地区百大最帅面孔第101名」的原创文章
边栏推荐
- P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
- A complete tutorial for getting started with redis: understanding and using APIs
- The Chinese output of servlet server and client is garbled
- PICT 生成正交测试用例教程
- VIM editor knowledge summary
- The caching feature of docker image and dockerfile
- ECCV 2022 | 腾讯优图提出DisCo:拯救小模型在自监督学习中的效果
- The difference between debug and release
- Explanation of bitwise operators
- Editplus-- usage -- shortcut key / configuration / background color / font size
猜你喜欢

Stm32 Reverse Introduction to CTF Competition Interpretation

Redis getting started complete tutorial: Geo

壁仞科技研究院前沿技术文章精选

Redis入門完整教程:Pipeline

A mining of edu certificate station

MariaDB的Galera集群-双主双活安装设置

debug和release的区别

colResizable. JS auto adjust table width plug-in

C language to quickly solve the reverse linked list

Redis:Redis的事务
随机推荐
Redis getting started complete tutorial: Key Management
Complete tutorial for getting started with redis: bitmaps
HMS core machine learning service
Redis introduction complete tutorial: Collection details
机器学习在房屋价格预测上的应用
Insert sort, select sort, bubble sort
MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
One of the commonly used technical indicators, reading boll Bollinger line indicators
Application of machine learning in housing price prediction
Redis入门完整教程:Redis使用场景
Mysql database backup and recovery -- mysqldump command
[try to hack] wide byte injection
CTF竞赛题解之stm32逆向入门
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
A complete tutorial for getting started with redis: redis shell
【剑指offer】1-5题
【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
Observable time series data downsampling practice in Prometheus
[sword finger offer] questions 1-5
Basic use and upgrade of Android native database