fun main() {
var str:String? = "butterfly" //?问好代表可空类型
str= null
}
安全的管理
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)
}
fun main() {
var str:String ?= "1"
str = null
println(str!!.capitalize())
}
fun main() {
var str:String? = readLine()
if (str!=null){
str=str.capitalize()
}else{
println("为null.")
}
}
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,否则 就按正常输出
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("操作不当")
就是判断条件是否满足,例如非空
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("操作不当")
const val NAME="Jimmy's friend"
fun main() {
val index = NAME.indexOf("'")
// println(NAME.substring(0, index))
var str = NAME.substring(0..
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..
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)
}
fun main() {
val str1 ="Jason"
val str2 = "jason".capitalize()
println(str1==str2)
println(str1===str2)
}
fun main() {
"The people's Republic of China.".forEach(::println)
}
fun main() {
// val number1:Int = "8.98".toInt()
val number1:Double? = "8.98".toDoubleOrNull()
println(number1)
}
println(8.98656.toInt())//取整
println(8.98656.roundToInt())//四舍五入取整
val s = "%.2f".format(8.956756)//四舍五入保留两位小数
println(s)
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)
}
}
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?"
}
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."
}
}
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)
}
新版本 他是把 作用域传给 里面,然后返回这个对象
import java.io.File
fun main() {
var fileContents:List
val apply:File = File("H:\\test\\test.txt").apply {
println(name)
}.apply {
fileContents = readLines()
}
println(fileContents)
}
import java.io.File
fun main() {
val result = File("H:\\tes1t\\test.txt")
.takeIf { it.exists() && it.canRead() }
?.readText()
println(result)
}
import java.io.File
fun main() {
val result = File("H:\\test\\test.txt")
.takeUnless { it.isHidden }
?.readText()
println(result)
}