1.Kotlin的可空性
fun main() {var str:String? = "butterfly" //?问好代表可空类型str= null
}
安全的管理
1.1 安全操作调用符
fun main() {var str:String? = "butterfly" //?问好代表可空类型str= nullprintln(str?.capitalize())//当String为null时候输出null
}
fun main() {var str:String? = "butterfly" //?问好代表可空类型
// str= ""str = str?.let {//非空白的字符串if (it.isNotBlank()){it.capitalize()}else{"butterfly"}}println(str)}
1.2 非空断言操作符
fun main() {var str:String ?= "1"str = nullprintln(str!!.capitalize())
}
1.3 使用if判断
fun main() {var str:String? = readLine()if (str!=null){str=str.capitalize()}else{println("为null.")}
}
1.4 空合并操作符
fun main() {var str: String? = "12"str = nullif (str != null) {str = str.capitalize()} else {println("为null.")}str = "rose "str = str?.capitalize()?.plus("is great.")println(str ?: "jack") //如果为null就是jack,否则 就按正常输出}
str = nullstr=str?.let { it.capitalize() }?: "butterfly"println(str ?: "jack") //如果为null就是jack,否则 就按正常输出
2.异常
import java.lang.NullPointerExceptionfun main() {var number:Int ?= nulltry {checkOperation(number)number!!.plus(1)}catch (e:NullPointerException){e.printStackTrace()println("出现异常")}}fun checkOperation(number:Int?){number?:throw UnskilledException()
}//自定义异常
class UnskilledException() : IllegalArgumentException("操作不当")
3.先决条件函数
就是判断条件是否满足,例如非空
import java.lang.NullPointerExceptionfun main() {var number:Int ?= nulltry {checkOperation(number)number!!.plus(1)}catch (e:NullPointerException){e.printStackTrace()println("出现异常")}}fun checkOperation(number:Int?){checkNotNull(number,{"Something is not good"})
// number?:throw UnskilledException()
}//自定义异常
class UnskilledException() : IllegalArgumentException("操作不当")
4.字符串的操作
4.1 substring
const val NAME="Jimmy's friend"fun main() {val index = NAME.indexOf("'")
// println(NAME.substring(0, index))var str = NAME.substring(0..<index)}
4.2 split
import javax.xml.transform.Sourceconst val NAME="Jimmy's friend"
const val NAMES = "jack,jacky,jason"
fun main() {val index = NAME.indexOf("'")
// println(NAME.substring(0, index))var str = NAME.substring(0..<index)println(str)/*** 称之为结构语法*/val (origin,dest,proxy)= NAMES.split(",")println("$origin $dest $proxy")}
4.3 replace
fun main() {val str = "The people's Republic of China."val str1 = str.replace(Regex("[aeiou]"),{it->when(it.value){"a" -> "8""e" -> "6""i" -> "9""o" -> "1""u" -> "3"else -> it.value}})println(str)println(str1)
}
5.字符串的比较
fun main() {val str1 ="Jason"val str2 = "jason".capitalize()println(str1==str2)println(str1===str2)
}
是因为第二种方式是存在堆内存的引用而不是 常量池
6. 字符串的遍历
fun main() {"The people's Republic of China.".forEach(::println)
}
7.数字类型的安全转换函数
fun main() {
// val number1:Int = "8.98".toInt()val number1:Double? = "8.98".toDoubleOrNull()println(number1)
}
8. Double转Int 与 类型格式化
println(8.98656.toInt())//取整println(8.98656.roundToInt())//四舍五入取整val s = "%.2f".format(8.956756)//四舍五入保留两位小数println(s)
9.标准库函数
9.1 apply
import java.io.Filefun main() {val file1 = File("H:\\test\\i have a dream_copy.txt")file1.setReadable(true)file1.setWritable(true)file1.setExecutable(false)val file2 = File("H:\\test\\i have a dream_copy.txt").apply {setReadable(true)setWritable(true)setExecutable(false)}}
9.2 let
fun main() {val result = listOf(3, 2, 1).first().let {it * it}println(result)
}
fun main() {val result = listOf(3, 2, 1).first().let {it * it}println(result)println(formatGreeting("jack"))}fun formatGreeting(guestNmae: String?): String {return guestNmae?.let { "Welcome ,$it" } ?: "What's your name?"
}
链式调用 流行
9.3 run
import java.io.File
import java.nio.charset.Charsetfun main() {var file = File("H:\\test\\test.txt")val run = file.run {//和apply很像readText().contains("great") //返回的类型 就是lambda表达式的最后一行}println(run)println("========================")"The people's Republic of China.".run(::isLong).run(::showMessage).run(::println)}fun isLong(name: String) = name.length >= 10fun showMessage(isLong:Boolean):String{return if (isLong){"Name is too long."}else{"Please rename."}
}
9.4 with
fun main() {val run = "The people's Republic of China.".run { length >= 10 }val with = with("The people's Republic of China.") {length >= 10}println(with)}
9.5 also
新版本 他是把 作用域传给 里面,然后返回这个对象
import java.io.Filefun main() {var fileContents:List<String>val apply:File = File("H:\\test\\test.txt").apply {println(name)}.apply {fileContents = readLines()}println(fileContents)
}
9.6 takeif
import java.io.Filefun main() {val result = File("H:\\tes1t\\test.txt").takeIf { it.exists() && it.canRead() }?.readText()println(result)}
9.7 takeUnless
import java.io.Filefun main() {val result = File("H:\\test\\test.txt").takeUnless { it.isHidden }?.readText()println(result)}