zoukankan      html  css  js  c++  java
  • kotlin string

    Kotlin String split 操作实践

     

    内容

    此文章展示kotlin中对String字符串的split操作,如果你有遇到这方面的需求,希望对你有用。

    1. split + 正则 

    先看下系统函数的定义,接收两个函数:
        regex:表示一个不可变的正则表达式
        limit:非负的值指定要返回的子字符串的最大数量。零默认方式是无限制的

    inline fun CharSequence.split(regex: Regex, limit: Int = 0): List<String>

    kotlin 提供了扩展函数toRegex()将字符串转换为正则表达式,下面请看

    栗子:

    val str = "Kotlination.com = Be Kotlineer - Be Simple - Be Connective"

    val separate1 = str.split("=|-".toRegex())

    运行结果:

    [Kotlination.com , Be Kotlineer , Be Simple , Be Connective]

    2. split + 任意字符串

    先看函数定义:
    delimiters:一个或多个字符作为分隔符
    ignoreCase: 在匹配分隔符时忽略字符情况。默认'false'。
    limit:返回子字符串的最大数量

    fun CharSequence.split(vararg delimiters: String, ignoreCase: Boolean = false, limit: Int = 0): List<String>

    栗子:

    val str = "Kotlination.com = Be Kotlineer - Be Simple - Be Connective"

    val separate2 = str.split(" = "," - ")

    结果:

    [Kotlination.com , Be Kotlineer , Be Simple , Be Connective]

  • 相关阅读:
    window对象的方法
    JS注册事件
    JS总结
    JS 扩展方法prototype
    Codeforces 460D Little Victor and Set(看题解)
    Codeforces 891C Envy
    Codeforces 251C Number Transformation
    Codeforces 490F Treeland Tour 树形dp
    Codeforces 605C Freelancer's Dreams 凸包 (看题解)
    几何模板
  • 原文地址:https://www.cnblogs.com/vana/p/10201290.html
Copyright © 2011-2022 走看看