1.Kotlin的可空性
fun main() {
var str:String? = "butterfly" //?问好代表可空类型
str= null
}
安全的管理
1.1 安全操作调用符
fun main() {
var str:String? = "butterfly" //?问好代表可空类型
str= null
println(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 = null
println(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 = null
if (str != null) {
str = str.capitalize()
} else {
println("为null.")
}
str = "rose "
str = str?.capitalize()?.plus("is great.")
println(str ?: "jack") //如果为null就是jack,否则 就按正常输出
}
str = null
str=str?.let { it.capitalize() }?: "butterfly"
println(str ?: "jack") //如果为null就是jack,否则 就按正常输出
2.异常
import java.lang.NullPointerException
fun main() {
var number:Int ?= null
try {
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.NullPointerException
fun main() {
var number:Int ?= null
try {
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.Source
const 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.File
fun 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.Charset
fun 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 >= 10
fun 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.File
fun 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.File
fun main() {
val result = File("H:\\tes1t\\test.txt")
.takeIf { it.exists() && it.canRead() }
?.readText()
println(result)
}
9.7 takeUnless
文章来源:https://www.toymoban.com/news/detail-826779.html
import java.io.File
fun main() {
val result = File("H:\\test\\test.txt")
.takeUnless { it.isHidden }
?.readText()
println(result)
}
文章来源地址https://www.toymoban.com/news/detail-826779.html
到了这里,关于Kotlin基本语法2基本内置方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!